Basics

Hi, I’m making my end-deegree project using Physx, but I have a little problem, I have only a box which is pushed up a bit, and then it falls, it works right, but when I try to create floor to stop the falling, it fails.
I’m trying to use the example code, so the box is a RigidDynamic and the floor is a RigidStatic, only creating it, the floor is like a ghost, the box pass through it, but if I use:

shape=plane->createShape(PxPlaneGeometry(), *mMaterial);

then it crashes when the box reach the floor.
I suppose this is a tinny thing I’m not seeing, but I need help because I’m not able to see what I need to do.
Thank you.

You should connect your app to PVD to see what’s going on.

Check out the filterShader which controls what is colliding with what.

if(!sceneDesc.filterShader)
	sceneDesc.filterShader	= getSampleFilterShader();

PX_FORCE_INLINE PxSimulationFilterShader getSampleFilterShader()
{
return PxDefaultSimulationFilterShader;
}

pairFlags = PxPairFlag::eCONTACT_DEFAULT;

Thanks for your answer,

I use the PVD everytime, but I don’t know how to use it more than viewing a box falling or selecting items…

After modifying the scene.filterShader=… like you have write, it don’t crash, but don’t collide too, so is the same result as commenting the line of shape=plane…

The screen of my PVD is this:External Media

I don’t know what more modify to make it working.
Thank you.

I had the same problem that no collision was performed. I followed a tutorial for PhysX 3.1, so something might have changed in the meantime. The problem was that the default SimulationFilterShader did not set the PxPairFlag to eCONTACT_DEFAULT, for whatever reason.

I created my own and now it works:

PxFilterFlags FilterShader (
	PxFilterObjectAttributes attributes0,
	PxFilterData filterData0, 
	PxFilterObjectAttributes attributes1,
	PxFilterData filterData1,
	PxPairFlags& pairFlags,
	const void* constantBlock,
	PxU32 constantBlockSize)  {
        pairFlags = PxPairFlag::eCONTACT_DEFAULT;
        return PxFilterFlag::eDEFAULT;
}

Note that it doesn’t mimic the default filter shader, so you might have to extend it. I just started as well, so if something is wrong with my implementation, please let me know.

It is the same, I have create a FilterShader like yours, and when I try to run with it the answer is the same, the app crash in the moment that the box collide with the floor, even in the PVD I can see the box a little under the floor, but I suppose it is normal, but IO don’t know.

Thanks for your help, I’ll continue trying.

What errors on the console did you get when the app crashed?

There’s a tutorial on on to use PVD PVD tutorial video - YouTube

Thanks for your answer, the console doesn’t have any errors, it apperas a pop up message with:

that in english should mean:

what if I try to debug, it send me to “PxDEfaultAllocator.h” at the “allocate()” function, in the return sentence:

return _aligned_malloc(size, 16);

more exactly, line 60, but it is not possible to be there the mistake because I have not touch any of this file, and I guess nvidia is not going to free an API with this kind of files, and I guess the rest of the people don’t have this trouble, so the fail should not be there.

About the tutorial, is very intersting, it has not show me nothing that could help me right now, but it shows a lot of interesting things about it, thank you very much.

Do you have a call stack when it crashes? I suggest you clone the SampleHelloWorld.

I have not a call stack because I only have one thread, Should I use one thread only for Physx? I mean, I know it would work better, but it is supposed to work without it, isn’t it?

About clonning SampleHelloWorld, I’m trying to follow just the steps in the guide, the “.chm” file who comes with the SDK, Where can I found this SampleHellowWorld? I suppose this sample should have more than one objects, because if it is only to try compiling a basic “Hello World”, I can do that, because I’m able to make one box works properly, the trouble is when I use ground, or another boxes.

Thanks for your help, I’ll be trying whatever I can.

Hi,

I don´t have the PhysX SDK here on this laptop - or my own source code.
But I´m really sure you receive this NULL pointer exception due to the fact you use the
PxDefaultSimulationFilter or allocater wrong.

