Page 1 of 1

Proper Way to Position Outside of Physics

Posted: Wed Sep 30, 2009 3:28 pm
by hemmi
Hey guys,

I've got a body that needs to be repositioned at any specified point. Is it safe to simply change the position vector of the body or do I need to take extra steps so as not to throw off the physics? cpBodySlew seems like the only other option, but I don't really want the body to hit anything on the way to where it needs to go. Any ideas? Thanks!

Re: Proper Way to Position Outside of Physics

Posted: Wed Sep 30, 2009 3:40 pm
by slembcke
Setting the position directly is only problematic when you do it every frame. If you only do it once in a while, it should work exactly like you expect.

Re: Proper Way to Position Outside of Physics

Posted: Wed Sep 30, 2009 4:06 pm
by hemmi
Actually I do need to do it periodically every frame. I'm implementing a system to keep two bodies in sync with each other. Is cpBodySlew okay for this? I actually realized I need to both move it so it interacts with physics as well as flat out reposition.

Re: Proper Way to Position Outside of Physics

Posted: Wed Sep 30, 2009 4:10 pm
by slembcke
You'll want to use a joint then. Repositioning them every frame is going to give you disappointing and unstable results.

Re: Proper Way to Position Outside of Physics

Posted: Wed Sep 30, 2009 4:59 pm
by hemmi
I'm sorry I'm still a little unclear. You mean in general? cpBodySlew won't work? Well the syncing is kind of arbitrary, not necessarily on the same axis either.

Re: Proper Way to Position Outside of Physics

Posted: Wed Sep 30, 2009 6:50 pm
by zaerl
So basically speaking you want to "teleport" your object in another place right? cpBodySlew actually change the body's velocity. Well in my opinion you can use cpBodySetPos but use it carefully cause the official documentation states:
Don't modify a body's position every step unless you really know what you are doing. Otherwise you're likely to get the position/velocity badly out of sync.
You can have problems if you move your body in a wrong position (generating for example an interpenetration between bodies), if it has constraints attached to itself or some other issues.

Regards.

Re: Proper Way to Position Outside of Physics

Posted: Wed Sep 30, 2009 7:21 pm
by hemmi
Well, it gets a little complicated, but there's three things I need to do within my application:

1) reposition a body to an arbitrary location without it interacting with the rest of the physics

2) move a body an arbitrary distance so that it does interact with the physics (this could be extented to a version of #1 that interacts with the physics - moving a large distance to the new point instead of just changing the position manually)

3) animate a body while still keeping it in line with the physics

#3 is really just an extension of #2

It looks like for #1 the only option is to manually change the position? That should be fine as it won't happen over and over again at once.

As I see it, there are two options for #2 and #3. Either use cpBodySlew to move the body, making sure to zero the velocity when done, or attach a "middle man" body to each real body by a joint, without adding a shape for the middle man. The middle man can then be used to move the real body in either my syncing system or the animation system, each having only to set the middle man body position, the real body following.

Will either of these methods, cpBodySlew or a middle man body, work for my purposes? If so, is there a reason to prefer one over another?

Am I missing something here? I'd really appreciate some help with this on a design level, I'm still getting acquainted with chipmunk.

Thanks! :D

Re: Proper Way to Position Outside of Physics

Posted: Thu Oct 01, 2009 5:33 am
by zaerl
1) reposition a body to an arbitrary location without it interacting with the rest of the physics
Use, wisely, cpBodySetPos.
2) move a body an arbitrary distance so that it does interact with the physics (this could be extented to a version of #1 that interacts with the physics - moving a large distance to the new point instead of just changing the position manually)
Move the body with cpBodySlew using a custom collision callback function as described in the Wiki. Just check if the object that is colliding is your object and that is time for it to not collide with anything. Return 0 when you what no collisions.
3) animate a body while still keeping it in line with the physics
Use joints, impulses, forces or whatever you want.

Re: Proper Way to Position Outside of Physics

Posted: Thu Oct 01, 2009 8:28 am
by slembcke
If you are using the trunk code (I know, I know, I *still* haven't made a release), joints give you a lot more control than you might associate with joints. For instance, you could attach a pivot joint between a body and the space and move the anchors around freely. There are a few parameters that you have to control how the joint corrects itself:
biasCoef - The rate at which joints are corrected, 0 means the joint cannot correct itself, 1 means it corrects itself instantly (very bad actually). Defaults to 10% of joint error per step.
maxBias - The maximum linear correction speed. Defaults to infinity.
maxForce - The maximum correction force so your joint can't push things through solid objects. Defaults to infinity.

You might give that a try as well. That is how the mouse control works in the trunk demos.

Re: Proper Way to Position Outside of Physics

Posted: Fri Oct 02, 2009 2:09 pm
by mobilebros
I had a similar problem, working on the iphone with Cocos2d; in cocos2d you can move sprites and such around with actions, I pretty much would remove the body from the space when an action took over, and then when the position was updated I used cpBodySlew to get the velocity to coincide with what the action was doing to it... when the action was done, I added the body back to the space to let the real physics take over (aka no more body slew-age)... this worked extremely well for my case, maybe it'll help yours.