Physics shapes issue

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
v1rr3n
Prole
Posts: 4
Joined: Fri Oct 08, 2010 9:30 pm

Physics shapes issue

Post 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.
User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: Physics shapes issue

Post 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
v1rr3n
Prole
Posts: 4
Joined: Fri Oct 08, 2010 9:30 pm

Re: Physics shapes issue

Post 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?
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Physics shapes issue

Post 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.
User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: Physics shapes issue

Post 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?
v1rr3n
Prole
Posts: 4
Joined: Fri Oct 08, 2010 9:30 pm

Re: Physics shapes issue

Post 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.
User avatar
rhezalouis
Party member
Posts: 100
Joined: Mon Dec 07, 2009 10:27 am
Location: Indonesia
Contact:

[Response]Non-destructive Solution?

Post 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.
Attachments
phaseEngine.love
example
(773 Bytes) Downloaded 205 times
Aargh, I am wasting my posts! My citizenshiiiip... :o
v1rr3n
Prole
Posts: 4
Joined: Fri Oct 08, 2010 9:30 pm

Re: Physics shapes issue

Post by v1rr3n »

Wow, thats a brilliant solution. I'll do that instead. Thanks that should simplify my code a lot as well.
User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: Physics shapes issue

Post 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. :)
Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests