Jump to content

  • Log In with Google      Sign In   
  • Create Account


Pygame - 2D world rotation?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 xhh   Members   -  Reputation: 129

Like
0Likes
Like

Posted 21 September 2012 - 08:30 PM

Hi, I'm creating a 2D platformer game using Pygame. I [sort of] know how to move the world around the player as if the screen was following him. What I need to do now is rotate the entire world around him. I've done this in Game Maker using view_angle, but I'm not exactly sure how to do this using Pygame.
I have one idea for solving this: I was thinking of rotating the image of all the game objects and moving their position using circular motion where the radius of the circle is the distance from the player. If this sounds good, then how would I go about doing circular motion?

Thanks in advance for any helpful replies.

Sponsor:

#2 Álvaro   Crossbones+   -  Reputation: 12365

Like
0Likes
Like

Posted 22 September 2012 - 02:10 PM

I will discuss only rotation around the origin. If you need to rotate around some other center, subtract the center first, apply the rotation around the origin and then add the center back.

import math

def rotate(x,y,alpha): # Rotate (x,y) around the origin by an angle alpha (in radians!)
	x,y = math.cos(alpha)*x - math.sin(alpha)*y, math.sin(alpha)*x + math.cos(alpha)*y
	return (x,y)

I don't use Python, but that seems to work. I am sorry if this code is not idiomatic.

Alternatively, if you are familiar with complex numbers (which in Python are implemented in cmath), you can use them to represent coordinates in 2D (point (x,y) becomes x+y*1j). Instead of thinking of the rotation as an angle alpha, think of it as the complex number z=exp(alpha*1j), which can also be expressed as z=cos(alpha)+sin(alpha)*1j. The rotation is now just multiplication by z.

import cmath

def complex_rotate(z,alpha):
	return z * cmath.exp(alpha*1j)

# Example:
# print complex_rotate(complex(2,3),1.57079632679489661922)

(NOTE: "1j" seems to be how one refers to sqrt(-1) in Python)

Edited by alvaro, 22 September 2012 - 02:11 PM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS