solved now, started the code all over, its more generally now (no special function for every direction) added a array with direction-data
NOTE: had to increase the radius... cause it was able to run through block
something i still do NOT understand...
when i check the radius to be
<=8 i can walk through
<9 i can not walk through
thought smaller&eqal 8 would be -??..8
and smaller than 9 would be -??..8 too
here i still let the original question remaining due some ppl may have a look on the idea itself (even if its something basic :D )
hello, i try to detect collisions via distance vector,
i'm working with a 16x16px tile-based "engine"
so my aim is:
get the middle of my player1 (got that by player.p1.posx, player.p1.posy)
fine so we got the coordinates for my player1
further we have a battlefield
when moving up i have to check TWO points
the lower right and the lower left edge of the tile above the player (where he wants to go to!)
so i need to get the tilex and tiley# of the player, where he is standing
also got that by player.p1.tilex, playerp1.tiley
we are getting somewhere ;)
further i get the tile above the player by: Map[player.p1.tilex][player.p1.tiley-1]
note: -1 cause we want the one above him!
in keyboard controls i use this code:
Code: Select all
pad = Controls.read()
p1_moves = false
if pad:up() then
if canMove(up) then
--movement-code here
end
end
--more code for other directions....
take 2 points and see if the distance is smaller than 4 (radius)
Code: Select all
function collide(x1,y1,x2,y2)
dx=(x2 - x1)
dy=(y2 - y1)
distance = math.sqrt( dx*dx + dy*dy )
if distance<4 then
return true
else
return false
end
end
--note: direction is an integer value 1..4 defined as global variable (for easier programming, than remembering was 1 up or was it 0)
Code: Select all
function canMove(direction)
--check the distance between two points...
-- x1,y1 & x2,y2
--if dx = (x2 - x1) and dy = (y2 - y1) then
--distance = square root of ( dx * dx + dy * dy )
--This assumes x1,y1 is the centre of bomberman
--check 2 points for collision (of ONE tile) <-- SIMPLYFIED, was 4 once!
--x;x+16,y;y+16
--set bombermans center:
x1=player.p1.posx
y1=player.p1.posy
x2=player.p1.tilex --check some variations
y2=player.p1.tiley --check some variations
--check the DOWN direction
if direction==down then
--check the upper 2 points
--point1 upper left one
if collide(x1,y1,x2,y2) then
return false
--point2 upper right one (+16 cause i have 16x16 sized tiles)
elseif collide(x1,y1,x2+16,y2) then
return false
else
return true
end
--other directions follow here
end
but i doo not want to increase the radius cause otherwise i have to
be exactly on the pixel to pass the block...
hope you get what i mean&try
greets LuMo