08_게임 물리 프로그래밍 가이드

44
김성익 김성익 ( ( [email protected] [email protected] ) ) 2008/06/22 2008/06/22 물리 물리 엔진 엔진 사용을 사용을 위한 위한 게임 게임 물리 물리 프로그래밍 프로그래밍 가이드 가이드

description

 

Transcript of 08_게임 물리 프로그래밍 가이드

Page 1: 08_게임 물리 프로그래밍 가이드

김성익김성익(([email protected]@hitel.net))2008/06/222008/06/22

물리물리 엔진엔진 사용을사용을 위한위한

게임게임 물리물리 프로그래밍프로그래밍 가이드가이드

Page 2: 08_게임 물리 프로그래밍 가이드

Overview

•• 물리물리 엔진엔진 사용을사용을 위한위한 백그라운드백그라운드 정보정보

–– 개념들개념들 소개소개//정리정리

–– 구현을구현을 다루지다루지 않음않음

–– Box2D Box2D 소개소개

•• PhysXPhysX 소개소개

Page 3: 08_게임 물리 프로그래밍 가이드

Box2D

•• Erin Erin CattoCatto씨씨 개인개인 프로젝트프로젝트

•• 2D 2D 오픈오픈 소스소스 물리물리 엔진엔진

•• 사용한사용한 프로젝트프로젝트 : : Crayon PhysicsCrayon Physics

•• http://www.box2d.orghttp://www.box2d.org

•• 최적의최적의 물리물리 처리처리 레퍼런스레퍼런스

Page 4: 08_게임 물리 프로그래밍 가이드

Kinematics

•• Newtonian PhysicsNewtonian Physics

–– F = maF = ma

•• Spring (F=Spring (F=--kxkx))

•• Drag Force (F=Drag Force (F=--mpvmpv))

•• GravityGravity

–– F = F = dp/dtdp/dt, P = , P = mvmv

–– a = a = dV/dtdV/dt

–– V = V = dX/dtdX/dt

Page 5: 08_게임 물리 프로그래밍 가이드

Calculus

•• PositionPosition

•• VelocityVelocity

•• AccelerationAcceleration

Page 6: 08_게임 물리 프로그래밍 가이드

Solution

•• Analytic SolutionAnalytic Solution

•• Numeric SolutionNumeric Solution

–– Euler MethodEuler MethodEuler MethodEuler MethodEuler MethodEuler MethodEuler MethodEuler Method

–– Midpoint MethodMidpoint Method

–– RungeRunge KuttaKutta

–– Implicit MethodImplicit Method

–– VerletVerlet

Page 7: 08_게임 물리 프로그래밍 가이드

Solution

•• Analytic SolutionAnalytic Solution

•• Numeric SolutionNumeric Solution

–– Euler MethodEuler Method

–– Midpoint MethodMidpoint MethodMidpoint MethodMidpoint MethodMidpoint MethodMidpoint MethodMidpoint MethodMidpoint Method

–– RungeRunge KuttaKutta

–– Implicit MethodImplicit Method

–– VerletVerlet

Page 8: 08_게임 물리 프로그래밍 가이드

Solution

•• Analytic SolutionAnalytic Solution

•• Numeric SolutionNumeric Solution

–– Euler MethodEuler Method

–– Midpoint MethodMidpoint Method

–– RungeRungeRungeRungeRungeRungeRungeRunge KuttaKuttaKuttaKuttaKuttaKuttaKuttaKutta

–– Implicit MethodImplicit Method

–– VerletVerlet

Page 9: 08_게임 물리 프로그래밍 가이드

Solution

•• Analytic SolutionAnalytic Solution

•• Numeric SolutionNumeric Solution

–– Euler MethodEuler Method

–– Midpoint MethodMidpoint Method

–– RungeRunge KuttaKutta

–– Implicit MethodImplicit MethodImplicit MethodImplicit MethodImplicit MethodImplicit MethodImplicit MethodImplicit Method

–– VerletVerlet

Page 10: 08_게임 물리 프로그래밍 가이드

Solution

•• Analytic SolutionAnalytic Solution

•• Numeric SolutionNumeric Solution

–– Euler MethodEuler Method

–– Midpoint MethodMidpoint Method

–– RungeRunge KuttaKutta

–– Implicit MethodImplicit Method

–– VerletVerletVerletVerletVerletVerletVerletVerlet

Page 11: 08_게임 물리 프로그래밍 가이드

Rotational Dynamics

•• Center of MassCenter of Mass

•• Linear Linear vsvs AngularAngular

–– Force Force vsvs TorqueTorque

–– Mass Mass vsvs Moment of inertiaMoment of inertia

–– Momentum Momentum vsvs Angular MomentumAngular Momentum

–– Velocity Velocity vsvs Angular VelocityAngular Velocity

–– Position Position vsvs OrientationOrientation

Page 12: 08_게임 물리 프로그래밍 가이드

Box2d CodeReview(1)

•• Simulation iteration stepSimulation iteration step

–– broadphasebroadphase

•• 충돌검사충돌검사 ((충돌정보충돌정보 Arbiter Arbiter 개체로개체로 저장저장))

–– force force 적용적용

•• forceforce-->velocity>velocity

•• torquetorque-->angular velocity>angular velocity

–– prestepprestep

•• 충돌충돌 포인트에포인트에 대해서대해서 massnormalmassnormal, , masstangentmasstangent, bias , bias 계산계산

Page 13: 08_게임 물리 프로그래밍 가이드

Box2d CodeReview(2)

–– 충돌에충돌에 의한의한 impulseimpulse

•• 충돌체에충돌체에 impulseimpulse

–– velocity, angular velocity velocity, angular velocity 적용적용

•• Joint Joint 에에 의한의한 impulseimpulse

–– 양양 jointjoint에에 충돌체에충돌체에 impulseimpulse

–– 위치위치 및및 회전에회전에 적용적용

•• velocity => positionvelocity => position

•• Angular velocity => rotationAngular velocity => rotation

Page 14: 08_게임 물리 프로그래밍 가이드

PhysX(1)

