04Collision Detection

Download Report

Transcript 04Collision Detection

95.4501

Comp 4501 Wilf LaLonde ©2012

Definition

Collision detection is the act of determining what is being hit for the purposes of providing suitable reaction behavior.

To have objects (especially the player) navigate through the game world without going 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).

Wilf LaLonde ©2012 Comp 4501

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...

Comp 4501

PhysX provides behavior simulation from increasingly complex objects built out of sphere , cube , and capsule sweeping and shapes along with sphere sweeping ray queries...

Wilf LaLonde ©2012

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 engine for more autonomous behavior .

The PhysX engine is a parallel multiprocessor capable engine that requires the use of ONE simple synchronization point to use effectively.

Wilf LaLonde ©2012 Comp 4501

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

Wilf LaLonde ©2012 Comp 4501

Generic Design physics engine scene physics actors collision shapes transform geometric entities friction materials mass density

Comp 4501 PxPhysics* sdk; PxCooking* cooking; PxScene* scene; PxMaterial* material;

cooker we call it physicsSystem

Wilf LaLonde ©2012

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 callback object 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...

Comp 4501 Wilf LaLonde ©2012

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...

Comp 4501

if (physicsManager->scene != NULL) physicsManager->scene-> simulate (DT); game->draw (); if (physicsManager->scene != NULL) physicsManager->scene-> fetchResults (true); wait until simulation done

Wilf LaLonde ©2012

Most Important Object: Actors

Actors are the only objects that can be associated with positional among other things.

information Instead of transformations , physX uses quaternions implemented in a class called PxTransform demos.

... and calls them poses in the We provide a handful of routines for converting back and forth WITHOUT HAVING TO KNOW HOW THEY WORK...

Comp 4501 Wilf LaLonde ©2012

Conversion Routines //Convenience conversion functions...

inline PxTransform asTransform ( 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 PxTransform Point &point ) { transformTranslatedTo ( PxTransform &transform, return PxTransform (asVec3 (point), transform.q);}

Wilf LaLonde ©2012 Comp 4501

Game Objects

• • •

Game objects , in general, need at least A transformation A display shape to position it 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 ...

Wilf LaLonde ©2012 Comp 4501

Actor Types Depend on their Collision Shapes

• • •

Actor types

• •

Static (unmovable) Dynamic (movable) Static only collision shapes include

• • •

infinite planes terrain (demos only) (needs a triangle meshes cooker to finalize it) (a polygon soup) Static or dynamic collision shapes include

Spheres , boxes , capsules , and convex meshes (but not encouraged to use latter)

Wilf LaLonde ©2012 Comp 4501

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 1

( 0

stick when hit, bounce with no loss of energy ).

PhysX calls physics materials “ PxMaterial ” which YOU MUST NOT CONFUSE WITH DISPLAY MATERIALS .

Comp 4501 Wilf LaLonde ©2012

Sample Friction Coefficients

• The coefficient of

static friction

, denoted

μ s

usually larger than the coefficient of

dynamic

(or kinetic)

friction

, denoted

μ k

.

is Aluminium Copper Brass Cast iron Cast iron Concrete (wet) Concrete (dry) Concrete Copper Glass Metal Polyethene Steel Steel PTFE Wood Materials Steel Steel Steel Copper Zinc Rubber Rubber Wood Glass Glass Wood Steel Steel PTFE PTFE Wood Static friction, Dry and clean Lubricated 0.61

0.53

0.51

1.05

0.85

0.30

1.0

0.62

[10] 0.68

0.94

0.2

–0.6

[10] 0.2

[11] 0.80

[11] 0.04

[11] 0.04

[11] 0.25

–0.5

[10] 0.2 (wet) [10] 0.2

[11] 0.16

[11] 0.04

[11] 0.04

[11] 0.2 (wet) [10] Sample values from Wikipedia PhysX demos often use 0.5 and 0.5

Wilf LaLonde ©2012 Comp 4501

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 triangle mesh plane sphere box capsule

     

PxHeightFieldGeometry PxTriangleMeshGeometry PxPlaneGeometry PxSphereGeometry PxBoxGeometry PxCapsuleGeometry Would have seemed simpler if it were private information provided to the shape during its contruction

Comp 4501 Wilf LaLonde ©2012

Some Shapes Are Complex

Complex shapes like triangle meshes or convex meshes this cooking ).

need extra initialization code to be executed before they are built (they call

There are tools for filing out cooked shapes and filing them back in pre-cooked to speed up their use (a more advanced topic)...

Wilf LaLonde ©2012 Comp 4501

Actor Construction

Create the actor as either a static or dynamic ( orientation and position ) in the scene.

type at its pose

Have the actor create the appropriate type of shape and specific geometric; it adds the shape to itself ...

with material

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 intermediate objects except for the actor and the shape ...

you had to create

Comp 4501 Wilf LaLonde ©2012

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); PxReal sphereDensity = 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 physicsManager who creates all the physics objects .

Spheres have a radius.

Comp 4501

The documentation sometimes uses thePhysics , sample code uses mSDK , I use physicsSystem .

Wilf LaLonde ©2012

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?

PxReal halfHeight = 5.0; //Meters?

