Jump to content
  • Advertisement
Sign in to follow this  
Firecore

PhysX help

This topic is 3809 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi guys. ive been trying to do the PhysX lesson 101: Box on a Plane using D3D. it has been hard. sorry if my code posting is a bit big but i thought it might be better to identify errors. The compile returns no errors. When i run the program i get a blank screen. I thnk it is because im not drawing anyting, but it also freezes the app and i dont kno why. How do I do the drawing? i also checked all the d3d objects and they are all released in the class destructor D3DApp. Here it is:
#define NOMINMAX
#include <windows.h>


#include "d3dApp.h"
#include <NxPhysics.h>
#include <tchar.h>
#include <crtdbg.h>

// Physics SDK globals
NxPhysicsSDK* gPhysicsSDK = NULL;
NxScene*  gScene = NULL;
NxVec3  gDefaultGravity(0,-9.8,0);
bool bPause = false;

//actor globals
NxActor* groundPlane = 0;
NxActor* box = 0;

// Force globals
NxVec3 gForceVec(0,0,0);
NxReal gForceStrength = 150;
bool bForceMode = true;



//function prototypes
NxActor* CreateGroundPlane(void);
NxActor* CreateBox(void);
NxReal Updatetime(void);
void StartPhysics(void);
void GetPhysicsResults(void);

void GetPhysicsResults()
{
    // Get results from gScene->simulate(deltaTime)
    while (!gScene->fetchResults(NX_RIGID_BODY_FINISHED, false));
}



NxReal UpdateTime()
{
    NxReal deltaTime;
#ifndef LINUX
    static __int64 gTime,gLastTime;
    __int64 freq;
    QueryPerformanceCounter((LARGE_INTEGER *)&gTime);  // Get current count
    QueryPerformanceFrequency((LARGE_INTEGER *)&freq); // Get processor freq
    deltaTime = (double)(gTime - gLastTime)/(double)freq;
#else
    static clock_t gTime, gLastTime;
    gTime = clock();
    deltaTime = (NxReal)((double)(gTime - gLastTime) / 1000000.0);
#endif
    gLastTime = gTime;
    return deltaTime;
}

void StartPhysics()
{
    // Update the time step
    NxReal deltaTime = UpdateTime();

    // Start collision and dynamics for delta time since the last frame
    gScene->simulate(deltaTime);
    gScene->flushStream();
}


NxActor* CreateBox()
{
    // Set the box starting height to 3.5m so box starts off falling onto the ground
    NxReal boxStartHeight = 3.5;

 

    // Add a single-shape actor to the scene
    NxActorDesc actorDesc;
    NxBodyDesc bodyDesc;

 

    // The actor has one shape, a box, 1m on a side
    NxBoxShapeDesc boxDesc;
    boxDesc.dimensions.set(0.5,0.5,0.5);
    actorDesc.shapes.pushBack(&boxDesc);

    actorDesc.body = &bodyDesc;
    actorDesc.density = 10;
    actorDesc.globalPose.t = NxVec3(0,boxStartHeight,0);
    return gScene->createActor(actorDesc); 
}


NxActor* CreateGroundPlane(void)
{
    // Create a plane with default descriptor
    NxPlaneShapeDesc planeDesc;
    NxActorDesc actorDesc;
    actorDesc.shapes.pushBack(&planeDesc);
    return gScene->createActor(actorDesc);
}

void InitNx()
{
    // Create the physics SDK
    gPhysicsSDK = NxCreatePhysicsSDK(NX_PHYSICS_SDK_VERSION);
    if (!gPhysicsSDK)  return;
	gPhysicsSDK->setParameter(NX_SKIN_WIDTH, 0.01);

	// Set the debug visualization parameters
    gPhysicsSDK->setParameter(NX_VISUALIZATION_SCALE, 1);
    gPhysicsSDK->setParameter(NX_VISUALIZE_COLLISION_SHAPES, 1);
    gPhysicsSDK->setParameter(NX_VISUALIZE_ACTOR_AXES, 1);

    // Create the scene
    NxSceneDesc sceneDesc;
    sceneDesc.gravity              = gDefaultGravity;
    sceneDesc.bpType               = NX_BP_TYPE_SAP_SINGLE;
    gScene = gPhysicsSDK->createScene(sceneDesc);

	//now we create the material
	NxMaterial* defaultMaterial = gScene->getMaterialFromIndex(0);
    defaultMaterial->setRestitution(0.5);
    defaultMaterial->setStaticFriction(0.5);
    defaultMaterial->setDynamicFriction(0.5);

	//create ground plane
	groundPlane = CreateGroundPlane();
	box = CreateBox();
	UpdateTime();
	
	//start the first frame of animation
	if(gScene)
		StartPhysics();


}


NxVec3 ApplyForceToActor(NxActor* actor, const NxVec3& forceDir, const NxReal forceStrength)
{
    NxVec3 forceVec = forceStrength*forceDir;
    actor->addForce(forceVec);
    return forceVec;
}



class HelloD3DApp : public D3DApp
{
public:
	HelloD3DApp(HINSTANCE hInstance, std::string winCaption, D3DDEVTYPE devType, DWORD requestedVP);
	~HelloD3DApp();

	bool checkDeviceCaps();
	void onLostDevice();
	void onResetDevice();
	void updateScene(float dt);
	void drawScene();
	LRESULT msgProc(UINT msg, WPARAM wParam, LPARAM lParam);

private:

//	ID3DXFont* mFont;
};

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prevInstance,
				   PSTR cmdLine, int showCmd)
{
	// Enable run-time memory check for debug builds.
	#if defined(DEBUG) | defined(_DEBUG)
		_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
	#endif


	HelloD3DApp app(hInstance, "Hello Direct3D", D3DDEVTYPE_HAL, D3DCREATE_HARDWARE_VERTEXPROCESSING);
	gd3dApp = &app;
	
	return gd3dApp->run();
}

