Tokamak Trouble !

Started by
13 comments, last by Emmanuel Deloget 16 years, 8 months ago
Hi all, I've been working on implementing Tokamak Physics engine into my game engine. Now, it all started out nicely. I downloaded the libraries, got my engine to compile with it etc. So here I am, I wrote a physicshandler class. However, trouble already arrises at the initialize function, physicshandler.cpp

#ifdef WIN32
#include <windows.h>
#endif

// Engine headers
#include "common.h"
#include "system.h"

using namespace Led::System;
using namespace Led::Math;

/// Constructor.
PhysicsHandler::PhysicsHandler()
{
	sim = NULL;
}

/// Initialize. Initializes the physics manager and preserves space for the amount of bodies.
/// @variable int rigbodies the amount of rigid bodies.
/// @variable int animbodies the amount of animated bodies.
int PhysicsHandler::Initialize(int rigbodies, int animbodies)
{
	unsigned long totalbodies = 0;
	neSimulatorSizeInfo sizeinfo;

	// Setup our physics engine info.
	sizeinfo.rigidBodiesCount = rigbodies;
	sizeinfo.animatedBodiesCount = animbodies;

	// Calculate the total amount of bodies.
	totalbodies = sizeinfo.rigidBodiesCount + sizeinfo.animatedBodiesCount;
	sizeinfo.geometriesCount = totalbodies;

	// Calculate the max pair of overlapping bodies.
	sizeinfo.overlappedPairsCount = totalbodies * (totalbodies - 1) / 2;
	sizeinfo.rigidParticleCount = 0; 
	sizeinfo.constraintsCount = 0; 
	sizeinfo.terrainNodesStartCount = 0;

	// Set our gravity.
	gravity.Set(0.0f, -10.0f, 0.0f);

	// Finally create our simulator.
	sim = neSimulator::CreateSimulator(sizeinfo, NULL, &gravity);

	return 0;
}

/// Run. Runs the physics every frame.
int PhysicsHandler::Run()
{
	if (sim != NULL)
		sim->Advance(1, 2, NULL);

	return 0;
}

/// Destroy. Destroys the physics manager.
int PhysicsHandler::Destroy()
{
	// Destroy the simulator.
	neSimulator::DestroySimulator(sim);

	// Set sim to NULL.
	sim = NULL;
	
	return 0;
}

/// SetTerrainMesh. Sets the terrainmesh pointer.
/// @variable neTriangleMesh mesh the new mesh.
int PhysicsHandler::SetTerrainMesh(neTriangleMesh *mesh)
{
	if (sim)
		if (mesh != NULL)
			sim->SetTerrainMesh(mesh);

	return 0;
}

/// GetSimulator. Returns a pointer to the simulator.
neSimulator *PhysicsHandler::GetSimulator()
{
	return sim;
}


physicshandler.h

#ifndef PHYSICSHANDLER_H
#define PHYSICSHANDLER_H

namespace Led
{

	namespace System
	{
		class PhysicsHandler
		{
		public:
			/// Constructor.
			PhysicsHandler();

			/// Initialize. Initializes the physics handler and preserves space for the amount of bodies.
			/// @variable int rigbodies the amount of rigid bodies.
			/// @variable int animbodies the amount of animated bodies.
			int Initialize(int rigbodies, int animbodies);

			/// Run. Updates the physics every frame at the given speed.
			int Run();

			/// Destroy. Destroys the physics handler.
			int Destroy();

			/// SetTerrainMesh. Sets the terrainmesh pointer.
			/// @variable neTriangleMesh mesh the new mesh.
			int SetTerrainMesh(neTriangleMesh *mesh);

			/// GetSimulator. Returns a pointer to the simulator.
			neSimulator *GetSimulator();

		private:
			neV3 gravity;
			neSimulator *sim;
		};
	};
};

#endif


For some reason, I get this error: Unhandled exception at 0x003d273f in LedEngine.exe: 0xC0000005: Access violation writing location 0x00000000. With VC++'s debugger pointing at the return 0; line of the Initialize function. Does anybody know if I am doing something incorrectly and if the problem lies within that function ? [EDIT!!!!!!!] This problem has already been fixed, read below for my other problem. [/EDIT!!!!!!!] [Edited by - PinguinDude on July 23, 2007 4:40:32 PM]
http://sourceforge.net/projects/pingux/ <-- you know you wanna see my 2D Engine which supports DirectX and OpenGL or insert your renderer here :)
Advertisement
i think this is wrong:
sizeinfo.overlappedPairsCount = totalbodies * (totalbodies - 1) / 2;

