Polyart 2D Physics Engine
Section 1: The Interface
Location: programming\c++\cvs\polylib
| Files:
| Sim2D.cpp -
|
| Body2D.cpp -
|
| Geometry2D.cpp -
|
| Contact2D.cpp -
|
| CheckSegmentCollision2D.cpp -
|
Status: Design, Partial Implementation
Todo Checklist
- Step currently updates the bounding boxes multiple times
- Instead of looping in step, add collided objects to a queue to be rechecked
- Implement time of collison
- Make a list of colliding objects sorted by time
Algorithms
Also see Shipgame specific Physics
Physics Documentation Index
What does this physics engine do?
- A bunch of physical objects known as bodies are placed in a collider. These bodies will have a position, velocity, mass and collision structure.
- Interfaces allow operations to be performed on the bodies such as apply velocity.
- The collider can be stepped moving the objects into their next position. Colliding objects will trigger a response.
- One type of response will be to apply forces to each colliding body to prevent interpenetration. The other is user defined.
Generic collision detection and response
The engine is implemented as a hierarchy of collision detection types and responses. Since bodies have a bounding box which is tested for collision before the internal segments, a detector could detect bounding box collisions then respond with a new detector detecting the segment collisions.
Collision detection is handled similarly regardless of object type. Similarly meaning having the same function names to perform the same operations. The classes might not be derived from each other.
It is implemented as 3 classes.
Object Type: The type of object to detect collisions
Detection : This class will hold Objects. A CollideWith(Object) function will enumerate all collisions with the object and call the response class for each.
Response : Derive a response class from this to handle the detected collisions in different ways
General Detection Procedure
- Add all collidable objects to a collider
- Derive a class from the response class. Set the response type to that class.
- Call collide with on an object. All collisions with that object will be enumerated though the response class.
User Classes
- CSim2D - The polyart physics simulator
- CBody2D - A body has position, mass and can move. It can also be affected by forces.
- CCollisionInfo - Information about the current collision being processed
- ------
- CGeometry2D - A Geometry structure is able to determine if in the current step 2 bodies will collide.
- CCollisionResponse2D - The collision solver will gather information from the Geometry about upcomming collisions and prevent them from happening.
- CContact2D - One of these will be linked to every two bodies in a collision
Goals of the physics engine
- Objects will have a position X,Y,Rotation and velocity XS,YS,RotationS
- Objects will have a collision structure represented by one sided line segments. All objects will be linearly moving.
- The engine can be stepped by X amount which will apply X amount of the velocity
- Sets of objects can be created and the sets can be independantly checked for collisions.
- The engine will prevent all objects from interpenetrating.
- ----------
- A special collision structure creation class should be used to prevent holes.
- Collision structures should not be permitted unless they do not have any holes.(seamless?)
- "Regions" can be defined so that an object inside a certain region will only collide with the walls and other objects within the region.
- Collision structures should be optimized with a Octree, BSP, Cell Hash and/or other collision optimization methods.
- An object should not be permitted to be placed inside a solid region.
- Collision detection does not involve checking segment intersections. Instead it relies on the perfection of the engine to prevent this from happening.
Usage
- Create a simulator instance.
CSim2D Sim;
- Create few body sets and add a bunch of bodies to each of them.
- Specify how sets interact.
Sim.SetResponse(&DohickeySet,&TerrainSet, RESPONSE_EVENT);
Sim.SetResponse(&DohickeySet,&BodySet, RESPONSE_EVENT);
Sim.SetResponse(&BodySet,&BodySet, RESPONSE_EVENT | RESPONSE_COLLIDE);
Sim.SetResponse(&BodySet,&TerrainSet, RESPONSE_EVENT | RESPONSE_COLLIDE);
Each response MUST prevent the objects from colliding. The simulator will warn you if you fail!
- Sim.Step();
Physics classes
CSim2D
- void Add(CBodySet2D *Set);
- void SetResponse(CBodySet *,CBodySet *,DWORD Flags); In debug mode this will check to ensure all body sets were added
- void Step();
- bool IsContained(CBodySet2D *Set);
-
-
CBodySet2D
- int AddBody(CBody2D *Body);
- int RemoveBody(CBody2D *Body);
CBody2D
Copyright 2004 © Polyart. All rights reserved.
Designed by Kenneth Manta