physic object collision detection ?
physic object collision detection ?
i read some docs about it.. but im not really understand it.. is it possible to check collision from the object physics shape ? please make it simple
-
Re: physic object collision detection ?
Collision events are handled by the world.
You can set callback events which provide you with a reference to the fixture of both colliding elements and a contact object which has some more data.
Via the fixture you can access the shape. You can also supply userdata to the fixture to have a reference to a custom table (for example a custom class of yours) if you need that data. Via which you could call a function that all your physics objects have but handle things differently on a per object basis.
Take a look at this tutorial if you haven't already:
https://love2d.org/wiki/Tutorial:Physic ... nCallbacks
You can set callback events which provide you with a reference to the fixture of both colliding elements and a contact object which has some more data.
Via the fixture you can access the shape. You can also supply userdata to the fixture to have a reference to a custom table (for example a custom class of yours) if you need that data. Via which you could call a function that all your physics objects have but handle things differently on a per object basis.
Take a look at this tutorial if you haven't already:
https://love2d.org/wiki/Tutorial:Physic ... nCallbacks
Re: physic object collision detection ?
i have a kinda more likely to work way to handle collision beacause i've tried this tutorial, and it havent been very successful to me.
i used few variables
i added the collisions of a player by adding it's position.
then i added this function
and finally i implemented 4 lines that composed a collision rectangle for the player.
EDIT : you can put anything you want in those CheckCollision events
and everything you put will be encountered only when colliding from the direction you set
exemple :
then a will be incremented by 1 only when colliding to the down.
i used few variables
Code: Select all
up = 0
left = 0
right = 0
down = 0
Code: Select all
player.x = 100
player.y = 200
Code: Select all
shape.x = 300
shape.y = 300
shape.wid = 32
shape.hei = 32
Code: Select all
function CheckCollision(x1,y1,w1,h1, x2,y2,w2,h2)--
local bx1,by1,bx2, by2 = x1 + w1 ,y1 + h1, x2 + w2, y2 + h2
return x1 < x2+w2 and
x2 < x1+w1 and
y1 < y2+h2 and
y2 < y1+h1
end
Code: Select all
if CheckCollision (player.x + 19, player.y + 13, 20, 1, shape.x, shape.y, shape.wid ,shape.hei)then
-- the first four values correspond to the first shape, the other fours the second shape.
up = 1
else
up = 0
end
if CheckCollision (player.x + 17, player.y + 14, 1, 20, shape.x, shape.y, shape.wid ,shape.hei)then
left = 1
else
left = 0
end
if CheckCollision (player.x + 40, player.y + 14, 1, 20, shape.x, shape.y,shape.wid, shape.hei)then
else
right = 0
end
if CheckCollision (player.x + 19, player.y + 35, 20, 1, shape.x, shape.y, shape.wid ,shape.hei)then
down = 1
else
down = 0
EDIT : you can put anything you want in those CheckCollision events
and everything you put will be encountered only when colliding from the direction you set
exemple :
Code: Select all
if CheckCollision (player.x + 19, player.y + 35, 20, 1, shape.x, shape.y, shape.wid ,shape.hei)then
down = 1
a= a + 1
else
down = 0
Who is online
Users browsing this forum: Google [Bot] and 15 guests