Transforms in Box2D
- by user1264811
I'm attempting to implement a camera in my game. I had it working for regular objects, but I began using Box2D and obviously things changed a bit. I have a Body object that I want to draw at the center of the screen. Basically what I'm doing is subtracting the viewportX and viewportY to the Body.
I use this code that currently is not working as it should:
public void paint(Graphics2D g, int viewportX, int viewportY) {
Transform xf = new Transform();
// m_body is the Body object
xf.set(m_body.getTransform());
// Here what I attemp to do is take the transform and alter it
// by the viewportX and Y, which is something like **-240, -150**.
// Why is it negative? Because an object has coordinates 500, 300 would be displayed
// at 160, 150 when the subtraction is done.
// With the DrawUtils.toScale(), it's just how I convert the units from JBox2D units
// to my units.
Vec2 v = Transform.mulTrans(xf, new Vec2(DrawUtils.toScale(-viewportX),
DrawUtils.toScale(-viewportY)));
// Set the new transform to the new vector. Keep the old angle.
xf.set(v, xf.q.getAngle());
g.setColor(Color.white);
// I know for a fact that the following method works 100%. It correctly displays
// my object, just that it doesn't follow it.
for (Fixture f = m_body.getFixtureList(); f != null; f = f.getNext())
DrawUtils.drawShape(f, xf);
}
Hopefully I didn't over comment this and you understand my problem. I don't want to alter the actual physics position of the object, I just want to display it in the center.