Jump to content
  • Advertisement
Sign in to follow this  
jdub

2D Camera question

This topic is 3700 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am building a camera for a 2D game I am creating. So far I have this function TransformToScreen which takes an objects position in the 2D world and transforms it into camera space so it can be rendered:

 public Vector2 TransformToScreen(Vector2 worldPos)
        {
            Vector2 screenPos = worldPos - cameraPos;

            return screenPos;
        }

The problem is that I want to make it so that the function can also take the camera's rotation into account also:
public Vector2 TransformToScreen(Vector2 worldPos)
        {
            Vector2 screenPos = worldPos - cameraPos;
            
            //find the Position of the object based on the camera rotation here:
            //...
            return screenPos;
        }

 
I'm lost on how I can do this. A little help would be good.

Share this post


Link to post
Share on other sites
Advertisement
- Translate the point so the camera is the origin, then rotate the point around the origin by the rotation of the camera. To do this, use transformation matrices. In this case, what you want is a translation matrix multiplied by a rotation matrix. Note that to represent translation as a matrix, you'll need to use affine transformations.

If all you care about is the result, then this should do the trick. However, I encourage you to learn this stuff. It's quite useful and is much more flexible and powerful than meorizing special cases :)

x' = (object.x - camera.x) * cos(θ) − (object.y - camera.y) * sin(θ)
y' = (object.x - camera.x) * sin(θ) + (object.y - camera.y) * cos(θ)



Quote:
Original post by PCN
Why would you have rotation with a 2D camera? Or am I missing what your trying to explain? 0_o


I assumed he wanted to rotate the camera around what would be his 2D plane's normal in 3D.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!