PxShape* capsuleShape = capsuleActor-> createShape PxCapsuleGeometry ( (radius, halfHeight), aMaterial, pose);

Comp 4501

Capsules have a height (half above, half below); x-axis oriented

Wilf LaLonde ©2012

Shape ONLY Example for Box Actors PxReal halfWidth 1.0; //Meter?

PxReal halfHeight = 5.0; //Meters?

PxReal halfDepth = 2.0; //Meters?

` PxShape* boxShape = boxActor-> createShape ( PxBoxGeometry ( halfWidth, halfHeight, halfDepth ), boxMaterial);

Comp 4501

Boxes are specified via half width, half height, half depth,

Wilf LaLonde ©2012

TriangleMesh Actors (Use a TriangleMesh shape) 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 description.triangles.data = indices; //std::vector This is for STATIC geometry PxCooking* cooker = PxCreateCooking (PX_PHYSICS_VERSION, thePhysics >getFoundation(), PxCookingParams ()); MemoryWriteBuffer buffer ; bool status = cooker > cookTriangleMesh (description, buffer ); PxTriangleMesh* triangleMesh = thePhysics->createTriangleMesh ( MemoryReadBuffer ( buffer .data)); cooker >release (); PxRigidStatic* triangleMeshActor = thePhysics-> createRigidStatic(pose ); PxShape* triangleMeshShape = aTriMeshActor-> createShape material); (PxTriangleMeshGeometry (triangleMesh), Triangle meshes are

collection of vertices and the triangle indices AND must be cooked.

Comp 4501 Wilf LaLonde ©2012

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.

PxConvexMeshDesc convexDescription ; 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...

Wilf LaLonde ©2012 Comp 4501

Initializing (Cooking) Convex Meshes before Creation PxCooking* cooker = PxCreateCooking (PX_PHYSICS_VERSION, thePhysics->getFoundation (), PxCookingParams ()); MemoryWriteBuffer buffer ; 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?

Comp 4501 Wilf LaLonde ©2012

Shape ONLY Example for Plane Actors //Planes placed into space with a pose. PxRigidStatic* planeActor = thePhysics-> createRigidStatic (pose); PxShape* planeShape = planeActor-> createShape ( PxPlaneGeometry (), material);

Comp 4501

Planes are specified with their backs hittable and the default direction pointing toward the positive x-direction (the identity pose)

Wilf LaLonde ©2012

Game Engine Versus PhysX Terrain

Both can cut quads this way Row based; texture Y goes up; vertex z goes more negative game engine Comp 4501 Column based; texture Y goes down; vertex z goes more positive PhysX Wilf LaLonde ©2012

PhysX Height Map diagonal through quad origin

• •

By executing, physXVertex-> setTessFlag left vertex. we mean split this vertex to get on the top material0

So every vertex will say this (it must be irrelevant for the rightmost column and the bottommost row)

material1 By executing, physXVertex-> clearTessFlag on the top left vertex, we mean don’t split this vertex to get material0

0-based index

diagonal NOT through quad origin

So every vertex will say this

material1

Wilf LaLonde ©2012 Comp 4501

Terrain (HeightField) Actors (Use a HeightField shape) PxHeightFieldSample* samples = //Unclear if origin is top left or bottom left new PxHeightFieldSample [rows * columns]; //PxHeightFieldDesc says it’s row based.

Loop over samples //More details later sample.

height = “a 16 bit integer (modified by scale below)” sample.

materialIndex0 = 0; //Upper triangle sample. //PxHeightFieldMaterial::eHOLE is special. Sample. materialIndex1 setTessFlag = 0; //Lower triangle (NOT CLEAR) (); // 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.

PxHeightFieldDesc description ; description.format = PxHeightFieldFormat::eS16_TM; description.nbColumns = cols; description.nbRows = rows; description.samples.data = samples; Actor deletes its shape but not samples or height field or the materials which can be deleted immediately after creating the shape.

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); TerraIns are HeightField actors with rectangular grids of height field samples.

Comp 4501 Wilf LaLonde ©2012

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 toPoint.height = (PxI16) terrain->physicsHeightFor (x, y); toPoint.clearTessFlag (); toPoint.materialIndex0 = 0; toPoint.materialIndex1 = 0; (x, y)]; } }

Comp 4501

We provide 2 routines for physics conversion

Wilf LaLonde ©2012

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 from point for 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

Comp 4501 Wilf LaLonde ©2012

Can Get Sphere Geometry of Existing Sphere Actor PxShape* shapeBuffer [1]; PxU32 shapeBufferSize = 1; physicsSphere-> getShapes (shapeBuffer, shapeBufferSize); PxSphereGeometry sphereGeometry ; shapeBuffer [0]-> getSphereGeometry ( sphereGeometry );

Comp 4501

The sphere geometry is in the sphere shape

Wilf LaLonde ©2012

Point/Sphere Sweeping PxSweepHit hit ; //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 easily float hitDistance = hit .distance; float t = hitDistance / toDistance; Point intersectionPoint = from + (to - from) * t;

Wilf LaLonde ©2012 Comp 4501

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 characters where direct motion control is desired.

Wilf LaLonde ©2012 Comp 4501

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 ?

Wilf LaLonde ©2012 Comp 4501