• Advertisement
Sign in to follow this  

PhysX Issues

This topic is 3917 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

I freaking swear PhysX API hates me. I can only get it working right if I don't use it in a class. But if I try to wrap it in its own class. It doesn't show me no love. The ball drops then the Box drops and doesn't move or bounce. And I have the default material all set to 0.5f. Doesn't help. PhysX wrapper
#include "CPhysX.h"

CPhysX::CPhysX()
{
	m_nxPhysXSDK = NULL;
	m_nxScene = NULL;
}

CPhysX::~CPhysX()
{
	if(!m_nxPhysXSDK)
	{
		if(!m_nxScene)
		{
			m_nxPhysXSDK->releaseScene(*m_nxScene);
		}
		m_nxPhysXSDK->release();
	}
}

bool CPhysX::Initialize(NxVec3 gravity, NxReal dynamicFriction, NxReal restitution, NxReal staticFriction )
{
	m_nxPhysXSDK = NxCreatePhysicsSDK(NX_PHYSICS_SDK_VERSION);
	if(!m_nxPhysXSDK)
		return false;
	m_nxPhysXSDK->setParameter(NX_SKIN_WIDTH, 0.05f);

	NxSceneDesc nxSceneDesc;
	nxSceneDesc.gravity = gravity;
	nxSceneDesc.simType = NX_SIMULATION_HW;
	m_nxScene = m_nxPhysXSDK->createScene(nxSceneDesc);
	if(!m_nxScene)
	{
		nxSceneDesc.gravity = gravity;
		nxSceneDesc.simType = NX_SIMULATION_SW;
		m_nxScene = m_nxPhysXSDK->createScene(nxSceneDesc);
		if(m_nxScene)
			return false;
	}

	NxMaterial *nxDefault = m_nxScene->getMaterialFromIndex(0);
	nxDefault->setRestitution(restitution);
	nxDefault->setDynamicFriction(dynamicFriction);
	nxDefault->setStaticFriction(staticFriction);
	return true;
}

void CPhysX::StartPhysX(float elapsedTime)
{
	m_nxScene->simulate(elapsedTime);
}

void CPhysX::EndPhysX()
{
	m_nxScene->flushStream();
	m_nxScene->fetchResults(NX_RIGID_BODY_FINISHED, true);
}

NxActor *CPhysX::CreatePhysXBox(NxVec3 linearVelocity, NxVec3 size, NxVec3 position)
{
	NxBodyDesc nxBoxBodyDesc;
	nxBoxBodyDesc.angularDamping = 0.5f;
	nxBoxBodyDesc.linearVelocity = linearVelocity;

	NxBoxShapeDesc nxBoxShapeDesc;
	nxBoxShapeDesc.dimensions = size;
		
	NxActorDesc nxBoxActorDesc;
	nxBoxActorDesc.shapes.pushBack(&nxBoxShapeDesc);
	nxBoxActorDesc.body = &nxBoxBodyDesc;
	nxBoxActorDesc.density = 10.0f;
	nxBoxActorDesc.globalPose.t = position;

	return m_nxScene->createActor(nxBoxActorDesc);
}

NxActor *CPhysX::CreatePhysXPlane(NxVec3 size)
{
	NxPlaneShapeDesc nxPlaneShapeDesc;
	NxActorDesc nxPlaneActorDesc;
	nxPlaneActorDesc.shapes.pushBack(&nxPlaneShapeDesc);

	return m_nxScene->createActor(nxPlaneActorDesc);
}

NxActor *CPhysX::CreatePhysXSphere(NxVec3 linearVelocity, float radius, NxVec3 position)
{
	NxBodyDesc nxSphereBodyDesc;
	nxSphereBodyDesc.angularDamping = 0.5f;
	nxSphereBodyDesc.linearVelocity = linearVelocity;

	NxSphereShapeDesc nxSphereShapeDesc;
	nxSphereShapeDesc.radius = radius;

	NxActorDesc nxSphereActorDesc;
	nxSphereActorDesc.body = &nxSphereBodyDesc;
	nxSphereActorDesc.density = 10.0f;
	nxSphereActorDesc.globalPose.t = position;
	nxSphereActorDesc.shapes.pushBack(&nxSphereShapeDesc);

	return m_nxScene->createActor(nxSphereActorDesc);
}

