Sign in to follow this  
Viperrr

OpenGL DirectX and PhysX

Recommended Posts

Does any here have experience using AGEIA PhysX with DirectX? And specifically its character controller? I am looking for a tutorial on how to use the PhysX character controller with DirectX, to create a standard First Person Shooter control. The samples that come with the documentation of PhysX are for openGL, and are not explained very well. I have never used PhysX before, can someone help me get started please?

Share this post


Link to post
Share on other sites
Ok well, how about I start simple: creating a plane, and displaying it onscreen (for debugging)

This is part of what I have now:
[source lang=cpp]
...
pGroundPlane = CreateGroundPlane();
...

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



How would I render this plane onscreen? Using directX..

Share this post


Link to post
Share on other sites
I see alot of people are viewing this topic.

If you, like me and 7thAvatar, would like to see a tutorial on charactercontrol with PhysX + DirectX, drop a line here!

If there is enough interest for it, maybe someone will be kind enough to make us one :)

Share this post


Link to post
Share on other sites
Perhaps important to point out (and I sense this is the point of confusion): the mesh or shape you supply to PhysX has absolutely nothing to do with the mesh that you render. They are completely different meshes who's data is stored in different places. In general, the mesh you supply to PhysX is a really simplified version of your actual mesh but in practice they share no data and can thus be considered "completely different".

So in your code you've created an NxPlaneShape but you have nothing to render because you still need to create an actual mesh to render. i.e. some set of vertices, indices, uv coordinates, textures, shaders and whatnot for DirectX to handle.

To put another way, if you don't already know how to create objects and draw them in DirectX (i.e. have an already working application that does this) then do not try to use these 2 APIs together. Start by building a basic "game" just using DirectX: have some objects that move around under your own control which are drawn to the screen.

Otherwise, if I'm off the mark I'm not entirely sure what the problem is. So here's another stab.

Think of PhysX as the "where stuff is" manager. DirectX is just the "draw stuff" manager.

Usually you'd draw something like this (in API agnostic pseudo-code)


MyAPIPushMatrix( myThingsOrientationMatrix );

DrawThing();

MyAPIPopMatrix();




Now if you're not using PhysX then:


myThingsOrientationMatrix == myThing.getMatrix();




however, you're using PhysX to figure out where stuff is so


myThingsOrientationMatrix = myThing->myNxActor.getMatrix();

//replace getMatrix() with the actual API call. too lazy to look it up right now




-me

Share this post


Link to post
Share on other sites
Palidine,

I know how to draw things with DirectX. Infact, I have the graphics part of my engine down; I can load and display meshes, using pixelshaders.

So my engine is at the point where I have a building (which consists of some meshes) and I can walk through it with WASD + mouse look. You know, the controls pretty much all FPS use. (I use DirectInput to do it)

But like you said, Direct3D has nothing to do with collision detection, it just draws. Now I need a way to keep the player from walking through walls, so I looked around and found PhysX :)

I also understand that PhysX meshes are completely seperate from Direct3D meshes. I understand what you mean by "where stuff is" manager. I knew that.

However, looking at the samples for openGL, I noticed they actually draw the PhysX meshes to the screen, for debug purposes. That way I could line up the PhysX meshes with the Direct3D meshes.

The reason I wanted to draw the plane now, is this:
If it shows up on screen, it mean Im doing something right! ;)

Are you saying this is not possible?

Share this post


Link to post
Share on other sites
Quote:
Original post by Viperrr
Palidine,

I know how to draw things with DirectX. Infact, I have the graphics part of my engine down; I can load and display meshes, using pixelshaders.

So my engine is at the point where I have a building (which consists of some meshes) and I can walk through it with WASD + mouse look. You know, the controls pretty much all FPS use. (I use DirectInput to do it)

But like you said, Direct3D has nothing to do with collision detection, it just draws. Now I need a way to keep the player from walking through walls, so I looked around and found PhysX :)

I also understand that PhysX meshes are completely seperate from Direct3D meshes. I understand what you mean by "where stuff is" manager. I knew that.

However, looking at the samples for openGL, I noticed they actually draw the PhysX meshes to the screen, for debug purposes. That way I could line up the PhysX meshes with the Direct3D meshes.

The reason I wanted to draw the plane now, is this:
If it shows up on screen, it mean Im doing something right! ;)

Are you saying this is not possible?


No they don't. In the demo code they build simple geom shapes with OpenGL calls. If you start with DrawActor() and go deep enough their are a whole bunch of utility functions that draw shapes in OpenGL.

