1 / 33

95.4501

95.4501. Collision Detection Via Nvidia’s PhysX. Definition. Collision detection is the act of determining what is being hit for the purposes of providing suitable reaction behavior .

taini
Télécharger la présentation

95.4501

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 95.4501 Collision Detection Via Nvidia’sPhysX

  2. Definition • Collision detection is the act of determining what is being hitfor the purposes of providing suitable reaction behavior. • To have objects (especially the player) navigate the game world without going through other objects; need instantaneous feedback. • To be able to perform visibility queries (is sun visible for lens flare, is player visible to an AI to cause attack, objects encountered by bullet so a scorch mark can be made).

  3. Definition • Physics simulation is the act of computing the behavior that should result from a set of properties that include the shape of an object, its mass, it translational and rotational velocities, and the other objects in its neighborhood that can constrain the movement of the object... PhysX provides behavior simulation from increasingly complex objects built out of sphere, cube, and capsule shapes along with raysweeping and sphere sweeping queries...

  4. PhysX • Free software that provides both capabilities. • Requires the user to build collision detection shapes for his world and to use a query API for immediate control purposes and a simulation enginefor more autonomousbehavior. • The PhysX engine is a parallel multiprocessor capable engine that requires the use of ONEsimple synchronization point to use effectively.

  5. Preliminaries • Need to have access to online PhysX SDK documentation (though we will review it here). • Need access to specific source libraries, DLLs, and LIBs. • Provides a not-so-easy to figure out ALL-ENCOMPASING DEMO for perusal... (too much intertwining of renderer code with physics code) • Begin perusal in file “PhysXSample.cpp”

  6. Generic Design physics engine scene physics actors collision shapes transform geometric entities friction materials mass density PxPhysics* sdk; PxCooking* cooking; PxScene* scene; PxMaterial* material; cooker we call it physicsSystem

  7. Creating / Deleting Physics Engine • Can peruse a subset of the sample code in “PhysXSample::onInit ()” and “PhysXSample::onShutdown ()” that creates/deletes appropriate components. • Important parts are the following: • A 16-byte boundary space allocator • An error callbackobject • An extension library for mass and inertia and the ability to obtain a cooker on demand • A scene object with complicated components associated with threads and CUDA. We’ll provide a physics manager that sets all this up...

  8. Once you have a physics system • You can make it perform simulations in parallel with something that is not going to change the state of your objects... if (physicsManager->scene != NULL) physicsManager->scene->simulate (DT); game->draw (); if (physicsManager->scene != NULL) physicsManager->scene->fetchResults (true); wait until simulation done

  9. Most Important Object: Actors • Actors are the only objects that can be associated with positional information among other things. • Instead of transformations, physX uses quaternions implemented in a class called PxTransform... and calls them poses in the demos. We provide a handful of routines for converting back and forth WITHOUT HAVING TO KNOW HOW THEY WORK...

  10. Conversion Routines //Convenience conversion functions... inline PxTransformasTransform (Transformation &transformation) { return PxTransform (*((PxMat44 *) &transformation));} inline Transformation asTransformation (PxTransform &transform) { return *((Transformation *) &PxMat44 (transform));} inline Point asPoint (PxVec3 &point) {return Point (point.x, point.y, point.z);} inline PxVec3 asVec3 (Point &point) {return PxVec3 (point.x, point.y, point.z);} inline PxTransformtransformTranslatedTo (PxTransform &transform, Point &point) { return PxTransform (asVec3 (point), transform.q);}

  11. Game Objects • Game objects, in general, need at least A transformation to position it A display shape for drawing it A collision shape for moving it. • PhysX by contrast deals with the collision aspect from actors and also needs the equivalent of a transformation to do this... It does not support user objects directly. • So the easiest way to deal with it is have the game object access the transformation information from the physics actor rather than duplicate it...

  12. Actor Types Depend on their Collision Shapes • Actor types • Static (unmovable) • Dynamic (movable) • Staticonlycollision shapesinclude • infinite planes (demos only) • terrain (needs a cooker to finalize it) • triangle meshes (a polygon soup) • Static or dynamic collision shapesinclude • Spheres, boxes, capsules, and convex meshes (but not encouraged to use latter)

  13. Shapes Need Friction Materials • Since shapes dictate what rubs when movement occurs, they also need information about friction; i.e., friction materials... • static friction (0 slippery, 1  sticky) • dynamic friction (0  slippery, 1  sticky), • coefficient of restitution (0  stick when hit, 1  bounce with no loss of energy). PhysX calls physics materials “PxMaterial” which YOU MUST NOT CONFUSE WITH DISPLAY MATERIALS.

  14. Sample Friction Coefficients • The coefficient of static friction, denoted μs is usually larger than the coefficient of dynamic (or kinetic) friction, denoted μk. Sample values from Wikipedia PhysX demos often use 0.5 and 0.5

  15. Shapes Need Geometric Information • Shapes make use of a small geometric objects for encoding simple information; e.g., each shape type requires a specific geometry type; e.g. terrain  PxHeightFieldGeometrytriangle mesh  PxTriangleMeshGeometryplane PxPlaneGeometrysphere PxSphereGeometrybox  PxBoxGeometrycapsule PxCapsuleGeometry Would have seemed simpler if it were private information provided to the shape during its contruction

  16. Some Shapes Are Complex • Complex shapes like triangle meshes or convex meshes need extra initialization code to be executed before they are built (they call this cooking). • There are tools for filing out cooked shapes and filing them back in pre-cooked to speed up their use (a more advanced topic)...

  17. Actor Construction • Create the actor as either a static or dynamic type at its pose (orientation and position) in the scene. • Have the actor create the appropriate type of shape with material and specific geometric; it adds the shape to itself... • Set density (so far, I set everything to 1), mass, linear velocity, and angular velocity (velocities 0 if not set; don’t now what [1,2,3] means for angular velocity). • Update the mass and inertia properties (via updateMassAndInertia) • Add the Actor to the scene if you want to be able to hit it or if you want the simulator to use it... (an example where you would not is if you only want it for sphere sweeping queries)... • Delete (via release) all intermediateobjects you had to create except for the actor and the shape...

  18. Sphere Actor Example PxRigidDynamic* sphereActor = thePhysics->createRigidDynamic (PxTransform (position)); PxMaterial* sphereMaterial = mSDK->createMaterial (0.5f, 0.5f, 0.1f); PxShape* sphereShape = sphereActor->createShape (PxSphereGeometry (radius), sphereMaterial); PxRealsphereDensity = 1.0; PxRigidBodyExt::updateMassAndInertia (*sphereActor, sphereDensity); sphereActor->setLinearVelocity (velocity); //Do nothing if not moving. scene->addActor (sphereActor); sphereMaterial->release () I keep the scene in a physicsManagerwho creates all the physics objects. Spheres have a radius. The documentation sometimes uses thePhysics, sample code uses mSDK, I use physicsSystem.

  19. Shape ONLY Example for Capsule Actors PxTransform pose; pose.q = PxQuat (PxHalfPi, PxVec (0,0,1)); //90 degrees around z. PxReal radius = 1.0; //Meter? PxRealhalfHeight = 5.0; //Meters? PxShape* capsuleShape = capsuleActor->createShape (PxCapsuleGeometry (radius, halfHeight), aMaterial, pose); Capsules have a height (half above, half below); x-axis oriented

  20. Shape ONLY Example for Box Actors PxRealhalfWidth 1.0; //Meter? PxRealhalfHeight = 5.0; //Meters? PxRealhalfDepth = 2.0; //Meters? PxShape* boxShape = ` boxActor->createShape (PxBoxGeometry ( halfWidth, halfHeight, halfDepth ), boxMaterial); Boxes are specified via half width, half height, half depth,

  21. TriangleMesh Actors (Use a TriangleMesh shape) This is for STATIC geometry PxTriangleMeshDesc description; description.points.count = “number of vertices”; description.triangles.count = “number of triangles”; description.points.stride = “size of a vertex”; description.triangles.stride = “size of a triangle”; description.points.data = vertices; //std::vector<PxVec3>description.triangles.data = indices; //std::vector<PxU32> PxCooking* cooker = PxCreateCooking (PX_PHYSICS_VERSION, thePhysics->getFoundation(), PxCookingParams ()); MemoryWriteBufferbuffer; bool status = cooker->cookTriangleMesh (description, buffer); PxTriangleMesh* triangleMesh = thePhysics->createTriangleMesh (MemoryReadBuffer (buffer.data)); cooker->release (); PxRigidStatic* triangleMeshActor = thePhysics->createRigidStatic(pose); PxShape* triangleMeshShape = aTriMeshActor->createShape (PxTriangleMeshGeometry (triangleMesh), material); Triangle meshes are collection of vertices and the triangle indices AND must be cooked.

  22. Shape ONLY Example for Convex Mesh Actors const PxVec3 convexVertices [] = {PxVec3 (0,1,0), PxVec3 (1,0,0), PxVec3 (-1,0,0), PxVec3 (0,0,1), PxVec3 (0,0,-1)}; //5 vertices for a pyramid with base at 0 and peak at 1. PxConvexMeshDescconvexDescription; convexDescription.points.count = 5;convexDescription.points.stride = sizeof (PxVec3); convexDescription.points.data = convexVertices; convexDescription.flags = PxConvexFlag::eCOMPUTE_CONVEX There are NO indices; These are polygon soup vertices....SO YOU CAN’T MAKE it NON-CONVEX... Convex meshes are specified via vertices and must be initialized (cooked) before creation...

  23. Initializing (Cooking) Convex Meshes before Creation PxCooking* cooker = PxCreateCooking (PX_PHYSICS_VERSION, thePhysics->getFoundation (), PxCookingParams ()); MemoryWriteBufferbuffer; bool status = cooker->cookConvexMesh (convexDescription, buffer); PxConvexMesh* convexMesh = thePhysics->createConvexMesh (MemoryReadBuffer (buffer.data)); cooker->release (); Don’t know whether a cooker can be reused... Personally, I don’t know why this is NOT private to createConvexMesh?

  24. Shape ONLY Example for Plane Actors //Planes placed into space with a pose. PxRigidStatic* planeActor = thePhysics->createRigidStatic (pose); PxShape* planeShape = planeActor->createShape (PxPlaneGeometry (), material); Planes are specified with their backs hittable and the default direction pointing toward the positive x-direction (the identity pose)

  25. Game Engine Versus PhysX Terrain Both can cut quads this way Row based; texture Y goes up; vertex z goes more negative Column based; texture Y goes down; vertex z goes more positive game engine PhysX

  26. PhysX Height Map diagonal through quad origin • By executing, physXVertex->setTessFlag on the top-left vertex. we mean split this vertex to get • By executing, physXVertex->clearTessFlag on the top-left vertex, we mean don’t split this vertex to get material0 So every vertex will say this (it must be irrelevant for the rightmost column and the bottommost row) material1 material0 diagonal NOT through quad origin So every vertex will say this 0-based index material1

  27. Terrain (HeightField) Actors (Use a HeightField shape) PxHeightFieldSample* samples = //Unclear if origin is top left or bottom leftnew PxHeightFieldSample [rows * columns]; //PxHeightFieldDesc says it’s row based. Loop over samples //More details latersample.height = “a 16 bit integer (modified by scale below)” sample. materialIndex0 = 0; //Upper trianglesample. materialIndex1 = 0; //Lower triangle (NOT CLEAR)//PxHeightFieldMaterial::eHOLE is special. Sample. setTessFlag(); //Means plit this vertex so triangle diagonal is top-left to bottom-right.Sample. clearTessFlag(); //Means don’t so triangle diagonal is bottom-left to top-right. PxHeightFieldDescdescription; description.format = PxHeightFieldFormat::eS16_TM; description.nbColumns = cols; description.nbRows = rows; description.samples.data = samples; description.samples.stride = sizeof (PxHeightFieldSample); PxHeightField* heightField = thePhysics->createHeightField (description); PxRigidStatic* terrainActor = thePhysics- >createRigidStatic (pose); PxShape* terrainShape = terrainActor->createShape (PxHeightFieldGeometry (heightField, PxMeshGeometryFlags (), yScale, xScale, zScale),materialReference); //OR materialPointersArray, materialPointersArraySize); Actor deletes its shape but not samples or height field or the materials which can be deleted immediately after creating the shape. TerraIns are HeightField actors with rectangular grids of height field samples.

  28. Dealing with The Game Engine VERSUS PhysX PxRigidStatic *PhysicsManager::physicsTerrain (Terrain *terrain) { float physXXScale, physXYScale, physXZScale; Point physXPosition; terrain->physicsAttributes (physXXScale, physXYScale, physXZScale, physXPosition); //Create points for the terrain (PhysX calls them samples). setTessFlag means triangle diagonal//going from top left to bottom right (like backslash character), clearTessFlag means triangle//diagonal from bottom left to top right (like divide character)... PxHeightFieldSample* samplePoints = new | PxHeightFieldSample [terrain->heightMapWidth * terrain->heightMapHeight]; for (long y = 0; y < terrain->heightMapHeight; y++) { for (long x = 0; x < terrain->heightMapWidth; x++) { PxHeightFieldSample &toPoint = samplePoints[terrain->physicsCoordinateFor (x, y)];toPoint.height = (PxI16) terrain->physicsHeightFor (x, y);toPoint.clearTessFlag (); toPoint.materialIndex0 = 0; toPoint.materialIndex1 = 0; } } We provide 2 routines for physics conversion

  29. Point/Sphere Sweeping • Look up “raycastSingle” (for point sweeping) and “sweepSingle” (for sphere sweeping) for details on parameters that are needed or search the demo for example uses... • Need frompointfor point sweeping and transform for sphere sweeping. Both need a distance and direction... which can be computed from “from” and “to” points... float toDistance; Vector direction = to - from; direction.normalize (toDistance); Game has both a normalize and a normalized method

  30. Can Get Sphere Geometry of Existing Sphere Actor PxShape* shapeBuffer [1]; PxU32 shapeBufferSize = 1; physicsSphere->getShapes (shapeBuffer, shapeBufferSize); PxSphereGeometrysphereGeometry; shapeBuffer [0]->getSphereGeometry (sphereGeometry); The sphere geometry is in the sphere shape

  31. Point/Sphere Sweeping PxSweepHithit; //Filled in by the query... if (scene->raycastSingle (“from point”, “direction”, “distance”, “flags”, hit) //true if blocked... if (scene->sweepSingle (“sphereGeometry”, “from transform”, “direction”, “distance”, “flags”, hit) //true if blocked.. where “flags” are PxSceneQueryFlag::eBLOCKING_HIT | PxSceneQueryFlag::eDISTANCE If it was blocked, can find the intersection point easilyfloat hitDistance = hit.distance; float t = hitDistance / toDistance;Point intersectionPoint = from + (to - from) * t;

  32. Odds and Ends • Kinematic actors (with property eKINEMATIC) are special dynamic actors that are not influenced by forces (such as gravity), and have no momentum. They are considered to have infinite mass and can be moved around the world using the moveKinematic() method. They will push regular dynamic actors out of the way. Kinematics will not collide with static or other kinematic objects. Kinematic actors are great for moving platforms or characterswhere direct motion control is desired.

  33. Conclusion • PhysX is more complex than it needs to be but the complexity can be hidden away... • I have seen demos with a huge number of collapsing objects; e.g., from a castle built out of cement blocks... Where can I get an editor to do this or how can I write a converter for the existing castle?

More Related