Main code
#include "CFX.h"

CFX::CFX()
{
    m_window = NULL;
	m_showInstructions = false;

	m_physX = new CPhysX;
	m_nxSphere = NULL;
	m_nxBall = NULL;

	m_planeMesh = NULL;
	m_sphereMesh = NULL;
	m_ballMesh = NULL;
}

CFX::~CFX()
{
	delete m_physX;
	delete m_window;
}

void CFX::SetFramework(CWindow* window)
{
    m_window = window;
}

bool CFX::Initialize()
{
    return true;
}

void CFX::OnCreateDevice(Direct3DDevice9 d3ddev)
{
	m_fpsFont.Initialize(d3ddev, "Arial", 12, true, false);
	m_instructionsFont.Initialize(d3ddev, "Arial", 12, true, false);

	m_physX->Initialize(NxVec3(0.0f, -20.0f, 0.0f), 0.5f, 0.5f, 0.5f);
	D3DXCreateBox(d3ddev, 40.0f, 1.0f, 40.0f, &m_planeMesh, NULL);
	D3DXCreateSphere(d3ddev, 1.0f, 20, 20, &m_sphereMesh, NULL);
	D3DXCreateBox(d3ddev, 1.0f, 1.0f, 1.0f, &m_ballMesh, NULL);
}

void CFX::OnResetDevice(Direct3DDevice9 d3ddev)
{
	m_fpsFont.OnResetDevice();
	m_instructionsFont.OnResetDevice();

	D3DLIGHT9 light;
	ZeroMemory(&light, sizeof(light));
	light.Type = D3DLIGHT_DIRECTIONAL;
	light.Direction = D3DXVECTOR3(0.5f, -0.8f, 0.7f);
	light.Diffuse.r = 0.0f;
	light.Diffuse.g = 0.5f;
	light.Diffuse.b = 1.0f;
	light.Diffuse.a = 1.0f;

	d3ddev->SetLight(0, &light);
	d3ddev->LightEnable(0, true);

	D3DMATERIAL9 material;
	ZeroMemory(&material, sizeof(D3DMATERIAL9));

	material.Diffuse.r = material.Ambient.r = 1.0f;
	material.Diffuse.g = material.Ambient.g = 1.0f;
	material.Diffuse.b = material.Ambient.b = 1.0f;
	material.Diffuse.a = material.Ambient.a = 1.0f;

	d3ddev->SetMaterial(&material);

	d3ddev->SetRenderState(D3DRS_LIGHTING, true);
	d3ddev->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_XRGB(30, 30, 30));

	D3DXVECTOR3 Look(-20.0f, 15.0f, 30.0f);
	D3DXVECTOR3 At(0.0f, 0.0f, 0.0f);
	D3DXVECTOR3 Up(0.0f, 1.0f, 0.0f);
	D3DXMATRIXA16 view;
	D3DXMatrixLookAtLH(&view, &Look, &At, &Up);
	d3ddev->SetTransform(D3DTS_VIEW, &view);
	D3DXMATRIXA16 projection;
	float aspect = static_cast<float>(m_window->GetWidth()) / static_cast<float>(m_window->GetHeight());
	D3DXMatrixPerspectiveFovLH(&projection, static_cast<float>(D3DX_PI / 4.0f), aspect, 0.1f, 1000.0f);
	d3ddev->SetTransform(D3DTS_PROJECTION, &projection);

	d3ddev->SetRenderState(D3DRS_FILLMODE, m_window->GetFillMode());
	d3ddev->SetRenderState(D3DRS_SHADEMODE, D3DSHADE_GOURAUD);

	m_physX->CreatePhysXPlane(NxVec3(40.0f, 1.0f, 40.0f));
	m_nxSphere = m_physX->CreatePhysXSphere(NxVec3(0.0f, 0.0f, 0.0f), 1.0f, NxVec3(0.0f, 5.0f, 0.0f));
	m_nxBall = m_physX->CreatePhysXBox(NxVec3(0.0f, 0.0f, 0.0f), NxVec3(1.0f, 1.0f, 1.0f), NxVec3(0.0f, 10.0f, 0.0f));
}

