I'm working on a 2D sprite-based strategy-type game (ok, full disclosure: it's a tower defense game), and I'm having a problem getting the actors to behave correctly when I move the camera.
I've got a Camera
class, which contains data and methods for moving the camera and exposing its' current position. When I draw the background, I offset the background thusly (and it works great):
var backPos = new Vector2(background_bounds.X - camera.CameraPositionX,
background_bounds.Y - camera.CameraPositionY);
spriteBatch.Draw(background_texture, backPos, Color.White);
My sprites each have a GraphicsComponent
that directs the rendering. I've been playing around with different combinations, but none seems to give the proper (expected behavior); I lose collision detection (done via my CollisionComponent
object), or the physics misbehave, etc, because actor's position is different than what the other components expected. Here's the GraphicsComponent.Draw(...Camera camera)
:
//...snip
var adjustedPosition = new Vector2(actor.Position.X - drawOffsetX,
actor.Position.Y - drawOffsetY);
//Uncommenting this line doesn't work either - just makes things seem worse
//actor.Position = adjustedPosition;
DrawContext.Draw(actor.Texture, adjustedPosition,
actor.SpriteBounds, DrawColor);
How do you folks approach applying camera offsets to sprites in a way that avoids exposing the camera to every component? Do you offset the actual position by the camera's position, or do you simply render the position with the offset, and compensate elsewhere?