c# - Camera offest when following body -
when moving object around, camera doesn't remain centered on object.
i can't seem locate bug. please help.
initial-
`convertunits.todisplayunits(body.position)-(camera._screencenter- camera._cameraposition)`
this returns zero.
after movement changes.
i using farseer physics engine 3.5 , moving body using force.
body.applyforce();
this camera class-
class camera { private static matrix _view; public static vector2 _cameraposition; public static vector2 _screencenter; private static float _zoom; private static float _rotation; public static void load(graphicsdevicemanager _graphics) { _view = matrix.identity; _cameraposition = vector2.zero; _screencenter = new vector2(_graphics.graphicsdevice.viewport.width / 2f, _graphics.graphicsdevice.viewport.height / 2f); _rotation = 0f; _zoom = 1.0f; } public static void handleinput(keyboardstate state) { if (state.iskeydown(keys.add) && _zoom < 1.0f) _zoom += 0.1f; if (state.iskeydown(keys.subtract) && _zoom > 0.1f) _zoom -= 0.1f; update(); } public static void update() { _view = matrix.createtranslation( new vector3(_cameraposition - _screencenter, 0f)) * matrix.createrotationz(_rotation) * matrix.createscale(new vector3(_zoom, _zoom, 1)) * matrix.createtranslation(new vector3(_screencenter, 0f)); } public static void follow(body body) { if (body != null) _cameraposition = _screencenter - convertunits.todisplayunits(body.position); } public static matrix getview() { return _view; } }
your _screencenter
origin of camera, , not being calculated correctly when make matrix.
try:
return matrix.createtranslation(new vector3(-position, 0.0f)) * matrix.createtranslation(new vector3(-origin, 0.0f)) * matrix.createrotationz(rotation) * matrix.createscale(zoom, zoom, 1) * matrix.createtranslation(new vector3(origin, 0.0f));
Comments
Post a Comment