LRESULT HelloD3DApp::msgProc(UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch(msg)
	{
	case WM_KEYDOWN:
		int fwKeys;
		LPARAM keyData;
		fwKeys = (int)wParam;    // virtual-key code 
		keyData = lParam;          // key data 

		switch(fwKeys)
		{
			
		case VK_ESCAPE:
			PostQuitMessage(0);
			break;
			
		case VK_UP:
			gForceVec = ApplyForceToActor(box, NxVec3(+1, 0, 0), gForceStrength);

			break;
			
		case VK_LEFT:
			box->setGlobalPosition(NxVec3(0,5,0));

			break;
			
			default:
				break;
		}
	break;

	
	default:
			break;

	}
	return DefWindowProc(mhMainWnd , msg, wParam, lParam);
}


HelloD3DApp::HelloD3DApp(HINSTANCE hInstance, std::string winCaption, D3DDEVTYPE devType, DWORD requestedVP)
: D3DApp(hInstance, winCaption, devType, requestedVP)
{
	//init PhysX
	InitNx();

	srand(time_t(0));

	if(!checkDeviceCaps())
	{
		MessageBox(0, "checkDeviceCaps() Failed", 0, 0);
		PostQuitMessage(0);
	}

	MessageBox(0, "Hello", "hi", MB_OK);
}

HelloD3DApp::~HelloD3DApp()
{
    if (gScene)
    {
        GetPhysicsResults();  // Make sure to fetchResults() before shutting down
        gPhysicsSDK->releaseScene(*gScene);
    }
    if (gPhysicsSDK)  gPhysicsSDK->release();
}

bool HelloD3DApp::checkDeviceCaps()
{
	// Nothing to check.
	return true;
}

void HelloD3DApp::onLostDevice()
{

}

void HelloD3DApp::onResetDevice()
{
}

void HelloD3DApp::updateScene(float dt)
{
}

void HelloD3DApp::drawScene()
{
	HR(gd3dDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(255, 255, 255), 1.0f, 0));

	RECT formatRect;
	GetClientRect(mhMainWnd, &formatRect);

	HR(gd3dDevice->BeginScene());
	
	if(gScene && !bPause);
	{
		GetPhysicsResults();
	}



	HR(gd3dDevice->EndScene());
	HR(gd3dDevice->Present(0, 0, 0, 0));
}

Share this post


Link to post
Share on other sites
Advertisement
A quick look at that and I see that you have not created anything to draw. You've created the physics object fine though. Have a look at the Box On a Plane example again and look at it more closely, it uses OpenGL (specifically glut).

In the RenderCallBack() function (which is the game loop), right click on RenderActors(bShadows) and click Goto Definition, in that function do the same with DrawActor(actor, gSelectedActor, false), and again with DrawShape(shapes[nShapes], useShapeUserData), again with one of the functions from the switch statement. I that function the world and view matrices are setup, then another function is called to actually render that object.

If you're new to PhysX and D3D, then I suggest to learn one first then other. I suggest you to learn D3D first.

Alternatively dive into the lion's den and look at an example here http://www.codesampler.com/dx9src/dx9src_10.htm#dx9_novodex_simple
It uses an older version so won't compile, put it will start you off

Share this post


Link to post
Share on other sites
I agree with smally, both D3D and PhysX are complex enough that learning just one will keep you busy for a long time. I suggest learning D3D first, there's much of PhysX that won't make sense until you understand how 3D graphics work.

Share this post


Link to post
Share on other sites
Quote:
Original post by smally
In the RenderCallBack() function (which is the game loop), right click on RenderActors(bShadows) and click Goto Definition, in that function do the same with DrawActor(actor, gSelectedActor, false), and again with DrawShape(shapes[nShapes], useShapeUserData), again with one of the functions from the switch statement. I that function the world and view matrices are setup, then another function is called to actually render that object.

If you're new to PhysX and D3D, then I suggest to learn one first then other. I suggest you to learn D3D first.



kk. thanx guys.
i guess ill learn d3d first my d3d book is gonna be delivered next week.
ty guys.

Share this post


Link to post
Share on other sites
What is it you are wanting to use in your program? Direct3D or OpenGL.

To properly understand the PhysX tutorials, you need at least some basic knowledge of reading OpenGL, just google for some 'beginning OpenGL code'. I learnt OpenGL through http://nehe.gamedev.net/ . I can't remember if those tutorials use glut which is what PhysX tutorials use, if they don't make sure you have a quick look at that (a quick look is all that is required).

Once you can get your 3D application translating/rotating objects with a world matrix, then you can start to get PhysX working (PhysX provides the world matrix for you to use when drawing your objects).

Share this post


Link to post
Share on other sites
Quote:
Original post by smally
What is it you are wanting to use in your program? Direct3D or OpenGL.

I want to learn something that will get me started on a small game with physics, like a brick throwing game or sumthing.

Quote:

Once you can get your 3D application translating/rotating objects with a world matrix, then you can start to get PhysX working (PhysX provides the world matrix for you to use when drawing your objects).


I see...
So is that what a physics engine basically is?
Something that handles the position of the world objects?

Share this post


Link to post
Share on other sites
I a word, yes. PhysX calculates the position of you objects which can change pending on gravity, collision, friction etc, and shows this by allowing you to get it's world matrix, ready for you to draw.

If you just want to get a small and simple 'brick throwing' program working, try just modifying the tutorials to get your desired effect. In the tutorial's code, you only ever have to work with PhysX as the code hides away as much of the graphics as possible.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!