Just do a fresh and clean copy and past of the samples - and compile the code again.
Or better: Look into the code and the samples, compile them, test them.
Maybe I´m wrong, but I seems to me that you did a copy and past of the samples - and cant even handle it.

You should know what you are doing, you said this is your “end degree project”

  • it sounds like you are already done with studing IT.

This isnt mean bad - its only a kind of weird, that a coder need help at the very
first stage of using PhysX - its almost a hello world example.
( And due to the fact that there are many samples to look into it and you didn´t solve it yet…)

When I´m home tomorrow, I will upload a small snippet to help you.

Another thing:

You could debug your code - step by step. You could even test your PxActor or something or shapes
for an invalid status (or pointer) - but I guess you already did it.
Thus as you said, there are no need to get access to the source code of PhysX - it´s maybe an easy
problem.

It would be easier for us when you upload your source code.

Other thing:
You have a call stack, of course, even when you have the simplest hello world example in c.
You dont need to have a multi threaded app (which you already have when you using PhysX - it uses some threads to calculate it) to use PhysX.

You could even compile the code which says in the PhysXGuide - I´ve test it for years.
( Ok, old code, but I´m pretty sure, the code in the guide is working )

To find the samples in the PhysX folder:
Look at a folder named “sample”. (When I remeber correctly.)
→ Then look at the example “Hello World” - or something similar.
You need to dig in the files to find the definition of the PhysX initalisation.

As I said before, I will post more information when I´m home tomorrow - when needed.)

Hi, I can not make an exact fresh copy of the examples, because it has a little things that are ommited, like var creations, or functions which doesn’t exists, like:

customizeSceneDesc(sceneDesc);

I have not done a copy&paste, I have spent almost a month trying to understand everything on the code which comes in the guide, and only when I knew what is supposed to do every code line, is when I have tried to use it, I have no problem compiling anything, and I’m able to start Physx, to give it a box, a force vector, and a gravity vector, so a box is launched up(or where I want) and then it falls using physx calculations.

Yes, I don’t know how it works outside, but in my country, when a student has passed every subject of a degree, he has to do a project that shows that he has learned everything and is ready for a workplace…

Yeah, I know this is almost a Hello World, this is the reason of being as frustrating…
I have not found these many samples you said, I have found now the samples in the Physx folder you have said me, but It seems to be very confusing having a lot of files, and using in a file something that is in any other, I’ll try to follow it, but I think they could have done it easier…

About debugging, Yes, I have done it, but it is useless, If I use the line I said at beggining, it fails just before:

while(!gScene->fetchResults() )

at StepPhysx() function, and the objects seems to work fine (but given that I have not manage to work right with multiples objects, maybe it is not fine, and I’m wrong).

If you think, it could be easier if I upload the code, I could try to do it, but is extensive, I’ll try to upload it later…

About the call stack, yes, you are right, this stack exists, I’m Spanish so I don’t master some expresions, what I tried to mean is that I have not any visualizer, I mean, it exists, but I haven’t it, because I’m not looking at it, that is what I thought he was asking me…
This is the reason what I have said that I have not multiples threads, even knowing that Physx use threads, because I have not control of them (I think I have no control of it…, maybe It has advanced options to manage it).

Thanks for all this information and I’ll be very gratefull if you post more info later, I’ll upload my code when I can select it. Thanks again.

Edit: the code are below:

Well, I have a class called Px, this Physx.h file contains the following code:

# pragma warning( push )
 # pragma warning( disable : 4018 )
//# define _ITERATOR_DEBUG_LEVEL 0
# define NDEBUG 1
/*
#ifndef MAX_PATH
#define MAX_PATH 256
#endif
*/
# include "Cubo.h"
# include <iostream>

# include <FPxPhysicsAPI.h> 
//# include <PxPhysicsAPI.h> 
# include <PxExtensionsAPI.h> 
# include <PxDefaultErrorCallback.h>
# include <PxDefaultAllocator.h> 
# include <PxDefaultSimulationFilterShader.h>
# include <PxDefaultCpuDispatcher.h>
# include <PxShapeExt.h>
# include <PxMat33Legacy.h> 
# include <PxSimpleFactory.h>

