ISceneNode -> Newton body

Post your questions, suggestions and experiences regarding game design, integration of external libraries here. For irrEdit, irrXML and irrKlang, see the
ambiera forums
Post Reply
Kim2
Posts: 14
Joined: Thu May 31, 2007 12:40 am

ISceneNode -> Newton body

Post by Kim2 »

Hello guys. I am wondering how I can pass an ISceneNode in my project:

Code: Select all

	mesh = Sys.smgr->getMesh("data/plate.3ds");
	node = Sys.smgr->addAnimatedMeshSceneNode(mesh);

	physicsman.createCube(node);
That is how I am calling the function which will create the physics body; this function needs the node so it can attach the physics body to it (this means when you move the node around the physics body will follow it)

Code: Select all

NewtonCube *physicsman::createCube(ISceneNode *node)
{
	NewtonCube *tmp = new NewtonCube;

	//tmp->mesh = smgr->getMesh("data/smallcube.3ds");
	//tmp->node = smgr->addAnimatedMeshSceneNode(tmp->mesh);
	//tmp->node->setMaterialTexture(0, driver->getTexture("data/crate.jpg"));

	// Create a box primitive. 38 is just an estimated value of the size of the model,
	tmp->collision = NewtonCreateBox(nWorld, 50, 50, 50, NULL);
	tmp->body = NewtonCreateBody(nWorld, tmp->collision);

	// Set user data pointer to the scene node
	NewtonBodySetUserData(tmp->body, tmp->node);

	// Set body mass & inertia matrix
	NewtonBodySetMassMatrix (tmp->body, 10.0f, 150.0f, 150.0f, 150.0f);

	// Set the freeze threshhold to 1 unit (default is 0.01 but irrlight uses a large unit scale)
	NewtonBodySetFreezeTreshold(tmp->body, 1.0, 1.0, 1.0); 
	NewtonBodySetAutoFreeze(tmp->body,0);
	// Set callback functions for the body
	NewtonBodySetForceAndTorqueCallback(tmp->body, EnemyApplyForceAndTorqueEvent);

	NewtonBodySetMaterialGroupID (tmp->body, metalID);

	if (currentCube == 512)
	{
		printf("* Too many cubes!");
		return NULL;
	}

	cubes[currentCube] = tmp;
	currentCube ++;

	printf("\n\nPlayer physics object created\n");

	return tmp;
}
physicsman.cpp(110): error C2065: 'ISceneNode' : undeclared identifier
physicsman.cpp(110): error C2065: 'node' : undeclared identifier
physicsman.cpp(110): error C2761: 'NewtonCube *physicsman::createCube(void)' : member function redeclaration not allowed


And here is the header for the physics manager class. The above function is defined here. As you can see from the lines

// Set user data pointer to the scene node
NewtonBodySetUserData(tmp->body, tmp->node);

the function needs to know which node to attach the body to.

Code: Select all

#include "StdAfx.h"
#include <Newton.h>

#if !defined(PHYSICSMAN_H)
#define PHYSICSMAN_H

struct NewtonCube {
	ISceneNode *node;
	NewtonBody *body;
	NewtonCollision *collision;
};

class physicsman
{

	// misc
	int lastFPS;
	unsigned int lasttick;

public:
	
	// Newton vars
	NewtonWorld *nWorld;


	NewtonCollision* collisionMap;
	NewtonBody* collisionMapbody;

	//////////////////////////////////////////////////////////////////////////
	// Newton Callbacks
	static void _cdecl ApplyForceAndTorqueEvent (const NewtonBody* body);
	static void _cdecl EnemyApplyForceAndTorqueEvent (const NewtonBody* body); 

	void Init();
	NewtonCube *createPlayerBody();
	NewtonCube *createCube(ISceneNode *node);
	void Finish();
	void SetPlayerPos(float PositionX, float PositionY, float PositionZ);
	void SetEnemyPos(float PositionX, float PositionY, float PositionZ);
	void Update();

