Tuesday, July 9, 2013

Physics and Graphics Objects

Physics and Graphics Objects

Physics Objects

Physics objects are made up:
  • Simple Shape
  • Position
  • Angle
  • Mass 

Set up Physics World

physics = new Physics(this, width, height);
physics.setDensity(1.0);
physics.setDensity(0.0);  // Objects won't be affected by physics and they will not move

Creating Physics Objects

Body box = physics.createRect(top-left-X, top-left-Y, bottom-right-X, bottom-right-Y);
Body ball = physics.createCircle(center-X, center-Y, radius);

Graphics Objects

Graphics objects are composed of:
  • Complex Shape
  • Colors
  • Images

Mapping Physics to Graphics

The most important thing is link physics to graphics i.e get position and rotation of physics bodies and apply that.

Vec2 pos = physics.worldToScreen(body.getWorldCenter()); // get object's position
float angle = physics.getAngle(boid);

pushMatrix();
  translate(pos.x, pos.y);
  rotate(-radians(angle));
  image(ballImage, 0, 0, ballSize, ballSize);
popMatrix();

Scaling Units

One meter in physics = one pixel in graphics


No comments:

Post a Comment