[Newton 2.0] A very weird thing... I really missed something

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

[Newton 2.0] A very weird thing... I really missed something

Postby Eltran » Sun Dec 21, 2008 1:37 pm

Hello,
I'm trying to integrate Newton into my project and there's something I seriously don't understand.
I've followed a lot of tutorials about setting the engine up, I make a floor, my cube moves down to the floor, but it doesn't collide, doesn't interact with the floor, pass through it and continues it's way to the infinity...

Here's my floor code :

Code: Select all
void G_NewtonFloor() {
   dVector floorSize (100.0f, 2.0f, 100.0f);
   dMatrix location (GetIdentityMatrix());

   location.m_posit.m_x = 0.1f;
   location.m_posit.m_y = -5.0f;
   location.m_posit.m_z = 0.1f;

   // create a box for floor
   floorCollision = NewtonCreateBox (g_NewtonWorld, floorSize.m_x, floorSize.m_y, floorSize.m_z, NULL);

   // create the the floor collision, and body with default values
   floorBody = NewtonCreateBody (g_NewtonWorld, floorCollision);
   NewtonReleaseCollision (g_NewtonWorld, floorCollision);

   NewtonBodySetMaterialGroupID (floorBody, NewtonMaterialGetDefaultGroupID (g_NewtonWorld));

   // set the transformation for this rigid body
   NewtonBodySetMatrix (floorBody, &location[0][0]);

   // set the newton world size based on the bsp size
   float boxP0[3];
   float boxP1[3];
   float matrix[4][4];
   NewtonBodyGetMatrix (floorBody, &matrix[0][0]);
   NewtonCollisionCalculateAABB (floorCollision, &matrix[0][0],  &boxP0[0], &boxP1[0]);
   // you can pad the box here if you wish
   //boxP0.y -= somevalue;
   //boxP1.y += somevaluef;
   NewtonSetWorldSize (g_NewtonWorld, (float*)boxP0, (float*)boxP1);
}


Here's my cube code :

Code: Select all
extern "C" void G_PhysicsAddEntity(gentity_t *ent) {
   dFloat Ixx;
   dFloat Iyy;
   dFloat Izz;
   NewtonBody* rigidBody;
   NewtonCollision* collision;
   dVector origin;
   dVector inertia;
   dFloat mass, volume;
   dMatrix matrix;
   float objSizeX, objSizeY, objSizeZ;

   matrix.m_posit.m_x = ent->s.origin[0];
   matrix.m_posit.m_y = ent->s.origin[1];
   matrix.m_posit.m_z = ent->s.origin[2];
   
   objSizeX = 10.0f;//(ent->r.mins[0] + ent->r.maxs[0])/2;
   objSizeY = 10.0f;//(ent->r.mins[1] + ent->r.maxs[1])/2;
   objSizeZ = 10.0f;//(ent->r.mins[2] + ent->r.maxs[2])/2;

   collision = NewtonCreateBox (g_NewtonWorld, objSizeX, objSizeY, objSizeZ, NULL);
   volume = 0.5f * NewtonConvexCollisionCalculateVolume (collision);

   // calculate the moment of inertia and the relative center of mass of the solid
   NewtonConvexCollisionCalculateInertialMatrix (collision, &inertia[0], &origin[0]);
   mass = 10;
   Ixx = mass * inertia[0];
   Iyy = mass * inertia[1];
   Izz = mass * inertia[2];

   //create the rigid body
   rigidBody = NewtonCreateBody (g_NewtonWorld, collision);

   // release the collision geometry when not need it
   NewtonReleaseCollision (g_NewtonWorld, collision);

   // set the correct center of gravity for this body
   NewtonBodySetCentreOfMass (rigidBody, &origin[0]);

   // set the mass matrix
   NewtonBodySetMassMatrix (rigidBody, mass, Ixx, Iyy, Izz);

   // activate
   //   NewtonBodyCoriolisForcesMode (blockBoxBody, 1);

   // save the pointer to the graphic object with the body.
   NewtonBodySetUserData (rigidBody, ent);

   // assign the wood id
   NewtonBodySetMaterialGroupID (rigidBody, NewtonMaterialGetDefaultGroupID (g_NewtonWorld));

//  set continue collision mode
   NewtonBodySetContinuousCollisionMode (rigidBody, 1);

   // set a destructor for this rigid body
   NewtonBodySetDestructorCallback (rigidBody, PhysicsBodyDestructor);

   // set the transform call back function
   NewtonBodySetTransformCallback (rigidBody, PhysicsSetTransform);

   // set the force and torque call back function
   NewtonBodySetForceAndTorqueCallback (rigidBody, PhysicsApplyGravityForce);

   // set the matrix for both the rigid body and the graphic body
   NewtonBodySetMatrix (rigidBody, &matrix[0][0]);
   //NewtonBodySetFreezeTreshold(rigidBody, 1.0, 1.0, 1.0, 1.0);
   PhysicsSetTransform (rigidBody, &matrix[0][0], 0);
}


