Introduction Physics Engines AGEIA Physx SDK AGEIA
Total Page:16
File Type:pdf, Size:1020Kb
25/3/2008 SVR2007 Introduction Tutorial AGEIA PhysX • What does physics give? – Real world interaction metaphor – Realistic simulation Virtual Reality • Who needs physics? and Multimedia { mouse, ds2, gsm, masb, vt, jk } @cin.ufpe.br Research Group Thiago Souto Maior – Immersive games and applications Daliton Silva Guilherme Moura Márcio Bueno • How to use this? Veronica Teichrieb Judith Kelner – Through physics engines implemented in software or hardware Petrópolis, May 2007 Physics Engines AGEIA PhysX SDK • Simplified Newtonian models • Based upon NovodeX API • “High precision” versus “real time” simulations • Commercial and open source engines • Middleware concept • What comes together in a physics engine? • First asynchronous physics API – Mandatory • Tons of math calculations • Takes advantage of multiprocessing game • Collision detection • Rigid body dynamics systems – Optional • Fluid dynamics • Cloth simulation • Particle systems • Deformable object dynamics AGEIA PhysX SDK AGEIA PhysX SDK • Supports game • Integration with game development life cycle engines – 3DSMax and Maya – Unreal Engine 3.0 plugins (PhysX – Emergent GameBryo Create) – Natural Motion – Properties tuning with Endorphin PhysX Rocket • Runs in PC, Xbox 360 – Visual remote and PS3 debugging with PhysX VRD 1 25/3/2008 AGEIA PhysX SDK AGEIA PhysX Processor • Supports • AGEIA PhysX PPU – Complex rigid body dynamics • Powerful PPU + – Collision detection CPU + GPU – Joints and springs – Physics + Math + – Volumetric fluid simulation Fast Rendering – Particle systems – Cloth simulation – Soft body simulation Other Physics Engines Download • Open source • Account registration required – Open Dynamics Engine (ODE) • http://devsupport.ageia.com – Bullet Physics Library • 1-3 business days • Free – TOKAMAK – Newton • Commercial – Havok FX Download Download • Download section • AGEIA Driver • System Software • SDK version selection 2 25/3/2008 License Installation • Free license • System requirements – Non-commercial use – Windows XP or Vista – PS3 platform (through Sony pre-purchase) • Most recent Service Pack – Through some middleware partnerships – 512MB of system memory • UE3, Gamebryo 2.2, etc – At least 150MB of free HD space – PC platform (if makes use* of PhysX HW) • $50k per platform – PCI expansion slot for PPU – All other uses – Additional 4-pin molex power connector Installation Documentation • PPU Driver and System Software • Documents – Run samples – Windows help file containing PhysX API • Samples – Source codes and executables covering most of • PhysX SDK Core PhysX features – Header and lib files • Training Programs – Documentation – Documents, overview and source code Architecture & Components Architecture: World NxPhysicsSDK * myWorld = NxCreatePhysicsSDK(NX_PHYSICS_SDK_VERSION); World (PhysX SDK) 3 25/3/2008 Architecture: Scene Architecture: Actor • Scene • Actor – Collection of every interactive element – Main simulation object • Bodies • Dynamic • Constraints • Static • Effectors NxPlaneShapeDesc planeDesc; NxActorDesc actorDesc; actorDesc.shapes.pushBack(&planeDesc); NxActor *staticActor = gScene->createActor(actorDesc); Architecture: Shapes Architecture: Shapes • Shapes • Box Shape NxBoxShapeDesc boxDesc; boxDesc.dimensions.set(0.5f, 0.5f, 0.5f); NxBoxShape *boxShape = actor->createShape(boxDesc)->isBox(); Architecture: Shapes Architecture: Shapes • Capsule Shape • Convex Shape – Defined by a radius – Shape defined by a set of vertices and a height NxCapsuleShapeDesc capsuleDesc; capsuleDesc.height = 2.0f; capsuleDesc.radius = 0.5f; NxCapsuleShape *capsuleShape=actor->createShape(capsuleDesc)->isCapsule(); 4 25/3/2008 Architecture: Shapes Architecture: Shapes NxVec3 verts[8] = { NxVec3(-1,-1,-1),NxVec3(-1,-1,1), NxVec3(-1,1,-1),NxVec3(-1,1,1), NxVec3(1,-1,-1),NxVec3(1,-1,1), • Height Field Shape NxVec3(1,1,-1),NxVec3(1,1,1) }; – Height Field Map to generate a collision NxU32 vertCount = 8; terrain NxConvexMeshDesc convexDesc; convexDesc.numVertices = vertCount; convexDesc.pointStrideBytes = sizeof(NxVec3); convexDesc.points = verts; convexDesc.flags = NX_CF_COMPUTE_CONVEX; MemoryWriteBuffer buf; bool status = NxCookConvexMesh(convexDesc, buf); NxConvexMesh *mesh = gPhysicsSDK->createConvexMesh(MemoryReadBuffer(buf.data)); NxConvexShapeDesc convexShapeDesc; convexShapeDesc.meshData = mesh; NxConvexShape *convexShape=actor->createShape(convexShapeDesc)->isConvex(); Architecture: Shapes Architecture: Shapes NxHeightFieldDesc heightFieldDesc; heightFieldDesc.nbColumns = nbColumns; heightFieldDesc.nbRows = nbRows; heightFieldDesc.verticalExtent = -1000; heightFieldDesc.convexEdgeThreshold = 0; heightFieldDesc.samples = new NxU32[nbColumns*nbRows]; heightFieldDesc.sampleStride = sizeof(NxU32); NxU8* currentByte = (NxU8*)heightFieldDesc.samples; NxHeightFieldShapeDesc heightFieldShapeDesc; for (NxU32 row = 0; row < nbRows; row++) { heightFieldShapeDesc.heightField = heightField; for (NxU32 column = 0; column < nbColumns; column++) { heightFieldShapeDesc.heightScale = gVerticalScale; NxHeightFieldSample* currentSample = (NxHeightFieldSample*)currentByte; heightFieldShapeDesc.rowScale = gHorizontalScale; currentSample->height = computeHeight(row,column); heightFieldShapeDesc.columnScale = gHorizontalScale; currentSample->materialIndex0 = gMaterial0; heightFieldShapeDesc.holeMaterial = 2; currentSample->materialIndex1 = gMaterial1; currentSample->tessFlag = 0; NxHeightFieldShape currentByte += heightFieldDesc.sampleStride; *heightFieldShape= } } actor->createShape(heightFieldShapeDesc)->isHeightField(); NxHeightField* heightField = gScene->getPhysicsSDK().createHeightField(heightFieldDesc); Architecture: Shapes Architecture: Shapes • Plane Shape • Sphere Shape – Default: y = 0 – Defined by – Defined by a Normal a radius and the distance from the origin NxPlaneShapeDesc planeDesc; NxSphereShapeDesc sphereDesc; planeDesc.normal = NxVec3(0.0f,1.0f,0.0f); sphereDesc.radius = 1.0f; planeDesc.d = 10.0f; NxSphereShape NxPlaneShape *sphereShape=actor->createShape(sphereDesc)->isSphere(); *planeShape=actor->createShape(planeDesc)->isPlane(); 5 25/3/2008 Architecture: Shapes Architecture: Shapes • Triangle Mesh Shape • Wheel Shape – Radius – Suspension travel – Motor torque – Brake torque – Steer angle – Raycast from shape’s NxTriangleMeshShapeDesc meshShapeDesc; origin along the axis meshShapeDesc.meshData = triangleMesh; • Hard contact • Soft suspension NxTriangleMeshShape *triangleMeshShape= • No contact actor->createShape(meshShapeDesc)->isTriangleMesh(); Architecture: Joints Architecture: Materials • Joints • Materials – Connections between rigid bodies – Shape surface properties • Friction • Restitution Architecture: Materials Mathematical Support Default NxMaterial* defaultMaterial = gScene->getMaterialFromIndex(0); material • Based on defaultMaterial->setRestitution(0.5); defaultMaterial->setStaticFriction(0.5); – NxMath defaultMaterial->setDynamicFriction(0.5); – NxMat33 NxMaterialDesc material; – NxMat34 material.restitution = 0.0f; material.staticFriction = 0.1f; – NxVec3 material.dynamicFriction = 0.1f; material.dynamicFrictionV = 0.8f; – NxQuat material.staticFrictionV = 1.0f; material.dirOfAnisotropy.set(0,0,1); • Storage format independent material.flags = NX_MF_ANISOTROPIC; – According to row or column major NxMaterial *anisoMaterial = Anisotropic gScene->createMaterial(material); material 6 25/3/2008 NxMath NxMat33 • Container of static math operations • Abstraction to 3x3 matrix • Conversion between degrees and radians • Represent rotations or inertia tensors • Functions • Implements operators – min, max, floor, ceil, sqrt – Sum and subtraction (+/-) – Logarithm functions in common bases – Matrix and scalar product (*) – Trigonometric functions • Other operations like inverting and – Some constants like NxPi, NxHalfPi, NxTwoPi transposing are also available and NxInvPi NxMat33 NxMat34 • Example • Wrapper class encapsulates – Given a vector in world space, how to – NxMat33 (rotation) transform it into local space? – NxVec3 (translation) NxVec3 localVec, worldVec; • Public attributes: M and t NxMat33 orient, invOrient; worldVec = NxVec3(0,0,1); • Conversions to 4x4 rendering matrix orient = actor->getGlobalOrientation(); formats orient.getInverse(invOrient); localVec = invOrient * worldVec; • Operator product (*) NxMat34 NxVec3 • Example • Can represent a point or a vector – Given a position in the world space, how to • Attributes: x, y, z transform it into the local space? • Direct or array-like access NxActor* actor; ... • Implements vectors’ arithmetic, including NxVec3 localPos, worldPos; cross product, dot product and others NxMat34 mat, invMat; operations related to vectors worldPos = NxVec3(0,0,1); mat = actor->getGlobalPose(); mat.getInverse(invMat); localPos = invMat * worldPos; 7 25/3/2008 NxQuat NxQuat • Quaternion representation • Example • Hamiltonian Arithmetic – How to transform a quaternion into its • Attributes: x, y, z, w correspondent 3x3 matrix? • Used to represent a rotation – Axis and angle NxQuat q; • Concatenation through product (*), like matrixes q.fromAngleAxis(90, NxVec3(0,1,0)); • Rotations can be applied to points and vectors – rot method ... NxMat33 orient; • Interpolation between two orientations orient.fromQuat(q); – Slerp – Quaternion is used as keyframe Scene Scene • Declaring variables • Initializing the World static NxPhysicsSDK* gPhysicsSDK = NULL; gPhysicsSDK = static NxScene* gScene = NULL; NxCreatePhysicsSDK(NX_PHYSICS_SDK_VERSION, NULL, &gErrorStream); static NxVec3 gDefaultGravity(0.0f, -98.1f,