void CFX::OnLostDevice()
{
	m_fpsFont.OnLostDevice();
	m_instructionsFont.OnLostDevice();
}

void CFX::OnDestroyDevice()
{
}

void CFX::OnUpdateFrame(Direct3DDevice9 d3ddev, float elapsedTime)
{
	UNREFERENCED_PARAMETER(d3ddev);
	UNREFERENCED_PARAMETER(elapsedTime);
}

void CFX::OnRenderFrame(Direct3DDevice9 d3ddev, float elapsedTime)
{
	m_physX->StartPhysX(elapsedTime);
	stringstream temp;
	temp << "FPS: " << int(m_window->GetFPS()) 
		 << "\nWindowWidth: " << m_window->GetWidth()
		 << "\nWindowHeight: " << m_window->GetHeight();
    d3ddev->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0); 
    d3ddev->BeginScene();
	D3DXMATRIX world;
	D3DXMatrixIdentity(&world);
	world._42 = -0.5f;

	d3ddev->SetTransform(D3DTS_WORLD, &world);
	m_planeMesh->DrawSubset(0);

	m_nxSphere->getCMassGlobalPose().getColumnMajor44(world);
	d3ddev->SetTransform(D3DTS_WORLD, &world);
	m_sphereMesh->DrawSubset(0);

	m_nxBall->getCMassGlobalPose().getColumnMajor44(world);
	d3ddev->SetTransform(D3DTS_WORLD, &world);
	m_ballMesh->DrawSubset(0);

	m_fpsFont.PrintString(temp.str(), 0, 0, D3DCOLOR_XRGB(255, 0, 0), NULL, 40, 40);
	if(m_showInstructions)
		m_instructionsFont.PrintString("Quit: ESC\nWireframe: F1", 0, 45, D3DCOLOR_XRGB(255, 255, 0), NULL, 50, 50);
	else
		m_instructionsFont.PrintString("Press F2 for instructions", 0, 45, D3DCOLOR_XRGB(255, 255, 0), NULL, 50, 50);
    d3ddev->EndScene();
#if _ISVISTA
	d3ddev->PresentEx(NULL, NULL, NULL, NULL, 0);
#else
    d3ddev->Present(NULL, NULL, NULL, NULL);
#endif
	m_physX->EndPhysX();
}

void CFX::OnKeyDown(WPARAM wParam)
{
    switch(wParam)
    {
    case VK_ESCAPE:
        PostQuitMessage(0);
        break;
    case VK_F1:
        if(m_window != NULL)
            m_window->ToggleWireframe();
        break;
	case VK_F2:
		if(!m_showInstructions)
			m_showInstructions = true;
		else
			m_showInstructions = false;
		break;
    }
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	UNREFERENCED_PARAMETER(hPrevInstance);
	UNREFERENCED_PARAMETER(lpCmdLine);
	UNREFERENCED_PARAMETER(nCmdShow);

    CFX* fx = new CFX;
    CWindow* window = new CWindow(fx);

    fx->SetFramework(window);

    if(!fx->Initialize())
	{
		LogInfo("Failed to initialize FX.")
        return 0;
	}
#if _ISVISTA
	if(!window->Initialize("D3D9Ex By Kiryn \"Mode: Windows Vista\"", hInstance, 640, 480))
#else
	if(!window->Initialize("D3D9 By Kiryn \"Mode: Windows XP SP2\"", hInstance, 640, 480))
#endif
	{
		LogInfo("Failed to intialize window.")
        return 0;
	}

    window->Run();
    
    delete fx;

    return 0;
}

Share this post


Link to post
Share on other sites
Advertisement
I've looked through your code a few times, and don't see anything wrong with it. Are you configuring any of the properties differently in the class version? Why don't you try setting up a report to see if any collisions are ever detected?

Share this post


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

  • Advertisement