Jump to content
  • Advertisement
Sign in to follow this  
zer0sum

Need Screen Space from WorldSpace Coords (2D Cam)

This topic is 2839 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

The problem I am having is that I need to get Screen Space coordinates out of my 2D camera. It's like the solution is on the tip of my tongue but I can't get it quite right. How would you complete the "GetScreenSpaceFromWorldSpace" function at the bottom of this codeblock?


public class Camera2d
{
protected float _zoom; // Camera Zoom
public Matrix _transform; // Matrix Transform
public Vector2 _pos; // Camera Position
protected float _rotation; // Camera Rotation

public Camera2d()
{
_zoom = 1.0f;
_rotation = 0.0f;
_pos = Vector2.Zero;
}

// Sets and gets zoom
public float Zoom
{
get { return _zoom; }
set { _zoom = value; if (_zoom < 0.1f) _zoom = 0.1f; if (_zoom >= 2.0f) _zoom =2.0f; } // Negative zoom will flip image
}

public float Rotation
{
get { return _rotation; }
set { _rotation = value; }
}

// Auxiliary function to move the camera
public void Move(Vector2 amount)
{
_pos += amount;
}
// Get set position
public Vector2 Pos
{
get { return _pos; }
set { _pos = value; }
}

public Matrix get_transformation(GraphicsDevice graphicsDevice)
{
_transform = // Thanks to o KB o for this solution
Matrix.CreateTranslation(new Vector3(-_pos.X, -_pos.Y, 0)) *
//Matrix.CreateRotationZ(Rotation) *
Matrix.CreateScale(new Vector3(Zoom, Zoom, 1)) *
Matrix.CreateTranslation(new Vector3(Game1.Instance.GraphicsDevice.PresentationParameters.BackBufferWidth * 0.5f, Game1.Instance.GraphicsDevice.PresentationParameters.BackBufferHeight * 0.5f, 0));
return _transform;
}

public Vector2 GetScreenSpaceFromWorldSpace(Vector2 worldCoordinate)
{
Vector2 ScreenPosition = new Vector2();

// magic here?

return ScreenPosition;
}
}

Share this post


Link to post
Share on other sites
Advertisement
World space coord * view matrix = eye space coord
eye space coord * projection matrix = clip space coord
clip space coord / clip space coord.w = NDC (normalized device coordinates, from -1 to 1 on each axis)

Shift -1/1 range to 0/1, then scale by the number of pixels, and the result is screen space.

Share this post


Link to post
Share on other sites

World space coord * view matrix = eye space coord
eye space coord * projection matrix = clip space coord
clip space coord / clip space coord.w = NDC (normalized device coordinates, from -1 to 1 on each axis)

Shift -1/1 range to 0/1, then scale by the number of pixels, and the result is screen space.


In this case I am passing the _transform matrix from the camera into a spritebatch. If I had a 3D camera I would just use Viewport.Project. But I don't have that here; what would you do using the transform matrix posted in the code to achieve this result?

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!