Download

How do I get pointer to PxScene in Nvidia PhysX API from Unreal Actor or component ?

Hello,

Can anyone help me in getting access to a pointer to PxScene which is part of the PhysX API. I’ve got an Actor and I want to do a PhysX raycast inside a sub step call back, but the raycast requires a pointer to PxScene. I’ve probably missed something obvious, but any help would be appreciated.

Cheers…

Welcome to the forums Mollymop!


**BodyInstance.cpp**

Check out BodyInstance.cpp!

There's so many examples there of how to get the PhysX scene and then do collision queries :)

check out



```


FBodyInstance::LineTrace(


```



Scene Locks

Dont forget to do scene read/write locks](https://developer.nvidia.com/sites/default/files/akamai/physx/Manual/DataAccess.html) so that your game code can be multi threaded!


**Scoped Scene Lock**

here's my favorite kind, scoped lock, notice the extra brackets explicitly containing the scene lock



```


PxRigidActor* PRigidActor = WeldParent ? WeldParent->GetPxRigidActor() : GetPxRigidActor();
if (PRigidActor)
{
	const PxTransform PBodyInstanceSpaceToTestSpace = U2PTransform(BodyInstanceSpaceToTestSpace);

        **//Extra Bracket here, containing the scene lock within this scope**
	// Get all the shapes from the actor
	{
		SCOPED_SCENE_READ_LOCK(PRigidActor->getScene());

		TArray<PxShape*, TInlineAllocator<8>> PShapes;
		PShapes.AddZeroed(PRigidActor->getNbShapes());
		int32 NumShapes = PRigidActor->getShapes(PShapes.GetData(), PShapes.Num());

		// Iterate over each shape
		TArray<struct FOverlapResult> TempOverlaps;
		for (int32 ShapeIdx = 0; ShapeIdx < PShapes.Num(); ShapeIdx++)
		{
			PxShape* PShape = PShapes[ShapeIdx];
			check(PShape);

			if (ShapeBoundToBody(PShape, this) == false)
			{
				continue;
			}

			// Calc shape global pose
			const PxTransform PLocalPose = PShape->getLocalPose();
			const PxTransform PShapeGlobalPose = PBodyInstanceSpaceToTestSpace.transform(PLocalPose);

			GET_GEOMETRY_FROM_SHAPE(PGeom, PShape);

			if (PGeom != NULL)
			{
				TempOverlaps.Reset();
				if (GeomOverlapMulti_PhysX(World, *PGeom, PShapeGlobalPose, TempOverlaps, TestChannel, Params, ResponseParams, ObjectQueryParams))
				{
					bHaveBlockingHit = true;
				}
				InOutOverlaps.Append(TempOverlaps);
			}
		}
	}
}


```




Enjoy!

:)

Rama

Code For you, Using UWorld* To Get PhysX Scene

Dear Community,

PhysX Wiki

I’ve added to my PhysX Wiki showing you some code and additional #includes to use to get access to the PhysX Scene in a way where you can do low level multi-threaded PhysX coding!

Rama’s Wiki on PhysX Coding in UE4
https://wiki.unrealengine.com/PhysX,Integrating_PhysX_Code_into_Your_Project#PhysX.7E_How_to_Get_the_PhysX_Scene


**PhysX + UE4 Code For You!**

Here's the code that you can use to get started in multi-thread friendly way, please see my higher post for more places to look for PhysX code in the UE4 code base.



```


//PhysX 
#include "PhysXIncludes.h"

//For the ptou conversions
#include "PhysicsPublic.h"	

//For Scene Locking using Epic's awesome helper macros like SCOPED_SCENE_READ_LOCK
#include "Runtime/Engine/Private/PhysicsEngine/PhysXSupport.h"

void SomeClass::SomeFunction(UWorld* World)
{
  check(World); 
  //if crash here world was not valid, needs to be investigated, did it come from a UObject?

  FPhysScene* PhysScene = World->GetPhysicsScene();
 
  // Scene Lock for Multi-Threading
  PxScene* SyncScene = PhysScene->GetPhysXScene(PST_Sync);
  SCOPED_SCENE_READ_LOCK(SyncScene);

  //now you can use the PhysX scene in multi-threaded fashion within this function context!
  //  -Rama
}


```



Note on UWorld*

Any Actor can easily just call GetWorld() to get you the UWorld* ptr



UWorld* World = YourActor->GetWorld();


UObjects can do the same but its not quite as reliable depending on the UObject


**Conclusion**

Have fun doing low level multi-threaded PhysX coding using the awesome Epic helper functions of PhysXSupport.h!

Enjoy!

Rama

Hello Rama,

You are a life saver. None of your stuff came up when googling, other than the sub stepping which I’m doing anyway.

Where I went wrong was figuring out what to pass to the GetPhysXScene(). Now you’ve cleared it up, all makes sense.

I’m in the process of using Unreal to develop my mountain bike game instead of Unity, hence digging into the PhysX API. If you are interested here is our last video https://vimeo.com/104773003 use flowdh as the password. This was in Unity and aiming for mobile. Now target platform is PC/Steam, hence the switch to Unreal.

Again, cheers.