# include <vector>

# ifdef _WIN64
# pragma comment(lib, "PhysX3_x64.lib")
# else
# pragma comment(lib, "PhysX3_x86.lib")
# endif
# pragma comment(lib, "PxTask.lib")
# pragma comment(lib, "Foundation.lib")
# pragma comment(lib, "PhysX3Extensions.lib")
# pragma comment(lib, "GeomUtils.lib") 

const int	WINDOW_WIDTH=1024, 
			WINDOW_HEIGHT=768;



using namespace std;
using namespace physx;
using namespace pubfnd3;





class Px
{
public:
	//Atributos
	PxPhysics* gPhysicsSDK;
	PxDefaultErrorCallback gDefaultErrorCallback;
	PxDefaultAllocator gDefaultAllocatorCallback;
	PxSimulationFilterShader gDefaultFilterShader;
	PxScene* gScene;
	PxReal myTimestep;
	vector<PxRigidActor*> boxes;
	PxReal d;	 
	PxTransform pose;
	PxReal density;
	PxMaterial* mMaterial;
	//PxShape* shape;

	PxRigidStatic* plane;
	PxTransform transsuelo;

	//Métodos
	Px();
	~Px(){};
	void DrawBox(PxShape* shape);

	PxScene* InitializePhysX();

	void DrawShape(PxShape* shape);

	void DrawActor(PxRigidActor* actor) ;

	void RenderActors();

	void ShutdownPhysX();

	void StepPhysX(/*PxScene *gScene*/);

	void getColumnMajor(PxMat33 m, PxVec3 t, float* mat);
};





#pragma warning( pop )

Its Physx.cpp file contains:

# include "Physx.h"

			cerr<<"¡Fallo en PxDefaultCpuDispatcherCreate!"<<endl;
			mciSendStringA("play C:/Windows/SysWow64/pasado.wav",0,0,0);	
			Sleep(555);
			exit (42);
		};
        sceneDesc.cpuDispatcher = mCpuDispatcher;
     } 
 	if(!sceneDesc.filterShader)
		{
			//sceneDesc.filterShader  = gDefaultFilterShader;
			sceneDesc.filterShader	= getSampleFilterShader();
		};


	//PxPairFlag::Enum pairFlags = PxPairFlag::eCONTACT_DEFAULT;
//		};
	
	 //Zona peliaguda, cruzar los dedos...
	//gScene=NULL;
	if(!gScene)
	{
		gScene = gPhysicsSDK->createScene(sceneDesc);
	};
	Sleep(555);//Demosle tiempo para prepararse...
	if (!gScene)
	{
		gScene = gPhysicsSDK->createScene(sceneDesc);//Intento crearla una segunda vez antes de dar un fallo.
		if (!gScene)
		{
cerr<<"¡Fallo en createScene!"<<endl;
			//Sleep(5);
			//exit(-1);
			//Aquí no le pongo música porque ya la reproduce fuera si ha entrado aquí, para eso se usa laurelindorinan...
		};
	};
	if(gScene)
	{
		gScene->setVisualizationParameter(PxVisualizationParameter::eSCALE,				 1.0);
		gScene->setVisualizationParameter(PxVisualizationParameter::eCOLLISION_SHAPES,	1.0f);
	};

	plane = gPhysicsSDK->createRigidStatic(pose);/////////////////////////////////////////////////// IMPORTANTE, SUELO
	if (!plane)
	{
		if(pose.isValid())
		{
			plane = gPhysicsSDK->createRigidStatic(pose);//intentándolo por segunda vez
		}else{
			mciSendStringA("play C:/Windows/SysWow64/pasado.wav",0,0,0);	
		};
		if (!plane)//Si aún así, no hay plano...
		{
			cerr<<"create plane failed!"<<endl;
			mciSendStringA("play C:/Windows/SysWow64/pasado.wav",0,0,0);	
			Sleep(555);
		};
	}else{

			PxShape* shape;
			PxMaterial *A;
		
			A=gPhysicsSDK->createMaterial(0.5,0.5,0.5);///
			mMaterial=A;
			shape=plane->createShape(PxPlaneGeometry(), *mMaterial);//////////////////////////////////////////////////////////////////////////////////////////////////////////////
			//if(PxPlaneGeometry.isValid())
			shape->setName("Pangea");
			if (!shape)
			{
				mciSendStringA("play C:/Windows/SysWow64/pasado.wav",0,0,0);
				exit(69);
			};
				//cerr<<"create shape failed!"<<endl;
			plane->setActorFlag(physx::PxActorFlag::eDISABLE_GRAVITY,1);
			gScene->addActor(*plane);
	};

	return gScene;
}
void Px::ShutdownPhysX() {
	 vector <PxRigidActor*>::iterator k=boxes.begin();
	for(unsigned int i=0;i<boxes.size();i++,k++) 
	{
		gScene->removeActor(*boxes[i]);	//.....
		//boxes[i]->release();//......
		(*k)->release();
	}

	boxes.clear();//......
	if(gScene)gScene->release();//..........
	
	gPhysicsSDK->release();
}
void Px::StepPhysX(/*PxScene *gScene*/) 
{ 
gScene->simulate(myTimestep);       
myTimestep+=1.0f/120.0f;//I'm trying to use 3D vision, so the frames should be 120fps
while(!gScene->fetchResults() )     
	{

	}
}

