Going Rogue Advice

Official forum for the Chipmunk2D Physics Library.
Post Reply
cocojoe
Posts: 13
Joined: Thu Mar 08, 2012 8:56 am
Contact:

Going Rogue Advice

Post by cocojoe »

I have a few bodies that I update in a manual physics step (the game is top-down puzzler, 0 gravity).
Would appreciate some advice on a couple of different scenarios. My main update loop consists of:

Code: Select all

        // Manual Physics Step
        for(Movement* manualBody in _manualStep)
        {
            if(manualBody.active) {
                [manualBody manualStep:timeStep];
            }
        }
        
        // Main Physics Step
        [_space step:timeStep];

For objects that simply move back and forth (simplest case) I use the following code in their manualStep:

Code: Select all

CGPoint newPos = ccpAdd(self.body.pos,ccpMult(_velocity,dt));
self.body.vel = cpvmult(cpvsub(newPos,self.body.pos), 1.0f/dt);
self.body.pos = newPos;
I remove the body from the space as I am processing manually. Although I wonder if I should leave the body in the simulation? and comment out the last line to set the new position as the main physics step would update this from the new velocity.

Should rogue bodies ideally be of infinite mass (static)? Some of the movement sub classes do things like use a motor to animate rotation so they can not be infinite or it breaks. Should I replace this with manual rotation of angle? (the effect is purely cosmetic, tbh as the fixture is a circle, could get away with simply rotating the sprite, if CCPhysicsSprite didn't over ride it ;)

One thing I did notice, if the body has infinite mass it will not collide with the space boundary? However it still collides fine with everything else, non-infinite works fine with space boundary.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Going Rogue Advice

Post by slembcke »

So I first off would recommend trying constraint controlled objects first. We made a tutorial that covers that here:
http://chipmunk-physics.net/tutorials/ChipmunkTileDemo/

The advantage of this is that you can get pretty close control of objects without needing to deal with the extra complexity of rogue bodies and it lets you do so while setting a maximum correction force.

Rogue bodies make things way complicated and I would recommend them as your last option. They get even more complicated if they are finite mass. (I would really really not recommend this!)

Modifying the velocity of a regular dynamic body can be problematic too since you are basically applying an impulse accelerate the body. If you are changing the velocity a lot in one step, you are applying a pretty big impulse, and doing that outside of a velocity integration callback can be problematic and cause a lot of overlap to happen. So if you do want to go this route, do it in a integration callback (or velocity update method in Obj-C, requires the Git code though), and clamp the velocity change to a reasonable amount of acceleration. Still, the problem with this method is that collisions with other objects will prevent you from attaining the velocity you want even if the force taken to move them is small.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
cocojoe
Posts: 13
Joined: Thu Mar 08, 2012 8:56 am
Contact:

Re: Going Rogue Advice

Post by cocojoe »

Thanks slembcke

I reviewed my code and I've restored the bodies to be part of the space, have removed the code to translate the bodies position and am only setting the velocity at start and when it needs to change. The only thing happening in the manual step is checking the bodies position relative to it's target location for the next action.

One thing I need to do is control the angle of a body semi-manually, I was manually modifying the angular velocity of this body with a EaseInOut method to achieve the desired look I wanted however it never feels smooth enough (time step 1/180). I changed it to use a motor and am modifying the motor rate per step instead (feels better).
Imagine a swinging axe however the axe can start at any angle and may also have any arc size.

Question I had with this, is it possible to set the initial angle of the body as the motor would if it had rotated it.
If I change the bodies angle manually, you will see it being corrected by the simulation and pulled back to the pivot point vs being rotated around the pivot joint in the first place?
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Going Rogue Advice

Post by slembcke »

Sure that's possible. You just have to set the position to correspond to where it would be if it was rotated around the pivot point like that.

I suppose the easiest way to do that is to:
1) Rotate the axe
2) Use [ChipmunkBody local2World:] to convert the relative anchor of the pivot (you can grab that from the pivot joint) to world coordinates.
3) Compare that to the world coordinate of the pivot point and add the difference to the axe's body.

Not a very functionally pure way of doing it, but it's hopefully more intuitive.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
cocojoe
Posts: 13
Joined: Thu Mar 08, 2012 8:56 am
Contact:

Re: Going Rogue Advice

Post by cocojoe »

Thanks again slembcke
Post Reply

Who is online

Users browsing this forum: No registered users and 31 guests