Player body

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Player body

Postby pRoxxxD » Mon Jul 22, 2013 7:40 am

Hello everyone. I had a small problem with Newton. Am using newton 2.36. My engine uses Quake 1-2 tehnology, so i load bsp maps and use Quake style physics, but i want use Newton physics too. So i load level by NewtonBegin/EndBuildCollisionTree, make to dynamic entitys Newton Bodys, but player body. =\ Each frame am teleporting player body to his origin:
Code: Select all

   float mat[16] =
   {
      1,0,0,0,
      0,1,0,0,
      0,0,1,0,
      player->pev->origin.x,player->pev->origin.y,player->pev->origin.z,1
   };

   NewtonBodySetMatrix(player->body,&mat[0]);

So the other body cant pass trought player. I tried set to player body NewtonBodySetContinuousCollisionMode(body, 1); , its better but still dont work fine. So what i need that player body collide with other objects, and i teleport it each frame, cuz am using Quake style physics for it.

P.S. Sorry for my bad english.
pRoxxxD
 
Posts: 4
Joined: Sat Jul 20, 2013 3:20 am

Re: Player body

Postby JoeJ » Mon Jul 22, 2013 8:12 am

Make the newton body follow the quake player instead of teleporting.
Something like:

vector diff = quakePos - newtonPos;
vector velocity = diff / timestep;
NewtonBodySetVelocity (body, velocity); // see newton.h for correct function name.

That should be ok for your purpose, but if there rmain problematic cases,
you can apply a force instead velocity inside force tourque callback.
That's a little more difficult but better because newton can calculate velocity itself...

Edit:
Onother way would be using the Kinematic Joint from the joint lib.
There you can set a target matrix, and newton make the following automatically for you.
User avatar
JoeJ
 
Posts: 1489
Joined: Tue Dec 21, 2010 6:18 pm

Re: Player body

Postby Julio Jerez » Mon Jul 22, 2013 8:55 am

migrate to newton 3.10 it come with a much better player controller.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Player body

Postby pRoxxxD » Mon Jul 22, 2013 9:11 am

Thank u ill try this, but how i can disable player body rotation?
Code: Select all
CVector3 damp(1,1,1);
NewtonBodySetAngularDamping(body, &damp.x);

cuz with this code body still rotating.
pRoxxxD
 
Posts: 4
Joined: Sat Jul 20, 2013 3:20 am

Re: Player body

Postby JoeJ » Mon Jul 22, 2013 10:10 am

how i can disable player body rotation?


velocity method:
NewtonBodySetOmega (body, &zerovector); // omega = angular velocity

force / torque method: calculate torque to make angular velocity zero.

joint method: controlling orientation at will.


The newton player controller julio mentioned might be good too even if quake already have its own.
I guess the velocity meothod is easiest to do and gives quick results,
but if bad things happen (player pushes some small object into a wall etc.) you might wanna try other methods.
Update to 3.xx soon - saves some unecessary work on doing it later :)
User avatar
JoeJ
 
Posts: 1489
Joined: Tue Dec 21, 2010 6:18 pm

Re: Player body

Postby pRoxxxD » Mon Jul 22, 2013 12:18 pm

Ty JoeJ. I tried velocity method, it was bad thing. Player pushed dynamic body even if their mass is over 9k. =( I tried Force method it was better, but speed is so slow:
Code: Select all
   CVector3 velocity;
   velocity = ed->origin - newtonorigin;
   velocity = velocity / timestep;
   ed->origin = newtonorigin;

   CVector3 zero( 0.0f, 0.0f, 0.0f );
   NewtonBodySetForce(body, &velocity .x);
   NewtonBodySetOmega(body, &zero.x);
   NewtonBodySetTorque(body, &zero.x);
pRoxxxD
 
Posts: 4
Joined: Sat Jul 20, 2013 3:20 am

Re: Player body

Postby Julio Jerez » Mon Jul 22, 2013 1:13 pm

I am telling you, you will save lot of agravation by moving to newton 310.

just download the package and play the basic player controller see fi that is what you want.
you can thow bodies in the scene by hitting teh enter key.

if this is what you want, or even if it si cloase to what you wnat and you nee some twiake that is a lot easier that tryin to make a playe controller with a
physics library. That has proven over and over again to be a very diffcult task. even for me when I have to do it for oteh physics library.

I put lot of effort in making the play controller, a game friendly tool.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Player body

Postby JoeJ » Mon Jul 22, 2013 1:18 pm

Player pushed dynamic body even if their mass is over 9k. =(


Oh sorry, i was thinking there should be no feedback from dynamic objects and player should always push them aside.
... then velocity method is no option.
I recommend trying the 3.xx player controller as Julio said, which is build for things like that.

However to do the force method right you'd need to use the force / torque callback which should then look like that:

Code: Select all
void ForceAndTorqueCallback (const NewtonBody* body, dFloat timestep, int threadIndex) // newton calls this for every body
{
   dFloat Ixx;
   dFloat Iyy;
   dFloat Izz;
   dFloat mass;

NewtonBodyGetMassMatrix (body, &mass, &Ixx, &Iyy, &Izz);
   
   BodyData *data = (BodyData*) BodyGetUserData ((Body*)body); // BodyData is my user data class, which i access by user data pointer

   sVec3 force (0.0f, mass * -9.81, 0.0f); // add gravity to any body
   if (IsPlayer(data))
   {
      vector targetVel = ... calculate velocity as before...
      vector currentVel; NewtonBodyGetVelocity (body, currentVelocity);
      vector playerForce ((targetVel - currentVel) * (mass / timestep)); // this is force that then should lead to our target velocity
// playerForce.SetMaxLength(35); // clamp somehow, so that you can't move heavy objects
force += playerForce; // or force = playerForce, if quake should calculate gravity
}

NewtonBodyAddForce (body, &force[0]);
}


For the KinematicJoint method you can set some max linear friction, to limit player strength.

But as said, better try the newton player controller.
User avatar
JoeJ
 
Posts: 1489
Joined: Tue Dec 21, 2010 6:18 pm

Re: Player body

Postby pRoxxxD » Tue Jul 23, 2013 11:26 am

Hello again, force method working fine but whem other bodys hits player body, player body still rotating. Im used NewtonSetOmega(body, zerovec) everywhere, each frame, in ForceAndTorqueCallback and in TransformCallback, but it still rotating. =(
pRoxxxD
 
Posts: 4
Joined: Sat Jul 20, 2013 3:20 am

Re: Player body

Postby JoeJ » Tue Jul 23, 2013 1:58 pm

Yep, that's the hardest part.
The math is similar as with linear stuff, but in contrast to mass inertia is orientation dependent, so it becomes more complex.
You need to measure current angular velocity and generate a torque which does the opposite,
that's why simply setting zero torque does not work.

Code: Select all

// code also in force torque callback..

vector curAngVel = NewtonBodyGetOmega()
vector targetAngVel = curAngVel * -1.0; // we want to remove any angular velocity

vector torque = targetAngVel / timestep;

// apply inertia...

//matrix = BodyGetMatrix
torque = matrix.Unrotate (torque); // go local space
torque[0] *= Ixx; // Inertia from BodyGetMassMatrix
torque[1] *= Iyy;
torque[2] *= Izz;
torque = matrix.Rotate (torque); // go back global

torque *= 0.3; // something between 0.0 and 1.0; recommended <0.5, high value could generate overspin
NewtonBodyAddTorque (torque)
User avatar
JoeJ
 
Posts: 1489
Joined: Tue Dec 21, 2010 6:18 pm


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 0 guests