This project has moved and is read-only. For the latest updates, please go here.

Sidescroller with camera and collision

Oct 15, 2007 at 5:14 PM
I first must say that I am thoroughly enjoying the new engine! I had been using the previous version, although sparsely, and it was rather simple to make the new one work, which was great! Now that I am starting to get into the finer details of my game, collision is becoming a priority. Unfortunately, I am having some trouble at this point:

My games is a side-scrolling shooter (similar to R-Type). I have a "virtual" camera that is constantly moving. The physics body and geometry for each entity in my game become active if they are within the bounds of the cameras current position. So in essence, the physics components for an entity exist in world coordinates, but I need to translate the sprite to screen coordinates in order for the objects to be seen.

Since spritePos = physPos - cameraPos, the physics body and the sprite are not in the same position as far as the game is concerned. My question is, how will this affect collision and is there a better way to approach this situation? Thanks for any and all responses!!
Oct 15, 2007 at 5:40 PM
I think you just need to set things up correctly. You want to keep all your objects in world coordinates, then just draw them in screen space.

Keep track of your camera offset then just before you render to screen, subtract your camera offset from the the position of your body and draw at that location. This should put your objects into the screen space.

I typed fast though, so I may not have got this correct.
Oct 15, 2007 at 11:27 PM
Yea that's how I did it and it works great. All my entities have a position in world space, which is the position of the farseer body. I created this utility method for drawing my entities :

Vector2 Utils.WorldToScreen(Vector2 pWorldPosition, Vector2 pCameraPosition);

so essentially it takes a world position and the camera position and returns the screen position. You can also do the opposite , ScreenToWorld(Vector2 pScreenPosition, Vector2 pCameraPosition);

I think what you were going to do and what crashlander suggests is the best way to go. You could enable/disable the body/geometries of your entities if their world coordinates is within a certain distance of the camera world coordinate.
Oct 17, 2007 at 4:22 AM
Thanks guys! This method does indeed seem to work, which is quite exciting! I am really enjoying working with the engine and am finally seeing the possibilities of a physics driven world.