Trouble with Forces [solved]

Official forum for the Chipmunk2D Physics Library.
Post Reply
mambazo
Posts: 3
Joined: Mon May 12, 2008 10:30 am
Contact:

Trouble with Forces [solved]

Post by mambazo »

I'm having a spot of trouble with forces.

It's a top down space ship, in zero gravity, and I'm applying forces to allow the ship to move and turn.

Each time a force is applied, it seems to be stronger than the last, causing the ship to become uncontrollable very quickly.

I'm resetting the forces after each step.

My forces for the UP arrow and DOWN arrow are as follows...

UP

Code: Select all

cpVect	   j = Ship.Body->rot
cpVect	   r = cpvzero
cpBodyApplyImpulse( Ship.Body, j, r )
DOWN

Code: Select all

cpVect	   j = -Ship.Body->rot
cpVect	   r = cpvzero
cpBodyApplyImpulse( Ship.Body, j, r )
And the LEFT and RIGHT arrows...

Note: I've added in some operands like + - * so I no longer need to use the cpvmult,add,sub functions etc

LEFT

Code: Select all

      cpVect	   j = cpvperp( -Ship.Body->rot ) * 0.1
      cpVect	   r = cpvrotate( cpv( 10.0, 0.0 ), Ship.Body->rot )
      cpBodyApplyImpulse( Ship.Body, j, r )
      
      j = cpvperp( Ship.Body->rot ) * 0.1
      r = cpvrotate( cpv( -10.0, 0.0 ), Ship.Body->rot )
      cpBodyApplyImpulse( Ship.Body, j, r )        

RIGHT

Code: Select all

      cpVect	   j = cpvperp( Ship.Body->rot ) * 0.1
      cpVect	   r = cpvrotate( cpv( 10.0, 0.0 ), Ship.Body->rot )
      cpBodyApplyImpulse( Ship.Body, j, r )        

      j = cpvperp( -Ship.Body->rot ) * 0.1
      r = cpvrotate( cpv( -10.0, 0.0 ), Ship.Body->rot )
      cpBodyApplyImpulse( Ship.Body, j, r )  

If I press LEFT or RIGHT just once, it starts rotating, but continues to increase the rotation speed even after reseting the forces.

Any ideas?
Last edited by mambazo on Mon May 12, 2008 12:58 pm, edited 1 time in total.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Trouble with Forces

Post by slembcke »

You want to be using the force function instead of the impulse function. Impulses are very large, nearly instantaneous forces (like a cannon firing or a collision). It doesn't actually use forces at all, and simply modifies the velocity directly. Try substituting in cpBodyApplyForce() and see if that helps.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
mambazo
Posts: 3
Joined: Mon May 12, 2008 10:30 am
Contact:

Re: Trouble with Forces

Post by mambazo »

Using ApplyForce seems to do the same, just much faster :)

It feels as though even though the force is applied only once, the simulation doesn't reset it to zero when I call cpBodyResetForces. However, when reading the values straight from the Body struct they say that the force vector is indeed 0.

Tis odd!

Update!:-

time

When I apply the force early in the simulation, it is a small force, but, if i wait for a time, and THEN apply the force, the magnitude is much large.

Seems to be proportional to the time I have waited!
mambazo
Posts: 3
Joined: Mon May 12, 2008 10:30 am
Contact:

Re: Trouble with Forces

Post by mambazo »

Solved!

The problem was in my update function. My timestep was not constant. *slaps self*

Thanks for the help :)
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests