A place to discuss everything related to Newton Dynamics.
Moderators: Sascha Willems, walaber
by roy844 » Wed May 05, 2010 10:14 am
hi,
i have been trying to implement the physics engine and it compiles well. but i am expecting it to fall right now because i added 1 rigidbody no floor or anything, just 1 box that is suposed to be affected by gravity. but it does not.
what i dont understand is how the model is affected by newton, is the model affected because newton affects the matrix of the model ?
- Code: Select all
dVector minBox;
dVector maxBox;
//NewtonCollisionCalculateAABB (shape, &matrix[0][0], &minBox[0], &maxBox[0]);
// add some extra padding
minBox.m_x -= 5000.0f;
minBox.m_y -= 50000.0f;
minBox.m_z -= 5000.0f;
maxBox.m_x += 5000.0f;
maxBox.m_y += 50000.0f;
maxBox.m_z += 5000.0f;
// set the new world size
NewtonSetWorldSize (g_world, &minBox[0], &maxBox[0]);
NewtonCollision* shape;
NewtonBody* box;
shape = CreateNewtonBox (g_world, &kubus , 0);
box = CreateRigidBody (g_world, &kubus, shape, 10.0f);
NewtonReleaseCollision (g_world, shape);
dMatrix matrix (kubus.m_curRotation, kubus.m_curPosition);
NewtonBodySetMatrix (box, &matrix[0][0]);
-
roy844
-
- Posts: 13
- Joined: Mon Mar 03, 2008 3:36 am
by JernejL » Wed May 05, 2010 10:44 am
You need to call newtonupdate and apply the gravity in forcetorque callback.
-

JernejL
-
- Posts: 1587
- Joined: Mon Dec 06, 2004 2:00 pm
- Location: Slovenia
-
by roy844 » Wed May 05, 2010 10:59 am
thanks for your reply, i use the AdvanceSimulation function from the tutorial and in that function is this line:
- Code: Select all
// run the newton update function
NewtonUpdate (g_world, (1.0f / DEMO_PHYSICS_FPS));
and the CreateRigidBody function this function is called also from the tutorial files:
- Code: Select all
// callback to apply external forces to body
void ApplyForceAndTorqueCallback (const NewtonBody* body, dFloat timestep, int threadIndex)
{
dFloat Ixx;
dFloat Iyy;
dFloat Izz;
dFloat mass;
// for this tutorial the only external force in the Gravity
NewtonBodyGetMassMatrix (body, &mass, &Ixx, &Iyy, &Izz);
dVector gravityForce (0.0f, mass * GRAVITY, 0.0f, 1.0f);
NewtonBodySetForce(body, &gravityForce[0]);
}
-
roy844
-
- Posts: 13
- Joined: Mon Mar 03, 2008 3:36 am
by Julio Jerez » Wed May 05, 2010 11:26 am
in this code
- Code: Select all
shape = CreateNewtonBox (g_world, &kubus , 0);
box = CreateRigidBody (g_world, &kubus, shape, 10.0f);
NewtonReleaseCollision (g_world, shape);
dMatrix matrix (kubus.m_curRotation, kubus.m_curPosition);
NewtonBodySetMatrix (box, &matrix[0][0]);
you did not set the box mass, NewtonBodySetMassMatrix
-
Julio Jerez
- Moderator

-
- Posts: 12426
- Joined: Sun Sep 14, 2003 2:18 pm
- Location: Los Angeles
-
by roy844 » Wed May 05, 2010 11:46 am
i use the following function on the bottom from the tutorial project.
this also includes the function below
NewtonBodySetMassMatrix (body, mass, mass * inertia.m_x, mass * inertia.m_y, mass * inertia.m_z);so the mass of the box is 10
box = CreateRigidBody (g_world, &kubus, shape, 10.0f);
- Code: Select all
NewtonBody* CreateRigidBody (NewtonWorld* world, Entity* ent, NewtonCollision* collision, dFloat mass)
{
dVector minBox;
dVector maxBox;
dVector origin;
dVector inertia;
NewtonBody* body;
// Now with the collision Shape we can crate a rigid body
body = NewtonCreateBody (world, collision);
// bodies can have a destructor.
// this is a function callback that can be used to destroy any local data stored
// and that need to be destroyed before the body is destroyed.
NewtonBodySetDestructorCallback (body, DestroyBodyCallback);
// save the entity as the user data for this body
NewtonBodySetUserData (body, ent);
// we need to set physics properties to this body
dMatrix matrix (ent->m_curRotation, ent->m_curPosition);
NewtonBodySetMatrix (body, &matrix[0][0]);
// we need to set the proper center of mass and inertia matrix for this body
// the inertia matrix calculated by this function does not include the mass.
// therefore it needs to be multiplied by the mass of the body before it is used.
NewtonConvexCollisionCalculateInertialMatrix (collision, &inertia[0], &origin[0]);
// set the body mass matrix
NewtonBodySetMassMatrix (body, mass, mass * inertia.m_x, mass * inertia.m_y, mass * inertia.m_z);
// set the body origin
NewtonBodySetCentreOfMass (body, &origin[0]);
// set the function callback to apply the external forces and torque to the body
// the most common force is Gravity
NewtonBodySetForceAndTorqueCallback (body, ApplyForceAndTorqueCallback);
// set the function callback to set the transformation state of the graphic entity associated with this body
// each time the body change position and orientation in the physics world
NewtonBodySetTransformCallback (body, SetTransformCallback);
return body;
}
-
roy844
-
- Posts: 13
- Joined: Mon Mar 03, 2008 3:36 am
by Julio Jerez » Wed May 05, 2010 12:01 pm
it should work then.
put a break point on the transform call back and see if the position is changing
-
Julio Jerez
- Moderator

-
- Posts: 12426
- Joined: Sun Sep 14, 2003 2:18 pm
- Location: Los Angeles
-
by roy844 » Wed May 05, 2010 12:14 pm
reading this line i asume that if transformation of the object has changed it will call the code below
NewtonBodySetTransformCallback (body, SetTransformCallback);but after putting a breakpoint in the code below i found that it is never called
a function that is called every loop is
ApplyForceAndTorqueCallback. i hope someone can help me understand what i am doing wrong.
- Code: Select all
// Transform callback to set the matrix of a the visual entity
void SetTransformCallback (const NewtonBody* body, const dFloat* matrix, int threadIndex)
{
c_Object* ent;
// Get the position from the matrix
dVector posit (matrix[12], matrix[13], matrix[14], 1.0f);
dQuaternion rotation;
// we will ignore the Rotation part of matrix and use the quaternion rotation stored in the body
NewtonBodyGetRotation(body, &rotation.m_q0);
// get the entity associated with this rigid body
ent = (c_Object*) NewtonBodyGetUserData(body);
// since this tutorial run the physics and a different fps than the Graphics
// we need to save the entity current transformation state before updating the new state.
ent->m_prevPosition = ent->m_curPosition;
ent->m_prevRotation = ent->m_curRotation;
if (ent->m_curRotation.DotProduct (rotation) < 0.0f) {
ent->m_prevRotation.Scale(-1.0f);
}
// set the new position and orientation for this entity
ent->m_curPosition = posit;
ent->m_curRotation = rotation;
}
-
roy844
-
- Posts: 13
- Joined: Mon Mar 03, 2008 3:36 am
by Julio Jerez » Wed May 05, 2010 1:25 pm
SetTransformCallback call back most be called each frame by the engine. there must be and error some where that is no setting teh call back correctly.
check the demo.
-
Julio Jerez
- Moderator

-
- Posts: 12426
- Joined: Sun Sep 14, 2003 2:18 pm
- Location: Los Angeles
-
Return to General Discussion
Who is online
Users browsing this forum: No registered users and 2 guests