The demos uses the PhysX internal list of objects as the render list so it looks like they are rendering the actual PhysX object. My entities have a pointer to their physics body and I keep my own list of rendering objects. Then I poll the entities physics object world matrix as the position to place the D3D mesh. Same as Palidine pseudo code.

Share this post


Link to post
Share on other sites
The PhysX physics simulation is SEPARATE from the DirectX Rendering API. To use them together, you want to supply some data from your scene (original matrix positions, bounding volume size, a simplified set of mesh data etc.) along with the physical attributes of the things in your scene to the physX simulation via NxActors. The simulation then runs and updates the actors world matrices - thats essentially all it does. Next you take the world matrix of the NxActor (NxActor->getMatrix()) and apply it before rendering your DirectX representation of the object. Remember the two things are separate!

Share this post


Link to post
Share on other sites
The PhysX SDK comes with an interactive debugger ... it's a long time since I've used it but I remember it being very helpful. You can also get PhysX to spit out buffers full of vertices for rendering debug shapes in-game. Rendering these will give you a visual representation of the actual PhysX actors (including character controller) within your scene. It's all in the docs :)

Share this post


Link to post
Share on other sites
Quote:
Original post by teutonicus
The PhysX SDK comes with an interactive debugger ... it's a long time since I've used it but I remember it being very helpful. You can also get PhysX to spit out buffers full of vertices for rendering debug shapes in-game. Rendering these will give you a visual representation of the actual PhysX actors (including character controller) within your scene. It's all in the docs :)


Yes thank you, that is what I was talking about :) I understand PhysX and DirectX are seperate, but if I know how to make PhysX "spit out" these buffers, I can use D3D to render them!

Why are you guys talking about matrices for the positioning of meshes tho? The documentation mentions a simple function like NxActor->getPos (or something similar). This would just return x,y and z.

Well regardless of all of this, a tutorial on how to setup up the charactercontroller would still be helpfull :)

Share this post


Link to post
Share on other sites
Quote:
Why are you guys talking about matrices for the positioning of meshes tho? The documentation mentions a simple function like NxActor->getPos (or something similar). This would just return x,y and z.


all transforms are applied using matrices in directX (and openGL for that matter) - you can either get the matrix directly from physX returning 16 floats which can be applied directly to your directX device to represent position AND rotation... or you can use the get-position / get-rotation functions which will then return NxVector3 vectors which then end up having to be packed into a matrix anyway (by your program at some level) before they can be applied to your DirectX geometry. thats why.

Share this post


Link to post
Share on other sites
Question:

My app DOES compile, but crashes immediately, why?

[source lang=cpp]
NxControllerManager* pManager;
NxController* playerController;

initPhysX()
{
<...>

pManager = NxCreateControllerManager(NULL);
NxCapsuleControllerDesc desc;
desc.height = 1.5f;
desc.radius = 0.5f;
playerController = pManager->createController(pScene, desc);
}



When I comment out the last line it doesnt crash anymore. What am I doing wrong?

Share this post


Link to post
Share on other sites
That tutorial is missing some pages. In the menu it says chapter 4 is about charactercontrol, but there is no chapter 4 :S

Also it seems to be about the irrlicht engine. I dont want to use any 3rd party engine stuff. Just DirectX, PhysX and C++

Hasn't anybody here ever made a first person shooter?

Share this post


Link to post
Share on other sites
Hi, it seems that not many people its using directx and physx together,
but in fact its quite simple, the "only" thing you need to do, its apply
the actor matrix directly to your desired mesh of that actor.

Now, i could not get character controller to work, its a lot o things and you
are limited to use a capsule, instead i did my own way to controll my actor
without use that thing, and with the feature of using a convex, not a cube
nor a capsule.

NxVec3 tmp = fpsactor->getGlobalPosition();
tmp.z += TIMECONSTANT*cos(angle);
tmp.x += TIMECONSTANT*sin(angle);
fpsactor->setGlobalPosition(tmp);

In this code, angle its your rotation on Y Axis, now you can move
your actor without using forces, and the matrix pose of the fpsactor
of this code should be applied to your mesh of directx directly.

I hope that can help, and sorry my grammar =/

Share this post


Link to post
Share on other sites
you should maybe read the documentation + know how to debug::::!!!!!

NxControllerManager* pManager;
NxController* playerController;

initPhysX()
{
<...>

pManager = NxCreateControllerManager(NULL);
NxCapsuleControllerDesc desc;
desc.height = 1.5f;
desc.radius = 0.5f;
playerController = pManager->createController(pScene, desc);
}


