non physical behaviour of rotation

MrX
Posts: 4
Joined: Fri Jul 02, 2010 9:50 am

non physical behaviour of rotation

Post by MrX »

Hello

I'm completly new to Bullet so I'm sure I must do sth wrong.

Here is a short description of what I did:
- 2 objects in the scene: box and plane, gravity and friction are set
- I cast the ray against the box and then link the intersection point on the ray with intersection point on the box with spring
- After I move the ray the distance between attachment points on the ray and on the box can be described by a vector, say, d.
In effect a force (impulse) spring_const*d acts upon the attachment point on the box (of course I have the reaction force on
the ray as well)

The problem:
- When my box loses contact with the ground (plane) it starts to rotate like crazy. Increasing angular damping doesn't help much and as I understand 1 is a maximal value anyway.
robagar
Posts: 48
Joined: Fri May 21, 2010 1:49 am

Re: non physical behaviour of rotation

Post by robagar »

Can you post your code for calculating & applying the force?
MrX
Posts: 4
Joined: Fri Jul 02, 2010 9:50 am

Re: non physical behaviour of rotation

Post by MrX »

thx for reply

the code for calculating and applying the force is:

Code: Select all

// I'm applying force to btRigidBody *d_object  at local point d_localAnchor
		btQuaternion orient = d_object->getOrientation();
		btVector3 distAnchor = d_localAnchor.rotate(orient.getAxis(),orient.getAngle());
// distAnchor is d_localAnchor in the world coordinates
		distAnchor += d_object->getCenterOfMassPosition();

		btVector3 force = d_rayToWorld - d_rayFromWorld;
		force.normalize();
		force *= d_distance;
		force += d_rayFromWorld; 
// just above variable "force" is equal to a point on the ray (in the world coords) to which d_object is attached

		force -= distAnchor; // now force is a vector joining the ray and d_object
		force *= d_spring_const; // calculation of the actual force

		
		d_object->activate(true);
		d_object->applyImpulse(force,d_localAnchor); // application of the force
I can also make playable version of the relevant bits of my program and either post it or place it somewhere for download

Thanks
MrX
robagar
Posts: 48
Joined: Fri May 21, 2010 1:49 am

Re: non physical behaviour of rotation

Post by robagar »

OK, first of all you want applyForce not applyImpulse since you're calculating a force, not a change in momentum. But that's not the problem. Basically, both parameters for applyForce (and applyImpulse) need to be in world coordinates, not object local as you might expect (I know I did).
MrX
Posts: 4
Joined: Fri Jul 02, 2010 9:50 am

Re: non physical behaviour of rotation

Post by MrX »

I used the second parameter of applyForce (Impulse) in world coords and I have the same problem again:
the box starts to spin like crazy

Thank you for reply
MrX
robagar
Posts: 48
Joined: Fri May 21, 2010 1:49 am

Re: non physical behaviour of rotation

Post by robagar »

I'm not sure I really understand what you're trying to do. If the ray is fixed in space the result is bound to be mad spinning, since every update the cube is spun up a bit more with nothing to counteract it.
MrX
Posts: 4
Joined: Fri Jul 02, 2010 9:50 am

Re: non physical behaviour of rotation

Post by MrX »

If you hang a box in space with gravity on, it should swing and when angular damping is on, the swinging should decay in time. In my demo it just spins like crazy and neither gravity nor damping seem to have any effect on it.

I'm trying to port it from PhysX because I want it to work out-of-the-box without necessity of installing any third party stuff.
I thought it should be easy task but unfortunatelly it's not :(
robagar
Posts: 48
Joined: Fri May 21, 2010 1:49 am

Re: non physical behaviour of rotation

Post by robagar »

My guess would be your force calculation is screwy - it certainly looks more complicated than it needs to be, if the box is hanging from a fixed point.

Say
a = anchor point on box (in world space coords)
b = fixed attachment point
d = spring constant

then assuming your spring's rest length is zero and the force is proportional to the spring length,

f = the force in world space coords = d * (b - a)

hth
robagar
Posts: 48
Joined: Fri May 21, 2010 1:49 am

Re: non physical behaviour of rotation

Post by robagar »

another thing you could try is an alternative way of getting the anchor position in world space, like this

Code: Select all

btVector3 anchorPos = (d_object->getWorldTransform().getBasis() * d_localAnchor) + d_object->getCenterOfMassPosition();
.. although the way you're explicitly applying the orientation rotation should work, AFAICS
robagar
Posts: 48
Joined: Fri May 21, 2010 1:49 am

Re: non physical behaviour of rotation

Post by robagar »

or even just

Code: Select all

btVector3 anchorPos = d_object->getWorldTransform() * d_localAnchor
sorry, I was thinking in Ogre terms, which keeps the orientation & position separately :oops: