A place to discuss everything related to Newton Dynamics.
Moderators: Sascha Willems, walaber
by arkdemon » Sun Jan 19, 2014 12:33 pm
Hello everyone. I am new to using Newton Game Dynamics so I do not know how to make an object move. I understood that we had to initialize NewtonBodySetForceAndTorqueCallback and NewtonBodySetTransformCallback. However, it is quite vague and it does not work. So, here is my code:
physics.h
- Code: Select all
#include <newton/Newton.h>
#include <core/dg.h>
static NewtonWorld *world;//rp3d::DynamicsWorld *world;
struct MovSet{
glm::vec3 pos;
glm::vec3 force;
float mass;
};
#define GRAVITY -9.81f
static void ApplyTransform(const NewtonBody* body, const dFloat* matrix, int threadIndex)
{
MovSet *mset = (MovSet *) NewtonBodyGetUserData(body);
// Get the position from the matrix
glm::vec3 posit (matrix[12], matrix[13], matrix[14]);
// set the new position and orientation for this entity
mset->pos = posit;
NewtonBodySetCentreOfMass(body, glm::value_ptr(mset->pos));
}
// callback to apply external forces to body
static void ApplyForceAndTorque(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);
MovSet *mset = (MovSet*) NewtonBodyGetUserData(body);
glm::vec3 gravity (0.f, mset->mass * GRAVITY, 0.f);
mset->force += gravity;
NewtonBodySetForce(body, glm::value_ptr(mset->force+gravity));
}
class Sphere{
public://private:
NewtonBody *body;
float rad;
//public:
BS(float radius, MovSet mset) : rad(radius){
NewtonCollision *colli = NewtonCreateBox(world, rad, rad, rad, 0, NULL);
dgMatrix m = dgMatrix(dgQuaternion(), dgVector(mset.pos.x,mset.pos.y,mset.pos.z,1.f));
body = NewtonCreateDynamicBody(world, colli, &m[0][0]);
NewtonBodySetMassProperties(body, mset.mass, colli);
NewtonBodySetCentreOfMass(body, glm::value_ptr(mset.pos));
NewtonDestroyCollision(colli);
// save the entity as the user data for this body
NewtonBodySetUserData (body,&mset);
NewtonBodySetFreezeState(body,0);
NewtonBodySetForceAndTorqueCallback(body, ApplyForceAndTorque); // register the apply force and torque callback
NewtonBodySetTransformCallback(body, ApplyTransform); // register the transformation callback
}
~BS(){
NewtonDestroyBody(body);
body = 0;
}
float* transform() {
float matrix[16];
NewtonBodyGetMatrix(body, matrix);
return matrix;
}
glm::vec3 center(){
float p[3];
NewtonBodyGetCentreOfMass(body, p);
return glm::make_vec3(p);
}
glm::vec3 set_center(float *p){
NewtonBodySetCentreOfMass(body, p);
}
MovSet *mset(){
return (MovSet *)NewtonBodyGetUserData(body);
}
};
#endif // PHYSICS_H
In my scene class, in my function update, I use
- Code: Select all
NewtonUpdate(world, 1.f/60.f);
Here is how I init the NewtonWorld
- Code: Select all
world = NewtonCreate();
// configure the Newton world to use iterative solve mode 0
// this is the most efficient but the less accurate mode
NewtonSetSolverModel (world, 5);
// for deterministic behavior call this function each time you change the world
NewtonInvalidateCache (world);
Thank you for your help.
My name is arkdemon and I don't approve this message

-

arkdemon
-
- Posts: 90
- Joined: Sat Jan 18, 2014 12:38 pm
by Julio Jerez » Sun Jan 19, 2014 1:03 pm
look at this tutorial
..\newton-dynamics\applications\tutorialsSDK300\NewtonTutorials
-
Julio Jerez
- Moderator

