Public Group

# 2D Camera question

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

## 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 on other sites
Why would you have rotation with a 2D camera? Or am I missing what your trying to explain? 0_o

##### Share on other sites
- 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 PCNWhy 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.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 11
• 11
• 15
• 11
• 11
• ### Forum Statistics

• Total Topics
634149
• Total Posts
3015834
×