Here's my transform callback :

Code: Select all
// set the transformation of a rigid body
void PhysicsSetTransform (const NewtonBody* body, const dFloat* matrix, int threadIndex)
{
   gentity_t *ent;
   dMatrix matr, matf;
   ent = (gentity_t*)NewtonBodyGetUserData(body);
   NewtonBodyGetMatrix(body, &matr[0][0]);
   VectorSet(ent->s.origin, matr.m_posit.m_x, matr.m_posit.m_y, matr.m_posit.m_z);
   VectorCopy(ent->s.origin, ent->r.currentOrigin);
   VectorCopy(ent->s.origin, ent->s.pos.trBase);
   G_Printf("ENT %s : X = %f Y = %f Z = %f\n", ent->targetname, matr.m_posit.m_x, matr.m_posit.m_y, matr.m_posit.m_z);
}


Here's my applyForceAndTorqueCallback :

Code: Select all
void  PhysicsApplyGravityForce (const NewtonBody* body, dFloat timestep, int threadIndex)
{
   dFloat Ixx;
   dFloat Iyy;
   dFloat Izz;
   dFloat mass;
   dMatrix matr;

   NewtonBodyGetMassMatrix (body, &mass, &Ixx, &Iyy, &Izz);
   dVector force (0.0f, 0.0f, (mass * -9.8f));
   NewtonBodySetForce (body, &force.m_x);
}


I seriously must have missed something or anything must be wrong, could you show me where's the error please ?

And how can I calculate a cube size from it's bounding boxes please ?
I have 6 coordinates for the size : mins.x mins.y mins.z ; maxs.x maxs.y maxs.z

Thanks a lot.
Eltran
 
Posts: 16
Joined: Sun Nov 18, 2007 5:32 am

Re: [Newton 2.0] A very weird thing... I really missed something

Postby JernejL » Sun Dec 21, 2008 3:10 pm

Do you call newtonupdate to actually advance the simulation?
Help improving the Newton Game Dynamics WIKI
User avatar
JernejL
 
Posts: 1531
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia

Re: [Newton 2.0] A very weird thing... I really missed something

Postby Eltran » Sun Dec 21, 2008 3:12 pm

Yes, I do like "NewtonUpdate(g_NewtonWorld, 0.01f);" at each frame.
The cube is moving, but not colliding...
Eltran
 
Posts: 16
Joined: Sun Nov 18, 2007 5:32 am

Re: [Newton 2.0] A very weird thing... I really missed something

Postby Eltran » Sun Dec 21, 2008 5:34 pm

Hmm got some news, when I set the size to 10, 10, 10, it does like this :

Image

Any idea ?
Eltran
 
Posts: 16
Joined: Sun Nov 18, 2007 5:32 am

Re: [Newton 2.0] A very weird thing... I really missed something

Postby Julio Jerez » Sun Dec 21, 2008 6:26 pm

you floor box isdefined alone the vertical direction (y)
dVector floorSize (100.0f, 2.0f, 100.0f);
but you gtavity point alon teh z direction, therefore teh box move horizontal, you can see thateth values chane alon teh z dirstion.

cheng yuor force call back to apply a gravity liek

g (0, -9.8 * mass, 0) and that should work
Julio Jerez
Moderator
Moderator
 
Posts: 11155
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: [Newton 2.0] A very weird thing... I really missed something

Postby Eltran » Sun Dec 21, 2008 6:35 pm

I already tried that, thanks for your reply.

For the Z or Y axis confusion, please don't worry about that, it's just the Quake 3 engine with inverted axis, the Z for the Y and the Y for the Z...