pManager = NxCreateControllerManager(NULL); //after this line pManager is Zero for sure cos NxCreateControllerManager needs a NxUserAllocator

playerController = pManager->createController(pScene, desc); //since pManager is zero this will cause a exception.

The Solution::

do not give NULL to NxCreateControllerManager, derive from NxUserAllocator and pass the derived class check:::

AgeiaPhysXDocumentation/Guide/Basics/MemoryManagement how to do that

Share this post


Link to post
Share on other sites
I write above code for drawing PhysX actors. Maybe useful for you.

//FVF structure and declaration
struct POINTVERTEX{
D3DXVECTOR3 vPosition;
D3DCOLOR color;
static const DWORD FVF =(D3DFVF_XYZ|D3DFVF_DIFFUSE);
};

typedef POINTVERTEX LINEVERTEX;




HRESULT CBTPhysX::DebugRender(){
if(lpDevice==NULL) return E_FAIL;
const NxDebugRenderable *data=m_Scene->getDebugRenderable();
unsigned int NbLines = data->getNbLines();

HRESULT hr;
hr=lpDevice->CreateVertexBuffer( NbLines*2*sizeof(LINEVERTEX),
D3DUSAGE_WRITEONLY,LINEVERTEX::FVF,D3DPOOL_MANAGED,&m_pVB,NULL );

if(hr!=S_OK){
MessageBox(NULL,"PhysXDebugRender vertex bufferi olu&#351;turulurken hata olu&#351;tu","",MB_OK);
return hr;
exit(0);
}

m_pVB->Lock(0,0,(void**)&pVertices,0);
if(NbLines)
{
const NxDebugLine* Lines = data->getLines();
static int l;
for(int k=0;k<NbLines;k++){
pVertices[l].vPosition=D3DXVECTOR3(Lines->p0.x,Lines->p0.y,Lines->p0.z);
pVertices[l].color=Lines->color;
pVertices[l+1].vPosition=D3DXVECTOR3(Lines->p1.x,Lines->p1.y,Lines->p1.z);
pVertices[l+1].color=Lines->color;
l+=2;
Lines++;
}
l=0;
}
m_pVB->Unlock();

lpDevice->SetStreamSource(0,m_pVB,0,sizeof(LINEVERTEX));
lpDevice->SetTexture(0,NULL); //di&#287;er çizimlerden gelen doku atalamalar&#305;n&#305; kald&#305;r yoksa çizgilerin vertex renkleri görünmeyecek.
lpDevice->SetFVF(LINEVERTEX::FVF);
lpDevice->SetRenderState( D3DRS_LIGHTING, FALSE );//Çizgilerin renklerine &#305;&#351;&#305;&#287;&#305;n etki etmesini engelle
lpDevice->DrawPrimitive(D3DPT_LINELIST,0,NbLines);
lpDevice->SetRenderState( D3DRS_LIGHTING, TRUE );
m_pVB->Release();

return S_OK;
}


Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Announcements

  • Forum Statistics

    • Total Topics
      628395
    • Total Posts
      2982437
  • Similar Content

    • By test opty
      Hi all,
       
      I'm starting OpenGL using a tut on the Web. But at this point I would like to know the primitives needed for creating a window using OpenGL. So on Windows and using MS VS 2017, what is the simplest code required to render a window with the title of "First Rectangle", please?
       
       
    • By DejayHextrix
      Hi, New here. 
      I need some help. My fiance and I like to play this mobile game online that goes by real time. Her and I are always working but when we have free time we like to play this game. We don't always got time throughout the day to Queue Buildings, troops, Upgrades....etc.... 
      I was told to look into DLL Injection and OpenGL/DirectX Hooking. Is this true? Is this what I need to learn? 
      How do I read the Android files, or modify the files, or get the in-game tags/variables for the game I want? 
      Any assistance on this would be most appreciated. I been everywhere and seems no one knows or is to lazy to help me out. It would be nice to have assistance for once. I don't know what I need to learn. 
      So links of topics I need to learn within the comment section would be SOOOOO.....Helpful. Anything to just get me started. 
      Thanks, 
      Dejay Hextrix 
    • By mellinoe
      Hi all,
      First time poster here, although I've been reading posts here for quite a while. This place has been invaluable for learning graphics programming -- thanks for a great resource!
      Right now, I'm working on a graphics abstraction layer for .NET which supports D3D11, Vulkan, and OpenGL at the moment. I have implemented most of my planned features already, and things are working well. Some remaining features that I am planning are Compute Shaders, and some flavor of read-write shader resources. At the moment, my shaders can just get simple read-only access to a uniform (or constant) buffer, a texture, or a sampler. Unfortunately, I'm having a tough time grasping the distinctions between all of the different kinds of read-write resources that are available. In D3D alone, there seem to be 5 or 6 different kinds of resources with similar but different characteristics. On top of that, I get the impression that some of them are more or less "obsoleted" by the newer kinds, and don't have much of a place in modern code. There seem to be a few pivots:
      The data source/destination (buffer or texture) Read-write or read-only Structured or unstructured (?) Ordered vs unordered (?) These are just my observations based on a lot of MSDN and OpenGL doc reading. For my library, I'm not interested in exposing every possibility to the user -- just trying to find a good "middle-ground" that can be represented cleanly across API's which is good enough for common scenarios.
      Can anyone give a sort of "overview" of the different options, and perhaps compare/contrast the concepts between Direct3D, OpenGL, and Vulkan? I'd also be very interested in hearing how other folks have abstracted these concepts in their libraries.
    • By aejt
      I recently started getting into graphics programming (2nd try, first try was many years ago) and I'm working on a 3d rendering engine which I hope to be able to make a 3D game with sooner or later. I have plenty of C++ experience, but not a lot when it comes to graphics, and while it's definitely going much better this time, I'm having trouble figuring out how assets are usually handled by engines.
      I'm not having trouble with handling the GPU resources, but more so with how the resources should be defined and used in the system (materials, models, etc).
      This is my plan now, I've implemented most of it except for the XML parts and factories and those are the ones I'm not sure of at all:
      I have these classes:
      For GPU resources:
      Geometry: holds and manages everything needed to render a geometry: VAO, VBO, EBO. Texture: holds and manages a texture which is loaded into the GPU. Shader: holds and manages a shader which is loaded into the GPU. For assets relying on GPU resources:
      Material: holds a shader resource, multiple texture resources, as well as uniform settings. Mesh: holds a geometry and a material. Model: holds multiple meshes, possibly in a tree structure to more easily support skinning later on? For handling GPU resources:
      ResourceCache<T>: T can be any resource loaded into the GPU. It owns these resources and only hands out handles to them on request (currently string identifiers are used when requesting handles, but all resources are stored in a vector and each handle only contains resource's index in that vector) Resource<T>: The handles given out from ResourceCache. The handles are reference counted and to get the underlying resource you simply deference like with pointers (*handle).  
      And my plan is to define everything into these XML documents to abstract away files:
      Resources.xml for ref-counted GPU resources (geometry, shaders, textures) Resources are assigned names/ids and resource files, and possibly some attributes (what vertex attributes does this geometry have? what vertex attributes does this shader expect? what uniforms does this shader use? and so on) Are reference counted using ResourceCache<T> Assets.xml for assets using the GPU resources (materials, meshes, models) Assets are not reference counted, but they hold handles to ref-counted resources. References the resources defined in Resources.xml by names/ids. The XMLs are loaded into some structure in memory which is then used for loading the resources/assets using factory classes:
      Factory classes for resources:
      For example, a texture factory could contain the texture definitions from the XML containing data about textures in the game, as well as a cache containing all loaded textures. This means it has mappings from each name/id to a file and when asked to load a texture with a name/id, it can look up its path and use a "BinaryLoader" to either load the file and create the resource directly, or asynchronously load the file's data into a queue which then can be read from later to create the resources synchronously in the GL context. These factories only return handles.
      Factory classes for assets:
      Much like for resources, these classes contain the definitions for the assets they can load. For example, with the definition the MaterialFactory will know which shader, textures and possibly uniform a certain material has, and with the help of TextureFactory and ShaderFactory, it can retrieve handles to the resources it needs (Shader + Textures), setup itself from XML data (uniform values), and return a created instance of requested material. These factories return actual instances, not handles (but the instances contain handles).
       
       
      Is this a good or commonly used approach? Is this going to bite me in the ass later on? Are there other more preferable approaches? Is this outside of the scope of a 3d renderer and should be on the engine side? I'd love to receive and kind of advice or suggestions!
      Thanks!
    • By nedondev
      I 'm learning how to create game by using opengl with c/c++ coding, so here is my fist game. In video description also have game contain in Dropbox. May be I will make it better in future.
      Thanks.
  • Popular Now