-
- Posts: 12426
- Joined: Sun Sep 14, 2003 2:18 pm
- Location: Los Angeles
-
by arkdemon » Sun Jan 19, 2014 1:37 pm
Hello, thank you for your answer.
I made it work: here is the code:
- Code: Select all
static NewtonWorld *world;
struct MovSet{
glm::vec3 pos,rot;
glm::vec3 force, torque;
glm::mat4 model;
float mass;
};
#define GRAVITY -9.81f
static void ApplyTransform(const NewtonBody* body, const dFloat* matrix, int threadIndex)
{
MovSet *mset = (MovSet*) NewtonBodyGetUserData(body);
glm::vec3 p = glm::vec3(matrix[12], matrix[13], matrix[14]);
mset->pos = p;
mset->model = glm::make_mat4(matrix);
}
// callback to apply external forces to body
static void ApplyForceAndTorque(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);*/
MovSet *mset = (MovSet*) NewtonBodyGetUserData(body);
glm::vec3 gravity (0.f, mset->mass * GRAVITY, 0.f);
mset->force += gravity;
NewtonBodySetForce(body, glm::value_ptr(mset->force));
mset->force = glm::vec3(0.f);
}
class BS{
public:
NewtonBody *body;
float rad;
BS(float radius, MovSet &mset) : rad(radius){
NewtonCollision *colli = NewtonCreateBox(world, rad, rad, rad, 0, NULL);
dgMatrix m = dgMatrix(dgQuaternion(), dgVector(mset.pos.x,mset.pos.y,mset.pos.z,1.f));
body = NewtonCreateDynamicBody(world, colli, &m[0][0]);
NewtonBodySetMassProperties(body, mset.mass, colli);
NewtonBodySetCentreOfMass(body, glm::value_ptr(mset.pos));
NewtonBodySetForce(body, glm::value_ptr(mset.force));
mset.model = glm::translate(glm::mat4(1.f), mset.pos);
NewtonBodySetMatrix(body, glm::value_ptr(mset.model));
NewtonDestroyCollision(colli);
// save the entity as the user data for this body
NewtonBodySetUserData (body,&mset);
NewtonBodySetFreezeState(body,0);
NewtonBodySetSleepState(body, 0);
NewtonBodySetForceAndTorqueCallback(body, ApplyForceAndTorque); // register the apply force and torque callback
NewtonBodySetTransformCallback(body, ApplyTransform); // register the transformation callback
}
~BS(){
NewtonDestroyBody(body);
body = 0;
}
float* transform() {
float matrix[16];
NewtonBodyGetMatrix(body, matrix);
return matrix;
}
glm::vec3 center(){
float p[3];
NewtonBodyGetCentreOfMass(body, p);
return glm::make_vec3(p);
}
glm::vec3 set_center(float *p){
NewtonBodySetCentreOfMass(body, p);
}
MovSet *mset(){
return (MovSet *)NewtonBodyGetUserData(body);
}
};
I found out that it is REALLY IMPORTANT to send the REFERENCE for NewtonBodySetUserData
My name is arkdemon and I don't approve this message

-

arkdemon
-
- Posts: 90
- Joined: Sat Jan 18, 2014 12:38 pm
by Julio Jerez » Sun Jan 19, 2014 2:19 pm
maybe you will better using the CNetwon
../newton-dynamics\applications\tutorialsSDK300\CNewtonTutorials
it is much small set of file, and eassy to get going in C++.
It already has calles that handle user data, ray case, and lot of other things.
-
Julio Jerez
- Moderator

-
- Posts: 12426
- Joined: Sun Sep 14, 2003 2:18 pm
- Location: Los Angeles
-
by arkdemon » Sun Jan 19, 2014 3:04 pm
Ok thank you very much.
My name is arkdemon and I don't approve this message

-

arkdemon
-
- Posts: 90
- Joined: Sat Jan 18, 2014 12:38 pm
Return to General Discussion
Who is online
Users browsing this forum: No registered users and 1 guest