Can you try that code please ?
I just made a sample in a console, to see that simulation, very weird because it pass through the floor too...
(Everything you just said have been applied, and it does the same effect ! Are you sure everything is allright ? I'm sure that I missed something...)

Code: Select all
#define _NEWTON_USE_LIB
#include <stdio.h>
#include <Newton.h>
#include "minimalMath/dMatrix.h"

NewtonWorld *n_World;
NewtonBody *n_Body;
NewtonCollision *n_Collision;

NewtonBody *n_BoxBody;
NewtonCollision *n_BoxCollision;

void PhysicsSetTransform (const NewtonBody* body, const dFloat* matrix, int threadIndex) {
   dMatrix mat;

   NewtonBodyGetMatrix(body, &mat[0][0]);
   printf("Box Update, X = %f, Y = %f, Z = %f.\n", mat.m_posit.m_x, mat.m_posit.m_y, mat.m_posit.m_z);
}

void  PhysicsApplyGravityForce (const NewtonBody* body, dFloat timestep, int threadIndex)
{
   dFloat Ixx;
   dFloat Iyy;
   dFloat Izz;
   dFloat mass;
   dMatrix matr;

   NewtonBodyGetMassMatrix (body, &mass, &Ixx, &Iyy, &Izz);
   dVector force (0.0f, mass * -9.8f, 0.0f);
   NewtonBodyAddForce (body, &force.m_x);
}

void nS_Init() {
   dVector origin;
   dVector inertia;
   dMatrix matrix;
   int mass;
   float Ixx, Iyy, Izz;

   n_World = NewtonCreate(NULL, NULL);

   n_Collision = NewtonCreateBox(n_World, 100.0f, 2.0f, 100.0f, NULL);
   n_Body = NewtonCreateBody(n_World, n_Collision);
   NewtonReleaseCollision(n_World, n_Collision);

   n_BoxCollision = NewtonCreateBox(n_World, 10.0f, 10.0f, 10.0f, NULL);
   NewtonConvexCollisionCalculateInertialMatrix (n_BoxCollision, &inertia[0], &origin[0]);

   matrix.m_posit.m_x = 10.0f;
   matrix.m_posit.m_y = 30.0f;
   matrix.m_posit.m_z = 10.0f;

   mass = 10;
   Ixx = mass * inertia[0];
   Iyy = mass * inertia[1];
   Izz = mass * inertia[2];

   n_BoxBody = NewtonCreateBody(n_World, n_BoxCollision);
   NewtonReleaseCollision(n_World, n_BoxCollision);

   NewtonBodySetCentreOfMass (n_BoxBody, &origin[0]);
   NewtonBodySetMassMatrix (n_BoxBody, mass, Ixx, Iyy, Izz);
   NewtonBodySetTransformCallback (n_BoxBody, PhysicsSetTransform);
   NewtonBodySetForceAndTorqueCallback (n_BoxBody, PhysicsApplyGravityForce);
   NewtonBodySetMatrix (n_BoxBody, &matrix[0][0]);
}

void nS_Update() {
   NewtonUpdate(n_World, 0.001f);
}

int main() {
   nS_Init();

   while (1) {
      nS_Update();
   }

   return 1;
}


Thanks.
Eltran
 
Posts: 16
Joined: Sun Nov 18, 2007 5:32 am

Re: [Newton 2.0] A very weird thing... I really missed something

Postby Julio Jerez » Sun Dec 21, 2008 6:49 pm

I looked at the code and it seems right, but there is a bug in this code

Code: Select all
void nS_Init() {
   dVector origin;
   dVector inertia;
   dMatrix matrix;
   int mass;
   float Ixx, Iyy, Izz;

   n_World = NewtonCreate(NULL, NULL);

   n_Collision = NewtonCreateBox(n_World, 100.0f, 2.0f, 100.0f, NULL);
   n_Body = NewtonCreateBody(n_World, n_Collision);
   NewtonReleaseCollision(n_World, n_Collision);

   n_BoxCollision = NewtonCreateBox(n_World, 10.0f, 10.0f, 10.0f, NULL);
   NewtonConvexCollisionCalculateInertialMatrix (n_BoxCollision, &inertia[0], &origin[0]);

   matrix.m_posit.m_x = 10.0f;
   matrix.m_posit.m_y = 30.0f;
   matrix.m_posit.m_z = 10.0f;



Matrix is no initilized, please try this before I test it.

dMatrix matrix (dGetIdentityMatrix());
Julio Jerez
Moderator
Moderator
 
Posts: 11155
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: [Newton 2.0] A very weird thing... I really missed something

Postby Eltran » Sun Dec 21, 2008 6:56 pm

OOOOOOOOOMMGod.

It worked on the console, I think I know what do I have to do for the rest.

What a stupid kind of error eh, thanks so much, and sorry for the disturb.
Eltran
 
Posts: 16
Joined: Sun Nov 18, 2007 5:32 am


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 2 guests