Jump to content
  • Advertisement
Sign in to follow this  
Michael Anthony Wion

[Easy] 2D coordinate space issues

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

This should be fairly simple, but my brain has been malfunctioning as of late.
Look at the image below:


The coordinate at [0,0] represents the top-left corner in screen space. The screen space can move freely in world space, but may not exceed beyond it's boundaries. The target location represents where we want to move the screen space to. However, we want the center of screen space [400, 300] to "land" on the target, NOT the top-left coordinate of screen space. How can this direction vector be calculated?

Am I correct in assuming the following steps?:
- Divide the screen space's width & height by 2 to find it's center (centerScreen = screenSize / 2)
- Subtract this from the target coordinate (directionVector = targetLocation - centerScreen)
- Normalize the resulting vector (directionVector = directionVector.Normalize() )
- Each update frame, move the screen space (screenLocation = screenDirection * screenVelocity)

... Cause if that's right, then that means there must be an error elsewhere in my 2D calculations.

Share this post

Link to post
Share on other sites
1. I cannot see image.

2. If you have working math to make (0, 0) land on the target, just add or subtract (depending what you move) screenSize / 2 before drawing everything.

Share this post

Link to post
Share on other sites
You should be able to see the image now.
So you're saying I should take the top-left corner of screen space, and have that point at the target instead of it's center?
Or should I take the top-left corner, add 1/2 of the screen space's size to it, then point it to the target?
I was doing the latter, though both situations become complicated during certain situations (such as when the target's polarity changes within screen space)

Share this post

Link to post
Share on other sites
i use the following code in my "smart camera" to have the camera follow a focus point

basically the jist is: find the camera position, find the camera center, find the focus position, find how far it is to the focus point, find the vector of the camera center to the focus point, move the camera origin the appropriate distance along that vector to the focus point. For your example, you dont need to worry about a focus radius, you just should jump strait to the focus point (use full distance)

Position focusPosition = (Position) c_PositionMapper.get(entity);
CameraFocus focus = (CameraFocus) c_CameraFocusMapper.get(entity);
ViewPort cameraView = (ViewPort)c_ViewportMapper.get(c_Camera);
Vector2 cPos = cameraView.getOrigin();
Vector2 center = cPos + cameraView.getDimensions() / 2;
Vector2 fPos = focusPosition.getPosition();
float dist,radius;
dist = Vector2.Distance(fPos,center);
radius = focus.getFocusRadius();
if (dist > radius)
Vector2 vec = Vector2.Subtract(fPos, center);
cPos += Vector2.Multiply(vec, dist - radius);

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!