And then, a few functions that I’m not using:

void getColumnMajor(PxMat33 m, PxVec3 t, float* mat) 
{
   mat[0] = m.column0[0];
   mat[1] = m.column0[1];
   mat[2] = m.column0[2];
   mat[3] = 0;

   mat[4] = m.column1[0];
   mat[5] = m.column1[1];
   mat[6] = m.column1[2];
   mat[7] = 0;

   mat[8] = m.column2[0];
   mat[9] = m.column2[1];
   mat[10] = m.column2[2];
   mat[11] = 0;

   mat[12] = t[0];
   mat[13] = t[1];
   mat[14] = t[2];
   mat[15] = 1;
}             


void Px::DrawShape(PxShape* shape) 
{ 
	PxGeometryType::Enum type = shape->getGeometryType();
    switch(type) 
    {          
		case PxGeometryType::eBOX:
			DrawBox(shape);
		break;
    } 
} 

void Px::DrawBox(PxShape* shape)
{
PxTransform pT = PxShapeExt::getGlobalPose(*shape);
	PxBoxGeometry bg;
	shape->getBoxGeometry(bg);
	
	PxMat33 m = PxMat33Legacy(pT.q );
    float mat[16];
};

void Px::DrawActor(PxRigidActor* actor) 
{  
	PxU32 nShapes = actor->getNbShapes(); 
    PxShape** shapes=new PxShape*[nShapes];
	
	actor->getShapes(shapes, nShapes);     
    while (nShapes--) 
    { 
        DrawShape(shapes[nShapes]); 
    } 
	delete [] shapes;
} 
void Px::RenderActors()  { 
    // Render all the actors in the scene 
	
	for(unsigned int i=0;i<boxes.size();i++ ) {
		//DrawActor(boxes[i]);
	}
}

Now, in the main program, Im doing:

Px PX;
	PX.gPhysicsSDK=PxCreatePhysics(PX_PHYSICS_VERSION, PX.gDefaultAllocatorCallback, PX.gDefaultErrorCallback, PxTolerancesScale() );

PxVec3 position;
			 
	position.x=E.VectorIconos[0].C.m_rY*-1;
	position.y=E.VectorIconos[0].C.m_rX;
	position.z=E.VectorIconos[0].C.m_rZ*-1;

	PxU32		pos=0;
	PxQuat pxq;
	PxTransform transform(PxVec3(0.0f, 5.0, 0.0f), pxq.createIdentity());
	PxVec3 dimensions(0.5,0.5,1.0);
	PxBoxGeometry geometry(dimensions); 


	transform.p  = PxVec3(0.0f,0.0f,0.0f);
	PxMaterial *pmaterial=PX.gPhysicsSDK->createMaterial(0.5f,0.5f,0.1f);//static friction, dynamic friction, restitution
	PX.mMaterial= pmaterial;

	if(!PX.mMaterial)
	{
	 fatalError("createMaterial failed!");
	};

	PxRigidDynamic* aBoxActor = PX.gPhysicsSDK->createRigidDynamic(PxTransform(position));
	if(!aBoxActor)
	{
	mciSendStringA("play C:/Windows/SysWow64/pasado.wav",0,0,0);
	}else{
		//mciSendStringA("play C:/Windows/SysWow64/pasado.wav",0,0,0);//aki
	};
	PxTransform pose = PxTransform(PxVec3(0.0f, 0, 0.0f),PxQuat(PxHalfPi, PxVec3(0.0f, 0.0f, 1.0f)));

PxVec3 velocity(0,11,0);
		
/**/	
	vector <PxShape *> VecForms;

	PxU32		pos2=0;
	PxVec3 position2;
	position2.x=position2.y=position2.z=0;

	PxShape* temp;	
	
	/**/
	for(int i=0;i<E.cantidad;i++)
	{

		pos2=-10*i;
		position2.x=position2.y=position2.z=10*i;
	};

	PxShape* aBoxShape;	
	aBoxShape = aBoxActor->createShape(PxBoxGeometry(10/2, 10/2, 10/2),&pmaterial,pos);
	PxRigidBodyExt::updateMassAndInertia(*aBoxActor, PX.density);
	aBoxActor->setLinearVelocity(velocity);
	
	while(!PX.gScene)
	{
		PX.gScene =PX.InitializePhysX();
//				mciSendStringA("play C:/Windows/SysWow64/pasado.wav",0,0,0);
	};
	//aBoxActor->setActorFlag(physx::PxActorFlag::eDISABLE_GRAVITY,1);

	PX.gScene->addActor(*aBoxActor);

And then, in the main loop:

if(PX.gPhysicsSDK)
{
 	PX.StepPhysX(/*PX.gScene*/);//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

		PX.transsuelo = PX.plane->getGlobalPose();	

		E.CSuelo.m_rX=PX.transsuelo.p.y-2;
		E.CSuelo.m_rY=PX.transsuelo.p.x*-1;
		E.CSuelo.m_rZ=PX.transsuelo.p.z*-1;

		trans2 = aBoxActor->getGlobalPose();

		E.VectorIconos[0].C.m_rX=trans2.p.y;
		E.VectorIconos[0].C.m_rY=trans2.p.x*-1;
		E.VectorIconos[0].C.m_rZ=trans2.p.z*-1;

};

and in the exit() function: “PX.ShutdownPhysX();”

I hope this help to undersand what I’m doing wrong.
Thank you all for all the answers

Hi,

Your code snippets are a little bit strange …

Okay: Here is an answer to your last post:

Sample Solution are here: (PhysX folder)\Samples\compiler\vc10win32public
For the extensions: (PhysX folder)\Source\compiler\vc10win32public

You dont need to call customizeSceneDesc(sceneDesc); - look into the code what happens there.
The PhysXGuide.chm shows alot of code and explains it- the samples uses a lot of the code.
You should study the samples - its a little bit smashed togehter, okay, but you could read the
important related code of it. I meant with “many samples” that there are the samples which
shows much things in PhysX - from PxController to Vehicles and Clothes … Callbacks, time stepping,
convertings, trigger shapes, convex mesh cooking - triangle mesh cooking… Almost every
important topics are covered there.

