Animation of a sprite with a pygmy in an elliptical path

This is pygame 1.9 on python 2.6 ..

Here is a screenshot of what is currently being drawn in my “game” to give some context. Here is the code.

It should be a moon orbiting the Earth (I’m not trying to create a real simulation or anything else, I just use the setting to play and learn pygame). These are 2 circles, and the moon is an elliptical orbit around the Earth. My final game is for the moon to follow it in an orbit around the Earth, but I want to use keyboard controls later to adjust the shape of the moon’s orbit.

I really need help figuring out how to get the moon to follow the path, I probably could think of everything else.

+4
source share
1 answer

Well, here's how you create points along an ellipse:

for degree in range(360): x = cos(degree * 2 * pi / 360) * radius * xToYratio y = sin(degree * 2 * pi / 360) * radius 

(x,y) will follow the ellipse centered at (0,0) , with radius y equal to radius and radius x xToYratio . In your case, you probably want the degree somehow related to time.

EDIT: you can also do this:

 for degree in range(360): x = cos(degree * 2 * pi / 360) * xRadius y = sin(degree * 2 * pi / 360) * yRadius 

where xRadius is half your rectangular width and yRadius is half the height of your rectangle. Visualize it intuitively - you have a circle and you stretch it (i.e., scale it, i.e., multiply it) so that it is larger than the horizontal and vertical rectangle.

+5
source

Source: https://habr.com/ru/post/1309822/


All Articles