•• NVidiaNVidia ((http://http://www.ageia.comwww.ageia.com))

•• 무료무료 ((일부일부 플렛폼플렛폼 및및 소스코드소스코드 라이센스는라이센스는유료유료))

•• 충실한충실한 튜터리얼튜터리얼 셈플셈플 제공제공

•• 메인스트림메인스트림 물리물리 엔진엔진

•• 사용사용 프로젝트프로젝트 : : 언리얼언리얼3, 3, 외외 다수다수

Page 15: 08_게임 물리 프로그래밍 가이드

PhysX(2)

•• 기능기능

–– 복잡한복잡한 강체강체 물리물리 시스템시스템

–– 캐릭터캐릭터 컨트롤컨트롤

–– 탈탈 것것 다이나믹스다이나믹스

–– 멀티쓰레드멀티쓰레드//멀티멀티 플렛폼플렛폼/PPU/PPU지원지원

–– 유체유체 시뮬레이션시뮬레이션

–– 옷옷, , 깃발등의깃발등의 천천 시뮬레이션시뮬레이션

–– 소프트소프트 바디바디

–– 역장역장((forcefieldforcefield) ) 시뮬레이션시뮬레이션

Page 16: 08_게임 물리 프로그래밍 가이드

PhysX–architecture diagram

Page 17: 08_게임 물리 프로그래밍 가이드

Shape

•• ShapeShape

–– SphereSphere

–– CapsuleCapsule

–– BoundBoxBoundBox

•• AABB AABB (Axis Aligned Bound Box)(Axis Aligned Bound Box)

•• OBB OBB (Object Oriented Box)(Object Oriented Box)

–– ConvexConvex

–– (height field)(height field)

Page 18: 08_게임 물리 프로그래밍 가이드

PhysX-Shape

•• SphereSphere•• BoxBox•• CapsuleCapsule

•• PlanePlane•• Height fieldHeight field•• Convex meshesConvex meshes•• Triangle meshesTriangle meshes

Page 19: 08_게임 물리 프로그래밍 가이드

PhysX-Actor

Page 20: 08_게임 물리 프로그래밍 가이드

Collision Detection(1)

•• Distance TestDistance Test

–– PointPoint--point distancepoint distance

–– LineLine--point distancepoint distance

–– LineLine--line distanceline distance

–– SegmentSegment--segment distancesegment distance

•• Rough BoundingRough Bounding

–– Sweep and pruneSweep and prune

Page 21: 08_게임 물리 프로그래밍 가이드

Collision Detection(1)

•• Distance TestDistance Test

–– PointPoint--point distancepoint distance

–– LineLine--point distancepoint distance

–– LineLine--line distanceline distance

–– SegmentSegment--segment distancesegment distance

•• Rough BoundingRough Bounding

–– Sweep and pruneSweep and pruneSweep and pruneSweep and pruneSweep and pruneSweep and pruneSweep and pruneSweep and prune

Page 22: 08_게임 물리 프로그래밍 가이드

Collision Detection(2)

•• SAT(separationSAT(separation axis test)axis test)

•• Lin CannyLin Canny

•• MinkowskiMinkowski DiffrenceDiffrence

Page 23: 08_게임 물리 프로그래밍 가이드

Collision Detection(2)

•• SAT(separationSAT(separation axis test)axis test)

•• Lin CannyLin Canny

•• MinkowskiMinkowski DiffrenceDiffrence

Page 24: 08_게임 물리 프로그래밍 가이드

Collision Detection(2)

•• SAT(separationSAT(separation axis test)axis test)

•• Lin CannyLin Canny

•• MinkowskiMinkowski Sum/Sum/DiffrenceDiffrence

Page 25: 08_게임 물리 프로그래밍 가이드

Collistion Detection(3)

•• Determine Collision TimeDetermine Collision Time

–– Time of impactTime of impact

•• TunnellingTunnelling (CCD)(CCD)

Page 26: 08_게임 물리 프로그래밍 가이드

Collistion Detection(3)

•• Determine Collision TimeDetermine Collision Time

–– Time of impactTime of impact

•• TunnellingTunnelling

Page 27: 08_게임 물리 프로그래밍 가이드

PhysX - CCD

•• Continuous Collision DetectionContinuous Collision Detection

Page 28: 08_게임 물리 프로그래밍 가이드

Collision Detection(4)

•• Spatial SubdivisionSpatial Subdivision

–– BspBsp, , kk--dd treetree

–– PortalPortal

–– Quad tree / Quad tree / octreeoctree

•• GroupingGrouping

–– SleepingSleeping

Page 29: 08_게임 물리 프로그래밍 가이드

Collision Detection(4)

•• Spatial SubdivisionSpatial Subdivision

–– BspBsp, , kk--dd treetree

–– PortalPortal

–– Quad tree / Quad tree / octreeoctree

•• GroupingGrouping

–– SleepingSleeping

Page 30: 08_게임 물리 프로그래밍 가이드

Collision Detection(4)

•• Spatial SubdivisionSpatial Subdivision

–– BspBsp, , kk--dd treetree

–– PortalPortal

–– Quad tree / Quad tree / octreeoctree

•• GroupingGrouping

–– SleepingSleeping

Page 31: 08_게임 물리 프로그래밍 가이드

Collision Detection(4)

•• Spatial SubdivisionSpatial Subdivision

–– BspBsp, , kk--dd treetree

–– PortalPortal

–– Quad tree / Quad tree / octreeoctree

•• GroupingGrouping

–– SleepingSleeping

Page 32: 08_게임 물리 프로그래밍 가이드

Collision Detection(4)

•• Spatial SubdivisionSpatial Subdivision

–– BspBsp, , kk--dd treetree

–– PortalPortal

–– Quad tree / Quad tree / octreeoctree

•• GroupingGrouping

–– SleepingSleeping

Page 33: 08_게임 물리 프로그래밍 가이드

PhysX-Collision Detection

•• Contact FilteringContact Filtering•• TriggersTriggers•• CCDCCD•• Dominance Dominance GrioupGrioup•• Overlap TestingOverlap Testing

Page 34: 08_게임 물리 프로그래밍 가이드

Collision Response(1)

•• Contact PointContact Point

–– EE (EdgeEE (Edge--Edge)Edge)

–– FV (FaceFV (Face--Vertex)Vertex)

–– GJKGJK

•• NormalNormal

•• PenetrationPenetration

Page 35: 08_게임 물리 프로그래밍 가이드

Collision Response(1)

•• Contact PointContact Point

–– EE (EdgeEE (Edge--Edge)Edge)

–– FV (FaceFV (Face--Vertex)Vertex)

–– GJKGJK

•• NormalNormal

•• PentrationPentration

Page 36: 08_게임 물리 프로그래밍 가이드

Collision Response(2)

•• CollidingColliding

–– BouncingBouncing

•• RestingResting

–– RollingRolling

–– SlidingSliding

Page 37: 08_게임 물리 프로그래밍 가이드

Collision Response(2)

•• CollidingColliding

–– BouncingBouncing

•• RestingResting

–– RollingRolling

–– SlidingSliding

Page 38: 08_게임 물리 프로그래밍 가이드

Constraint

•• JointJoint

–– Pin JointsPin Joints

–– HingeHinge

•• Angular ConstraintsAngular Constraints

•• Distance Constraints, ...Distance Constraints, ...

Page 39: 08_게임 물리 프로그래밍 가이드

PhysX-Joint

•• spherical jointspherical joint•• revolute jointrevolute joint•• prismatic jointprismatic joint•• cylindrical jointcylindrical joint•• fixed jointfixed joint•• distance jointdistance joint•• point in plane joint / point on line jointpoint in plane joint / point on line joint•• pulley jointpulley joint•• d6d6

Page 40: 08_게임 물리 프로그래밍 가이드

PhysX-Joint

•• spherical jointspherical joint•• revolute jointrevolute joint•• prismatic jointprismatic joint•• cylindrical jointcylindrical joint•• fixed jointfixed joint•• distance jointdistance joint•• point in plane joint / point on line jointpoint in plane joint / point on line joint•• pulley jointpulley joint•• d6d6

Page 41: 08_게임 물리 프로그래밍 가이드

PhysX-Joint

•• spherical jointspherical joint•• revolute jointrevolute joint•• prismatic jointprismatic joint•• cylindrical jointcylindrical joint•• fixed jointfixed joint•• distance jointdistance joint•• point in plane joint / point on line jointpoint in plane joint / point on line joint•• pulley jointpulley joint•• d6d6

Page 42: 08_게임 물리 프로그래밍 가이드

PhysX-Example Review

•• 예제예제 : : SampleBoxesSampleBoxes

•• InitNxInitNx–– Initialize Initialize PhysicsScenePhysicsScene => => gPhysicsSDKgPhysicsSDK–– CreateSceneCreateScene => => gScenegScene–– Create Ground Create Ground => => gScenegScene-->>createActorcreateActor

•• RenderCallbackRenderCallback–– SimulateSimulate–– GetResultGetResult/Render All actors => actor/Render All actors => actor-->>getGlobalPosgetGlobalPos–– FetchSimulationFetchSimulation => => flushScreamflushScream, , fetchResultfetchResult

•• CreateCubeCreateCube–– shapes.pushBackshapes.pushBack–– gScenegScene-->>createActorcreateActor

Page 43: 08_게임 물리 프로그래밍 가이드

More

•• BreakingBreaking

•• Force FieldForce Field

•• Character ControllerCharacter Controller

•• Raycast/SweepTestRaycast/SweepTest

•• Multithreading ModelMultithreading Model

Page 44: 08_게임 물리 프로그래밍 가이드

Reference

•• http://http://www.essentialmath.comwww.essentialmath.com

•• 게임물리바이블게임물리바이블,David H ,David H EverlyEverly, , 사이텍미디어사이텍미디어