Page 1 of 1

Trouble with Forces [solved]

Posted: Mon May 12, 2008 11:03 am
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?

Re: Trouble with Forces

Posted: Mon May 12, 2008 12:13 pm
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.

Re: Trouble with Forces

Posted: Mon May 12, 2008 12:27 pm
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!

Re: Trouble with Forces

Posted: Mon May 12, 2008 12:58 pm
by mambazo
Solved!

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

Thanks for the help :)