	NewtonCube *cubes[512];
	int currentCube;

};
#endif
physicsman.h(36): error C2061: syntax error : identifier 'ISceneNode'
physicsman.h( 8 ): error C2143: syntax error : missing ';' before '*'
physicsman.h( 8 ): error C2501: 'NewtonCube::ISceneNode' : missing storage-class or type specifiers
physicsman.h( 8 ): error C2501: 'NewtonCube::node' : missing storage-class or type specifiers


I'm not sure if this is the right way to do this. Please help me out!

Thanks for your time.
fmx

Post by fmx »

in physicsman.h, this is the problem line:
NewtonCube *createCube(ISceneNode *node);
you've defined an Irrlich entity (ISceneNode) yet you haven't declared the Irrlicht header/library anywhere earlier in the file

I would think that you just need to add
#include <irrlicht.h>
with the other includes (newton.h, etc)

I hope it helped
Kim2
Posts: 14
Joined: Thu May 31, 2007 12:40 am

Post by Kim2 »

Here's the full code for the class:

Code: Select all

#include "StdAfx.h"
#include "physicsman.h"
#include "Material.h"
#include "dMatrix.h"
#include "dVector.h"
#include <Irrlicht.h>

dMatrix playerLocNewt;

float playerLocX;
float playerLocY;
float playerLocZ;

dMatrix enemyLocNewt;

float enemyLocX;
float enemyLocY;
float enemyLocZ;


void physicsman::Init()
{   
	// Init newton
	nWorld = NewtonCreate(NULL, NULL);
	SetupMaterials (nWorld);
	// set up all material and  material interactions
	lasttick = 0;

	currentCube = 0;

    float boxP0[3] = {-99999999, -99999999, -99999999};
	float boxP1[3] = {99999999, 99999999, 99999999};

	NewtonSetWorldSize(nWorld, boxP0, boxP1);

	printf("\n\nPhysics world initialised.\n");
}

void physicsman::SetPlayerPos(float PositionX, float PositionY, float PositionZ)
{
	playerLocX = PositionX;
	playerLocY = PositionY;
	playerLocZ = PositionZ;
}

void physicsman::SetEnemyPos(float PositionX, float PositionY, float PositionZ)
{
	enemyLocX = PositionX;
	enemyLocY = PositionY;
	enemyLocZ = PositionZ;
}

void physicsman::Finish()
{
	// release the collision tree
	NewtonReleaseCollision(nWorld, collisionMap);

	// release the box primitives
	for (int i=0; i<currentCube; i++)
		NewtonReleaseCollision(nWorld, cubes[i]->collision);
	// clear all resources use with materials
	// finish newton & irrlicht
	NewtonDestroy(nWorld);
}


//////////////////////////////////////////////////////////////////////////
// Adds a cube into the world.
//////////////////////////////////////////////////////////////////////////

NewtonCube *physicsman::createPlayerBody()
{
	NewtonCube *tmp = new NewtonCube;

	//tmp->mesh = smgr->getMesh("data/smallcube.3ds");
	//tmp->node = smgr->addAnimatedMeshSceneNode(tmp->mesh);
	//tmp->node->setMaterialTexture(0, driver->getTexture("data/crate.jpg"));

	// Create a box primitive. 38 is just an estimated value of the size of the model,
	tmp->collision = NewtonCreateBox(nWorld, 50, 50, 50, NULL);
	tmp->body = NewtonCreateBody(nWorld, tmp->collision);

	// Set user data pointer to the scene node
	//NewtonBodySetUserData(tmp->body, tmp->node);

	// Set body mass & inertia matrix
	NewtonBodySetMassMatrix (tmp->body, 10.0f, 150.0f, 150.0f, 150.0f);

	// Set the freeze threshhold to 1 unit (default is 0.01 but irrlight uses a large unit scale)
	NewtonBodySetFreezeTreshold(tmp->body, 1.0, 1.0, 1.0); 
	NewtonBodySetAutoFreeze(tmp->body,0);
	// Set callback functions for the body
	NewtonBodySetForceAndTorqueCallback(tmp->body, ApplyForceAndTorqueEvent);
	NewtonBodySetMaterialGroupID (tmp->body, metalID);

	if (currentCube == 512)
	{
		printf("* Too many cubes!");
		return NULL;
	}

	cubes[currentCube] = tmp;
	currentCube ++;

	printf("\n\nPlayer physics object created\n");

	return tmp;
}


