remove physics object when colliding ?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
Vrx8
Prole
Posts: 20
Joined: Sat Jun 17, 2017 5:35 am

remove physics object when colliding ?

Post by Vrx8 »

i use world:setCallbacks to handle physics object collisions, so i want to remove an object when its colliding with other, any idea on how to do it ? need the most simple way..
User avatar
Pospos
Citizen
Posts: 73
Joined: Sun Jun 18, 2017 11:10 am
Location: Agadir

Re: remove physics object when colliding ?

Post by Pospos »

this is a copy and paste of my response to Pg UP

it's my own way to handle physics, it's a simple way to handle physics object collisions
i used the world and physics engine but it was not very successful

i used few variables

Code: Select all

up = 0
left = 0
right = 0
down = 0
i added the collisions of a player by adding it's position.

Code: Select all

	player.x = 100
	player.y = 200

Code: Select all

shape.x = 300
shape.y = 300
shape.wid = 32
shape.hei = 32
then i added this function

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 

and finally i implemented 4 lines that composed a collision rectangle for the player.

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
     end

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
end

if you need to delete an object, simply cancel its effect by putting the condition under an if statement that disables physics
and sprites.

Code: Select all

while (condtition of removal == false)do
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
 end
end
User avatar
erasio
Party member
Posts: 118
Joined: Wed Mar 15, 2017 8:52 am
Location: Germany

Re: remove physics object when colliding ?

Post by erasio »

I would strongly suggest not doing that manually.

Box2D is a really solid implementation, has lots of additional features a physics engine would need and lots of optimizations to the collision step. Being written in C++ it is better than what you will be able to write (very very most likely and definitely better than anything you can do in a months work).

Physics objects have the function destroy to explicitly destroy them.

If you have your own objects (tables) you can add them as userdata to the fixture and use it on collision since you receive the fixture as a / b. That way you can nill them out or modify them as needed.
Post Reply

Who is online

Users browsing this forum: No registered users and 78 guests