Page 1 of 1

Physics shapes issue

Posted: Tue Mar 29, 2011 4:26 am
by v1rr3n
I am trying to add more than one physics shapes to a body and remove just one of them. The problem is I can't seem to get the work around of setting the mask to all categories and removing one shape to work. If I try this for example:

Code: Select all

world:update(dt)
for _, shape in ipairs(deleteMe) do --deleteMe is an array that has shapes to be deleted
	shape:destroy()
end
it doesn't work and love2d crashes. My question is: Is it possible to remove just one shape from a body given love2d's problem with shape:destroy()? Please note that I've eliminated all possible references in my code to the shape and it still results in a crash.

Re: Physics shapes issue

Posted: Tue Mar 29, 2011 4:42 am
by BlackBulletIV
As far as I know, on top of masking, you must also wait a frame (this is annoying I know... hopefully this will be fixed sometime in the future). Try this:

Code: Select all

world:update(dt)
for _, shape in ipairs(deleteMe) do
  shape:destroy()
end

-- use the code in this loop for every shape that needs to be destroy
for _, shape in ipairs(shapesToBeDestroyed) do
  shape:setMask(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)
  table.insert(deleteMe, shape)
end

Re: Physics shapes issue

Posted: Tue Mar 29, 2011 5:11 am
by v1rr3n
I tried the setMask and the result is the same. I wanted to attach a sensor to a body that already has a non-sensor shape. The sensor would only appear when a key is pressed and disappear when released (created and destroyed). Basically it is simulating a punch or melee attack. I can't seem to get destroy to work and my deadline approaches :? . One solution is I can perhaps set the sensor inside of the other shape and when a key is pressed set the sensor outside the other shape where the punch needs to be. I'll try that instead. I just hope that when I get to destroying the baddies things go easier. After all, I can set them outside of any collisions and handle them that way hopefully.

Edit: I don't know if I can set the position of a body once it is attached. So I don't know what I'm going to do. Anyone have a suggestion as to how to do a physical attack?

Re: Physics shapes issue

Posted: Tue Mar 29, 2011 5:44 am
by ivan
v1rr3n wrote:it doesn't work and love2d crashes. My question is: Is it possible to remove just one shape from a body given love2d's problem with shape:destroy()?
I think it is possible although it depends on WHEN you are trying to destroy the particular shape.
Box2D doesn't allow destroying or creating shapes during collision callbacks for example.
Hmm in your case something else might be causing the crash.
v1rr3n wrote:So I don't know what I'm going to do. Anyone have a suggestion as to how to do a physical attack?
A simple approach would be not to destroy the sensor at all.
Whenever the user presses the attack button simply iterate all shapes/bodies that intersect the sensor and call their 'takeHit' function.
If you had a messaging system in your game this would be pretty easy to implement.

Re: Physics shapes issue

Posted: Tue Mar 29, 2011 8:51 am
by BlackBulletIV
v1rr3n wrote:I tried the setMask and the result is the same. I wanted to attach a sensor to a body that already has a non-sensor shape. The sensor would only appear when a key is pressed and disappear when released (created and destroyed). Basically it is simulating a punch or melee attack. I can't seem to get destroy to work and my deadline approaches :? . One solution is I can perhaps set the sensor inside of the other shape and when a key is pressed set the sensor outside the other shape where the punch needs to be. I'll try that instead. I just hope that when I get to destroying the baddies things go easier. After all, I can set them outside of any collisions and handle them that way hopefully.

Edit: I don't know if I can set the position of a body once it is attached. So I don't know what I'm going to do. Anyone have a suggestion as to how to do a physical attack?
Did you look at any of the code apart from the setMask bit? The code has a one frame delay, because it shifts the shapes needing to be deleted after checking if anything is ready to be deleted. The action flow might go like this:

-- First update --
Update world.
Loop through shapes in deleteMe. Nothing found, nothing destroyed.
Add a shape that needs to be destroyed.
Loop through shapes to be destroyed. One found. Mask it (removing it from any collision) and insert into deleteMe.
Empty the table of shapes to be destroyed.

-- Second update --
Update world.
Loop through shapes in deleteMe. One found. By now collision info is gone. Safe to destroy.
Loop through shapes to be destroyed. None found.

Does that help?

Re: Physics shapes issue

Posted: Tue Mar 29, 2011 2:15 pm
by v1rr3n
Thanks I went back and revisited the problem and I wasn't emptying the table every time I was deleting a shape. I appreciate the help Ivan and Blackbullet.

[Response]Non-destructive Solution?

Posted: Tue Mar 29, 2011 2:47 pm
by rhezalouis
Hi v1rr3n,
v1rr3n wrote:The sensor would only appear when a key is pressed and disappear when released (created and destroyed). Basically it is simulating a punch or melee attack.
Is it acceptable to use shape mask to skip collision instead of destroying the shape? Basically, the shape you mask is instructed to avoid other shape having the mentioned category. So, instead of destroying it, you 'hide' it. Moreover, the graphical representation/animation could be adjusted according to the active mask of each shape.

P.S. Btw, you could comment out the timer variable and use keyboard 2-5 to manually alter the mask of the shapes.

Re: Physics shapes issue

Posted: Tue Mar 29, 2011 5:30 pm
by v1rr3n
Wow, thats a brilliant solution. I'll do that instead. Thanks that should simplify my code a lot as well.

Re: Physics shapes issue

Posted: Tue Mar 29, 2011 8:49 pm
by BlackBulletIV
v1rr3n wrote:Thanks I went back and revisited the problem and I wasn't emptying the table every time I was deleting a shape. I appreciate the help Ivan and Blackbullet.
No worries. :)