NewtonCube *physicsman::createCube(ISceneNode *node)
{
	NewtonCube *tmp = new NewtonCube;

	//tmp->mesh = smgr->getMesh("data/smallcube.3ds");
	//tmp->node = smgr->addAnimatedMeshSceneNode(tmp->mesh);
	//tmp->node->setMaterialTexture(0, driver->getTexture("data/crate.jpg"));

	// Create a box primitive. 38 is just an estimated value of the size of the model,
	tmp->collision = NewtonCreateBox(nWorld, 50, 50, 50, NULL);
	tmp->body = NewtonCreateBody(nWorld, tmp->collision);

	// Set user data pointer to the scene node
	NewtonBodySetUserData(tmp->body, tmp->node);

	// Set body mass & inertia matrix
	NewtonBodySetMassMatrix (tmp->body, 10.0f, 150.0f, 150.0f, 150.0f);

	// Set the freeze threshhold to 1 unit (default is 0.01 but irrlight uses a large unit scale)
	NewtonBodySetFreezeTreshold(tmp->body, 1.0, 1.0, 1.0); 
	NewtonBodySetAutoFreeze(tmp->body,0);
	// Set callback functions for the body
	NewtonBodySetForceAndTorqueCallback(tmp->body, EnemyApplyForceAndTorqueEvent);

	NewtonBodySetMaterialGroupID (tmp->body, metalID);

	if (currentCube == 512)
	{
		printf("* Too many cubes!");
		return NULL;
	}

	cubes[currentCube] = tmp;
	currentCube ++;

	printf("\n\nPlayer physics object created\n");

	return tmp;
}

//////////////////////////////////////////////////////////////////////////
//
// The last 2 functions are callbacks from newton
//
//////////////////////////////////////////////////////////////////////////

void _cdecl physicsman::ApplyForceAndTorqueEvent (const NewtonBody* body) 
{ 
	float mass; 
	float Ixx; 
	float Iyy; 
	float Izz; 
	float force[3]; 
	float torque[3]; 

	NewtonBodyGetMassMatrix (body, &mass, &Ixx, &Iyy, &Izz); 

	force[0] = 0.0f; 
	force[1] = 0.0f; 
	force[2] = 0.0f; 

	torque[0] = 0.0f; 
	torque[1] = 0.0f; 
	torque[2] = 0.0f; 

	NewtonBodyAddForce (body, force); 
	NewtonBodyAddTorque (body, torque);

	playerLocNewt[2][4] = playerLocX;
	playerLocNewt[3][1] = playerLocY;
	playerLocNewt[3][2] = playerLocZ;
	NewtonBodySetMatrix(body, &playerLocNewt[0][0]); 

	//printf("Location of player physics object is %f, %f, %f.\n",playerLocNewt[2][4],playerLocNewt[3][1],playerLocNewt[3][2]);
}

void _cdecl physicsman::EnemyApplyForceAndTorqueEvent (const NewtonBody* body) 
{ 
		float mass; 
	float Ixx; 
	float Iyy; 
	float Izz; 
	float force[3]; 
	float torque[3]; 

	NewtonBodyGetMassMatrix (body, &mass, &Ixx, &Iyy, &Izz); 

	force[0] = 0.0f; 
	force[1] = 0.0f; 
	force[2] = 0.0f; 

	torque[0] = 0.0f; 
	torque[1] = 0.0f; 
	torque[2] = 0.0f; 

	NewtonBodyAddForce (body, force); 
	NewtonBodyAddTorque (body, torque);

	enemyLocNewt[2][4] = enemyLocX;
	enemyLocNewt[3][1] = enemyLocY;
	enemyLocNewt[3][2] = enemyLocZ;
	NewtonBodySetMatrix(body, &enemyLocNewt[0][0]); 

	//printf("Location of enemy physics object is %f, %f, %f.\n",enemyLocNewt[2][4],enemyLocNewt[3][1],enemyLocNewt[3][2]);
}

void physicsman::Update()
{
	NewtonUpdate (nWorld, 1.0f/60.0f);
}

It gives me those errors despite the fact Irrlicht.h is included.
Post Reply