A place to discuss everything related to Newton Dynamics.
Moderators: Sascha Willems, walaber
by Ripiz » Mon Oct 05, 2009 6:11 am
I'm trying to implement Newton's to my project, however I'm having some issues. I'm not sure if I have done everything right. For some reason shape remains illegal pointer after calling NewtonCreateConvexHull(), and then it throws errors in CreateRigidBody() as it pass illegal pointer. Could anyone help me please?
- Code: Select all
//top of main.cpp
#include "physics/Newton.h"
#include "physics/JointLibrary.h"
static NewtonWorld* g_world;
//WinMain
g_world = NewtonCreate (AllocMemory, FreeMemory);
NewtonSetPlatformArchitecture (g_world, 0);
Vector minSize (-1000.0f, -1000.0f, -1000.0f);
Vector maxSize ( 1000.0f, 1000.0f, 1000.0f);
NewtonSetWorldSize (g_world, &minSize[0], &maxSize[0]);
NewtonSetSolverModel (g_world, 1);
NewtonCollision* shape;
test=new Model(p_Device,"Jin.x",Vector(0,0,0),Vector(0,0,0),Vector(1,1,1));
test->Load();
float f=test->Mesh->GetNumVertices()*3.0f*sizeof(float);
shape = NewtonCreateConvexHull(g_world, test->Mesh->GetNumVertices(),&f, 3 * sizeof (float), 0.1f, 0, NULL);
CreateRigidBody (g_world, test, shape, 10.0f);
NewtonReleaseCollision (g_world, shape);
//other stuff
void* AllocMemory (int sizeInBytes){
return malloc (sizeInBytes);
}
void FreeMemory (void *ptr, int sizeInBytes){
free (ptr);
}
void DestroyBodyCallback (const NewtonBody* body){
}
void ApplyForceAndTorqueCallback (const NewtonBody* body, float timestep, int threadIndex){
float Ixx;
float Iyy;
float Izz;
float mass;
NewtonBodyGetMassMatrix (body, &mass, &Ixx, &Iyy, &Izz);
D3DXVECTOR4 gravityForce (0.0f, mass * -10.0f, 0.0f, 1.0f);
NewtonBodySetForce(body, &gravityForce[0]);
}
void SetTransformCallback (const NewtonBody* body, const float* matrix, int threadIndex){
Model* ent;
D3DXVECTOR4 posit (matrix[12], matrix[13], matrix[14], 1.0f);
NewtonBodyGetRotation(body, NULL);
ent = (Model*) NewtonBodyGetUserData(body);
}
NewtonBody* CreateRigidBody (NewtonWorld* world, Model* ent, NewtonCollision* collision, float mass){
Vector minBox;
Vector maxBox;
Vector origin;
Vector inertia;
NewtonBody* body;
body = NewtonCreateBody (world, collision);
NewtonBodySetDestructorCallback (body, DestroyBodyCallback);
NewtonConvexCollisionCalculateInertialMatrix (collision, &inertia[0], &origin[0]);
NewtonBodySetMassMatrix (body, mass, mass * inertia.x, mass * inertia.y, mass * inertia.z);
NewtonBodySetCentreOfMass (body, &origin[0]);
NewtonBodySetForceAndTorqueCallback (body, ApplyForceAndTorqueCallback);
NewtonBodySetTransformCallback (body, SetTransformCallback);
return body;
}
Edit: I'm using Newton 2.10 for Windows
-
Ripiz
-
- Posts: 47
- Joined: Sat Oct 03, 2009 12:07 pm
by Julio Jerez » Mon Oct 05, 2009 6:55 am
I do not think that in this
float f=test->Mesh->GetNumVertices()*3.0f*sizeof(float);
&f
f is no a pointer, you need to pass the pointer to the array of floats of teh mesh not just a pointe to one float.
-
Julio Jerez
- Moderator

-
- Posts: 12426
- Joined: Sun Sep 14, 2003 2:18 pm
- Location: Los Angeles
-
by Ripiz » Mon Oct 05, 2009 9:00 am
Array of float? And what to store there? Or empty one?
-
Ripiz
-
- Posts: 47
- Joined: Sat Oct 03, 2009 12:07 pm
by Julio Jerez » Mon Oct 05, 2009 9:30 am
there must be a funtion the get teh pointer to teh vertex array in your mesh.
you can not just past a printe to one float
when you do
float f = ...
&f
is just teh adress to one float on the stack.
-
Julio Jerez
- Moderator

-
- Posts: 12426
- Joined: Sun Sep 14, 2003 2:18 pm
- Location: Los Angeles
-
by JernejL » Mon Oct 05, 2009 9:30 am
Ripiz wrote:Array of float? And what to store there? Or empty one?
A cloud of points to build the convex hull.
-

