Can a physics object's density be changed?

Can a physics object's density be modified after it's been created (without destroying and recreating the physics object)? I can't seem to find a way to do it, nor can I find anything in previous posts or the documentation. For what I'm doing, I don't think destroying and recreating is an option because the objects are members of a joint.

Gentle bump :)

That's a good question. Why would you want to though?

In my game I am creating a chain by colliding one object with another. When the objects collide I create a pivot joint. In order for a chain to work properly with Box2D (with gravity enabled), each subsequent link in the chain needs to have a lower density than the one above it. If you break that rule, the chain has undesirable stretching problems. So what I'd like to do is change an object's density when it joins the chain, and change it back if it gets disconnected.

Further complicating the issue is the fact that the objects I'd like to change the density of are already connected to another object via a physics joint, so I don't think it would work for me in this case to destroy the colliding physics object and recreate it.

Hm. Can you store the density for the chain links in a table and pull them out as they're created? That way you could specify them to your liking?

Hmm. All of my links are created at the time the level starts. I already have the density value stored in a variable, but I guess I haven't tried changing the value of that variable (all of my chain links currently use that same variable). I was looking for a way to "access" the density property/value of a physics object, but if the physics engine is constantly sampling the density value then I suppose I could setup a better table/variable system for setting density values. I'll have to test it out this evening. I suspect though that once a physics object is created, its density and other attributes are locked. I'll let you know what I find. Thanks for your help!

Yeah, I'm pretty sure you're right that the density property is static and not updated or looked at after the physics.addBody happens.

What I was suggesting was that upon creation of the links to look at pulling a different value in for each. If you're creating the chain at random this will probably be more difficult/impossible, but if you know which order they will be it shouldn't be too difficult.

When a link collides but just before it is jointed, remove it and spawn a new one in the same position, but with the desired physics.

I can't do that in this case because the object I would destroy/recreate is a already a member of another pivot joint (think of it as connecting an existing chain to another chain).

@3cordDesign

Unfortunately, I am creating the chain at random because the user can drag any one of the links up to add to the previous link. Difficult/impossible is what I'm up against and unfortunately it's an app-stopper for me.

Unless anyone knows of a better/more efficient way of creating a chain, I'm stuck. I've been all through the forums reading about physics joints and think the density rule is just an unavoidable Box2D limitation.

Yikes. That's a tough one. I'm pretty new at this stuff too, I just hate it when I have to bump my posts for people to look at them. ;)

:) Thanks, I appreciate you replying. Despite this recent setback, I have been EXTREMELY impressed with Corona and it's user community. I'm having a ton of fun using it--and that's coming from someone with a graphic arts background and not a lot of previous programming experience. Can't say enough good things about it.

yeah, I'm in the same boat as you. Graphics experience and not much coding. But Corona does make it approachable. The forums are sometimes hit or miss. :)

Can you clone the entire chain (and then delete the original) when a new link is added, redoing the physics from the top down?

I thought of that, but in this case I'm not sure that's practical because the downstram "chain" is actually fairly complicated (not just a simple chain). I might toy around with this if changing the variable doesn't work. Thanks for the suggestion!

Does anyone know if starting and stopping or pausing the physics engine causes it to look at the physics parameters? If it did maybe you could do a quick pause and start again?

Ok, just wanted to come back and post my findings:

1) As I suspected, changing the variable where the density was assigned does not affect physics objects that have already been created.

2) Starting and stopping the physics engine causes objects to freeze in place, which leads me to...

3) Removing and recreating the chain DOES produce the desired changes to the chain, however the "switch" is not invisible. For example, if you have a chain swinging around and you remove it and recreate a new one, the old chain disappears mid-swing and the new chain is created hanging straight down (or wherever you create it).

Looks like I'm out of luck on this one. Thanks everyone for your suggestions!

views:1620 update:2011/9/28 9:01:40
corona forums © 2003-2011