Skip to main content
GameDev.net gamedev.net
🔒 Locked

Help with rendering physics.

Started by Enerjak Jan 25, 2008 at 3:16 PM 1 replies 850+ views
Original Post
Enerjak
Enerjak
Ok, so i got DirectX to render with a premade window. Now i'm trying to get PhysX to work. for the most part, everything compiles fine but i don't know what to add to make the actors render. I got this code set up but when it runs all i see is the screen color that i cleared Direct3D to.

#include "stdafx.h"

//Globals for using the PhysX SDK
NxPhysicsSDK* g_pPhysics;
NxScene* g_pScene;

//Set the gravity.
NxVec3 DefaultGravity(0,-9.8,0);

NxActor* groundPlane = NULL;
NxActor* Box = NULL;

int PhyInitialize()
{
	g_pPhysics	=	NxCreatePhysicsSDK(NX_PHYSICS_SDK_VERSION);

	if(!g_pPhysics)
	{
		MessageBoxA(NULL,"The PhysX SDK could not be initial;ized","Sorry",MB_OK);
		return 0;
	}

	// Set the physics parameters
	g_pPhysics->setParameter(NX_SKIN_WIDTH, 0.01);
	// Set the debug visualization parameters
	g_pPhysics->setParameter(NX_VISUALIZATION_SCALE, 1);
	g_pPhysics->setParameter(NX_VISUALIZE_COLLISION_SHAPES, 1);
	g_pPhysics->setParameter(NX_VISUALIZE_ACTOR_AXES, 1);

	NxSceneDesc SceneDesc;
	SceneDesc.gravity	= DefaultGravity;
	SceneDesc.simType	= NX_SIMULATION_SW;
	g_pScene = g_pPhysics->createScene(SceneDesc);	
	
	NxMaterial* defaultMaterial = g_pScene->getMaterialFromIndex(0);
	defaultMaterial->setRestitution(0.1);
	defaultMaterial->setDynamicFriction(0.5);
	defaultMaterial->setStaticFriction(0.5);
	
	if(g_pScene)
	  StartPhysics();
		RenderActors();

	UpdateTime();
	return 1;
}

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

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 g_pScene->createActor(actorDesc);
}

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
g_pScene->simulate(deltaTime);
g_pScene->flushStream();
}

void RenderActors()
{
	// Render all the actors in the scene
	NxU32 nbActors = g_pScene->getNbActors();
	NxActor** actors = g_pScene->getActors();
}

help with this will be appreciated and used in future projects. thank you for all the help i might get.
SanityAssassin
SanityAssassin
You're not rendering anything in your RenderActors() function.

If you're using PhysX, you can look at their examples to see how they grab data from the NXActors and render it.
Enerjak
Enerjak
Ah, i see they use OpenGL to make the shapes. I could do the same with Direct3D since i need to make the plane and box in order to render it in the first place. thanks.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.