————————————————————————————————————————————————————————
Labels
- javaSE 1.4
- 2d
- engine
- collisions
- forces
Introduction
Homepage: http://www.cokeandcode.com/phys2d/
Try there demo box! It is obvious than this tutorial.
phys2d is physical engine. It means that it take care about collisisns and movment and rotation of objects, and many more.
Main thing you need to care about is to:
- set objects
- add forces, so objects can move
- handle collisions (quite simple, see below)
ATTENTION! phys2d don't have methods to draw, so you need to draw objects yourself. Or use next code (only for rectangles!):
for (int i=0;i<bodies.size();i++) {
Body body = bodies.get(i);
Box box = (Box) body.getShape();
Vector2f[] pts = box.getPoints(body.getPosition(), body.getRotation());
Vector2f v1 = pts[0];
Vector2f v2 = pts[1];
Vector2f v3 = pts[2];
Vector2f v4 = pts[3];
g.setColor(Color.black);
g.drawLine((int) v1.x,(int) v1.y,(int) v2.x,(int) v2.y);
g.drawLine((int) v2.x,(int) v2.y,(int) v3.x,(int) v3.y);
g.drawLine((int) v3.x,(int) v3.y,(int) v4.x,(int) v4.y);
g.drawLine((int) v4.x,(int) v4.y,(int) v1.x,(int) v1.y);
}
Step-by-step
- add phys2d.jar to your project
- add World object
- configure World
- create Body
- configure Body
- add Body
- repeat 2 last steps for more bodies
Properties
I'll put simple code that show example how it can be used.
Vector
So getX() equal width, and getY() - height.
World
Object that hold everything and execute everything inside it. Every coordinate inside it is in float.
- gravity - direction (and power) where bodies will fall
- hint 1 - can be changed during play
- hint 2 - can be set to any direction (e.g. objects will not fall, but move to upper left corner)
- hint 3 - if (0, 10) then like at Earth; if (0, 0) - like in space/top view games
- world = new World(new Vector2f(0.0f, 10.0f), 10, new QuadSpaceStrategy(20,5));
- add(Body) - you will use it to add other objects
- step() - one step, that move/collides bodies
Shapes
Every object in World must have shape. You can create own shape or use premaded shapes:
- Box (probably you need this one :)
- Circle
- Line
- Polygon
Body
Bodies is objects in World, so everything will happened to them. Also it is possible to create reference to object that represents this body in game (setUserObject())
Bodies can be static or not. Static just means that body will not move (for faster processing).
- `new Body("player", new Box(20, 40), 100);
- shape - geom representation of body (usually used for collisions)
- mass - mass of body (the more mass, the more force need to move it)
- addForce (force that will apply for body)
- hint 1 - for moving side small force should be applied continually (e.g. mass 100, force shuold be (300,0))
- hint 2 - for jumping one-time force should be applied (e.g. mass 100, force shuold be (0,-200000)) NOTE: force is so big, but it is right.
- create link to non-World object
- `body.setUserData(player);'
- position - you can adjust position of object any time in game (or just at the beggining)
Collisions
When World processing step(), CollisionEvent can occure one or several times. CollisionEvent have coordinate and reference to bodies that collide.
You can add listener to catch CollisionEvents: world.addListener(this); and then
public void collisionOccured(CollisionEvent event)
{
ceList.add(event); //for example collect all collision events, so later you can handle them
}