Camera rotation problems (SOLVED)

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
abcd
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 2
Joined: Thu Dec 26, 2013 8:11 pm
Programming Language of Choice: C++, Python, C#
Location: UK, Cambridge
Contact:

Camera rotation problems (SOLVED)

Post 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; 
}
Last edited by abcd on Sun Jan 12, 2014 4:31 am, edited 1 time in total.
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Camera rotation problems

Post 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?
User avatar
abcd
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 2
Joined: Thu Dec 26, 2013 8:11 pm
Programming Language of Choice: C++, Python, C#
Location: UK, Cambridge
Contact:

Re: Camera rotation problems (SOLVED)

Post 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; 
}
Post Reply