should be something like this
sizeInfo.overlappedPairsCount = ( rigbodies * animbodies ) + rigbodies * ( rigbodies - 1 ) / 2;

try changing that.
----------------------------"I refuse to answer that question on the grounds that I don't know the answer"-- Douglas Adams (1952 - 2001)
I'm not familiar with Tokomak, but quite often when catching an error down in a library, the debugger will be pointing at the line AFTER the function that crashed.

Is it perhaps trying to use that NULL you're passing in?

Is there a library initialise method you need to call before using Tokomak?
Nope, there are no methods required to be called before any of those functions. Also, I tried to change what you told me to but that isn't making any difference either. :\
http://sourceforge.net/projects/pingux/ <-- you know you wanna see my 2D Engine which supports DirectX and OpenGL or insert your renderer here :)
Well, I got it fixed to NOT crash there. The problem was, I was calling this:
/// GeneratePhysicsModel. Generates the model's physics information.int HeightObject::GeneratePhysicsModel(){	// Now we're done with that, let's generate some information for the physics engine.	terrainbody.vertexCount = num_verts;	// Create a new vertice array specially for the physics manager.	terrainverts = new neV3[num_verts];	for (int i = 0; i < num_verts; i++)	{		terrainverts.Set(vertbuffer.x, vertbuffer.y, vertbuffer.z);	}	// Assign the terrain's vertices.	terrainbody.vertices = terrainverts;	// Calculate the amount of triangles in the terrain.	terrainbody.triangleCount = ((width-1)*(height-1)*2);	terraintris = new neTriangle[terrainbody.triangleCount];	int dindex = 0;	// Generate a triangle array.	for (int x = 0; x < width - 1; x++)	{		for (int y = 0; y < height - 1; y++)		{			terraintris[dindex].indices[0] = x + y * width;			terraintris[dindex].indices[1] = x + (y + 1) * width;			terraintris[dindex].indices[2] = (x + 1) + y * width;			terraintris[dindex].materialID = 0;			terraintris[dindex].flag = neTriangle::NE_TRI_TRIANGLE;						dindex += 1;			terraintris[dindex].indices[0] = x + (y + 1) * width;			terraintris[dindex].indices[1] = (x + 1) + (y + 1) * width;			terraintris[dindex].indices[2] = (x + 1) + y * width;			terraintris[dindex].materialID = 0;			terraintris[dindex].flag = neTriangle::NE_TRI_TRIANGLE;						dindex += 1;		}	}	// Set the terrain's triangle pointer.	terrainbody.triangles = terraintris;	//engine->GetPhysicsHandler()->GetSimulator()->SetTerrainMesh(&terrainbody);	return 0;}

before initialization.. However...

Now, when I uncomment the last line, it crashes with another error.

Unhandled exception at 0x003d273f in LedEngine.exe: 0xC0000005: Access violation writing location 0x00000000.

What is going on here, Anyone ?
http://sourceforge.net/projects/pingux/ <-- you know you wanna see my 2D Engine which supports DirectX and OpenGL or insert your renderer here :)
Have you tried splitting that big ol' line out into

Type1* handler = engine->GetPhysicsHandler();
Type2* simulator = handler->GetSimulator();
simulator->SetTerrainMesh(&terrainbody);

And then stepping through with a debugger?


Yeah, it's obviously happening at the SetTerrainMesh function... :\ This really sucks because when I look at tutorials my code looks perfectly fine. I can't find any other reason why this would happen either. My engine also worked fine before adding in tokamak.
http://sourceforge.net/projects/pingux/ <-- you know you wanna see my 2D Engine which supports DirectX and OpenGL or insert your renderer here :)
It's probably not much help, but I personally find it much easier to take something simple that is working and add complexity to it, than trying to take something complex that crashes (or otherwise doesn't work) and make it work.

So the next thing I'd try is sending the simplest terrain mesh (a single quad) to Tokomak and see if this works. Once I had that working, I'd start bringing back the real geometry. If you can't get a basic quad working, you know the problem is most likely in the way you're using the library, rather than the terrain geometry you're passing in.
You need to set your sizeinfo.terrainNodesStartCount to the number of triangles in your terrain. Apart from that I can't see anything else obvious.

Good Luck,
ViLiO
Richard 'ViLiO' Thomasv.net | Twitter | YouTube
Thx alot, that seemed to have done the trick ! Rating up for you !.

Also, if any other problems arrise I'll be sure to post them in here.
http://sourceforge.net/projects/pingux/ <-- you know you wanna see my 2D Engine which supports DirectX and OpenGL or insert your renderer here :)

This topic is closed to new replies.

Advertisement