You should use precomplied header. Its really easy to handle it and its takes lesser time
to compile your project. There are other improvments - just read about them.
( And about header guards - use #pragma once )

I guess its only for testing purpose - but you should abstract each possible layer from a game -
and make different classes of it. (Example: The creation of plane doesnt have to do with
the physics - its created in another class (Level?) or in the main gamemanager)

Look at this - why should you place these important references on the stack instead of the heap?
Use them as pointer like this:

PxDefaultErrorCallback* gDefaultErrorCallback;
PxDefaultAllocator* gDefaultAllocatorCallback;
PxSimulationFilterShader* gDefaultFilterShader;

You should also use something like a Px::init() method for initialize the PhysX.

Like:

void Px::init()
{
PxTolerancesScale scale;
scale.mass = 1000.f;
scale.speed = 10.f;
scale.length = 1.f;

mFoundation = PxCreateFoundation(PX_PHYSICS_VERSION,*mAllocatorCallback,*mErrorCallback); 

 if(!mFoundation) printf("PxCreateFoundation failed!");
mProfileZoneManager = &PxProfileZoneManager::createProfileZoneManager(mFoundation);
if(!mProfileZoneManager) printf("PxProfileZoneManager::createProfileZoneManager failed!");
mSDK = PxCreatePhysics(PX_PHYSICS_VERSION, *mFoundation, scale, true, mProfileZoneManager);

if(!mSDK) printf("PxCreatePhysics failed!");
if(!PxInitExtensions(*mSDK)) printf("PxInitExtensions failed!");
    mMaterial = mSDK->createMaterial(0.15f,0.15f,0.15f);
PxSceneDesc sceneDesc(mSDK->getTolerancesScale());
sceneDesc.gravity = PxVec3(0,-100.f,0);
    CpuDispatcher = PxDefaultCpuDispatcherCreate(0);
    if(!CpuDispatcher) printf("PxDefaultCpuDispatcherCreate failed!");
sceneDesc.cpuDispatcher = CpuDispatcher;
sceneDesc.filterShader  = FilterShader;
sceneDesc.simulationEventCallback = mEventCallback;
    if (!sceneDesc.filterShader) printf("FilterShader failed!");
mScene = getSDK()->createScene(sceneDesc);
    mCooking = PxCreateCooking(PxVersion,*getFoundation(),PxCookingParams()); //Only when needed
if (!getCooking()) printf("Cooking failed!\n");

}

This is uncompleted - you need to modify it:

PxFilterFlags FilterShader(PxFilterObjectAttributes attributes0, PxFilterData filterData0,PxFilterObjectAttributes attributes1, PxFilterData filterData1,
PxPairFlags& pairFlags, const void* constantBlock, PxU32 constantBlockSize)
{
pairFlags = PxPairFlag::eCONTACT_DEFAULT | PxPairFlag::eTRIGGER_DEFAULT;
return PxFilterFlag::eDEFAULT;
}

Okay… you need to modify the code above to your own code.
And always set uninitialized pointer to NULL (or when using C++ 11 to nullptr)

And when I see something like this:
PxMaterial A;
A=gPhysicsSDK->createMaterial(0.5,0.5,0.5);
I´m really worried if you really can program. Just type PxMaterial
A = gPhysics …

I´m also confused about:
PX.gScene->addActor(aBoxActor); - You set gScene as pointer - you should access to it via →
( You didnt declare PX as Px PX didnt you?! This should be a pointer like this: Px
PX !)

Your timestep is wrong.
Use a accumulator for it (semi fixed timestep)

PxReal mAccumulator = 0.0f;
PxReal mStepSize = 0.01f; // You could use another StepSize here

void update() // Physic
{
mAccumulator += mGameManager->getDeltaTime();//timeSinceLastUpdate;
if (mAccumulator > 0.1f) mAccumulator = 0.1f; // Or other limit of the mAccumulator

while (mAccumulator >= mStepSize)
{
	mAccumulator -= mStepSize;
	mScene->simulate(mStepSize);
	mScene->fetchResults(true);	
}	

}

I really dont know what you want to archiev with while(!gScene->fetchResults()) …
its … like … you didnt read the PhysXGuide and just smash some code together and hope for the best.


while(!PX.gScene)
{
PX.gScene =PX.InitializePhysX();
};
… No words needed here. Did you realize what you did here?

Hm there are other things too.
You really should study the examples to understand how you use them correctly.
(or to create /initialize PhysX related objects.)

All I said above was no offense to you!

I hope you think about your coding style and study the samples - and uses things to… (like pch)