Page 1 of 1

Camera rotation problems (SOLVED)

Posted: Sat Jan 11, 2014 6:18 pm
by abcd
I have been designing/coding a 2D side scroller for a while now. I am now adding the functionality of rotating the camera.

I use the below method to get the position of where each game object should be rendered relative to the camera position (posX and posY).

Code: Select all

void CCamera::mapWorldToScreen(COutput & output, float worldX, float worldY, float* screenX, float* screenY)
{
	*screenX = (worldX-posX)*zoom+output.getScreenX()/2;
	*screenY = (worldY-posY)*zoom+output.getScreenY()/2;	
}


Also note that the image rotation is done separately. It's the position on screen which which I want to map.
I am just not sure how to add this. If anyone could help me out, that would be great.


***EDIT***

Ok so I worked out the math (I think?) for the rotation transformation...
Find the distance to the current position:
hyp = sqrt(posX^2+posY^2)

Then get the new posX and posY like:
posX = cos(newAngle)*hyp
posY = sin(newAngle)*hyp

But I still don't know how to use this to map on to the screen?

***EDIT***
Finally after several hours of fucking around I finally managed to get a result, however the zoom scales disproportionately and i'm still not sure?

Code: Select all

void CCamera::mapWorldToScreen(COutput & output, float worldX, float worldY, float* screenX, float* screenY)
{	
	static float degToRad = 57.29577;

	float sinAng = sin(rotation/degToRad);
	float cosAng = cos(rotation/degToRad);

	float thisX = worldX-posX;
	float thisY = worldY-posY;

	*screenX = (thisX * cosAng - thisY * sinAng)*zoom + output.getScreenX()/2;
	*screenY = (thisX * sinAng + thisY * cosAng)*zoom + output.getScreenY()/2; 
}

Re: Camera rotation problems

Posted: Sun Jan 12, 2014 1:57 am
by Falco Girgis
Have you considered actually using standard matrix multiplications for these mathematics? What you're trying to do would be a trivial set of translate(), rotate(), and scale() calls, times the local coordinates to get the screen coordinates. That is really how these kinds of transforms are represented in graphics.

I can't even really help you here, because I'm not able to visualize or analyze what you're even doing without sitting down and multiplying some matrices out myself... (It's me, not you).

If you used a matrix stack
1) it would simplify your math
2) you could do even more complex transforms
3) you can make it a lot faster computationally by storing intermediate matrices
Finally after several hours of fucking around I finally managed to get a result, however the zoom scales disproportionately and i'm still not sure?
How do you mean disproportionately? Scales more on the X or Y axis than the other axis?

Re: Camera rotation problems (SOLVED)

Posted: Sun Jan 12, 2014 5:05 am
by abcd
I am a fucking moron.

This code totally works. I just forgot to scale the textures. I am not a smart man.

Code: Select all

void CCamera::mapWorldToScreen(COutput & output, float worldX, float worldY, float* screenX, float* screenY)
{   
   static float degToRad = 57.29577;

   float sinAng = sin(rotation/degToRad);
   float cosAng = cos(rotation/degToRad);

   float thisX = worldX-posX;
   float thisY = worldY-posY;

   *screenX = (thisX * cosAng - thisY * sinAng)*zoom + output.getScreenX()/2;
   *screenY = (thisX * sinAng + thisY * cosAng)*zoom + output.getScreenY()/2; 
}