[Easy] 2D coordinate space issues

Started by
3 comments, last by Michael Anthony Wion 12 years, 1 month ago
This should be fairly simple, but my brain has been malfunctioning as of late.
Look at the image below:

problem.png

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.
Advertisement
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.
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)
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);
vec.Normalize();
cPos += Vector2.Multiply(vec, dist - radius);
cameraView.setOrigin(cPos);
}
Thanks, that puts it into perpective. I was aiming for something quite similar, so that really helped.

This topic is closed to new replies.

Advertisement