JernejL
-
- Posts: 1587
- Joined: Mon Dec 06, 2004 2:00 pm
- Location: Slovenia
-
by Ripiz » Mon Oct 05, 2009 11:30 am
I'm getting error =/ Marked like where it happens. Any ideas?
- Code: Select all
#include "physics/Newton.h"
#include "physics/JointLibrary.h"
static NewtonWorld* g_world;
typedef D3DXVECTOR3 Vector;
typedef D3DXMATRIX Matrix;
struct D3DVERTEX {
D3DXVECTOR3 p;
D3DXVECTOR3 n;
FLOAT tu, tv;
static const DWORD FVF;
};
void* AllocMemory (int sizeInBytes){
return malloc (sizeInBytes);
}
void FreeMemory (void *ptr, int sizeInBytes){
free (ptr);
}
void DestroyBodyCallback (const NewtonBody* body){
}
void ApplyForceAndTorqueCallback (const NewtonBody* body, float timestep, int threadIndex){
float Ixx;
float Iyy;
float Izz;
float mass;
NewtonBodyGetMassMatrix (body, &mass, &Ixx, &Iyy, &Izz);
D3DXVECTOR4 gravityForce (0.0f, mass * -10.0f, 0.0f, 1.0f);
NewtonBodySetForce(body, &gravityForce[0]);
}
void SetTransformCallback (const NewtonBody* body, const float* matrix, int threadIndex){
Model* ent;
Vector posit (matrix[12], matrix[13], matrix[14]);
ent = (Model*) NewtonBodyGetUserData(body); //-------------------------------ERROR------------------------------------
ent->vecPos=posit;
}
NewtonBody* CreateRigidBody (NewtonWorld* world, Model* ent, NewtonCollision* collision, float mass){
Vector minBox;
Vector maxBox;
Vector origin;
Vector inertia;
NewtonBody* body;
body = NewtonCreateBody (world, collision);
NewtonBodySetDestructorCallback (body, DestroyBodyCallback);
NewtonConvexCollisionCalculateInertialMatrix (collision, &inertia[0], &origin[0]);
NewtonBodySetMassMatrix (body, mass, mass * inertia.x, mass * inertia.y, mass * inertia.z);
NewtonBodySetCentreOfMass (body, &origin[0]);
NewtonBodySetForceAndTorqueCallback (body, ApplyForceAndTorqueCallback);
NewtonBodySetTransformCallback (body, SetTransformCallback);
return body;
}
NewtonCollision* CreateNewtonConvex (NewtonWorld* world, Model *ent, int shapeId){
NewtonCollision* collision;
float* tmpArray = new float [ent->Mesh->GetNumVertices()*3];
LPDIRECT3DVERTEXBUFFER9 pVertices;
ent->Mesh->GetVertexBuffer( &pVertices );
D3DVERTEX *pVerticesW;
pVertices->Lock( 0, 0, ( void** )&pVerticesW, 0 );
for(unsigned int i=0;i<ent->Mesh->GetNumVertices();i++){
tmpArray[i*3+0]=pVerticesW[i].p.x;
tmpArray[i*3+1]=pVerticesW[i].p.y;
tmpArray[i*3+2]=pVerticesW[i].p.z;
}
collision = NewtonCreateConvexHull(world, ent->Mesh->GetNumVertices(), (float*)tmpArray, sizeof (Vector), 0.1f, shapeId, NULL);
delete tmpArray;
return collision;
}
//WinMain
g_world = NewtonCreate (AllocMemory, FreeMemory);
NewtonSetPlatformArchitecture (g_world, 0);
Vector minSize (-1000.0f, -1000.0f, -1000.0f);
Vector maxSize ( 1000.0f, 1000.0f, 1000.0f);
NewtonSetWorldSize (g_world, &minSize[0], &maxSize[0]);
NewtonSetSolverModel (g_world, 1);
NewtonCollision* shape;
test=new Model(p_Device,"Jin.x",Vector(0,5,10),Vector(0,0,0),Vector(1,1,1));
test->Load();
shape=CreateNewtonConvex (g_world, test, 0);
CreateRigidBody (g_world, test, shape, 10.0f);
NewtonReleaseCollision (g_world, shape);
// loop
NewtonUpdate (g_world,elapsedTime); (elapsedTime is float in seconds, so values low)
Thank you in advance
-
Ripiz
-
- Posts: 47
- Joined: Sat Oct 03, 2009 12:07 pm
by Ripiz » Mon Oct 05, 2009 12:46 pm
Found it.. Somehow this line disappeared, added NewtonBodySetUserData (body, ent); and error disappeared. However my model is falling to the right, if that can be called fall, not downwards, why?
Edit: Nevermind, found that one too, I wonder how to delete own post...
-
Ripiz
-
- Posts: 47
- Joined: Sat Oct 03, 2009 12:07 pm
Return to General Discussion
Who is online
Users browsing this forum: No registered users and 1 guest