pygame 3d graphics rotation issue

this may be just as much a maths problem than a code problem. I decided to learn how 3d engines work, and i'm following http://petercollingridge.appspot.com/3D-tutorial/rotating-objects this guide, but converting the code to python. in the function for rotating on the Z-axis, my code looks like this:

def rotate_z(theta):
theta=math.radians(theta)
for i in ords:
    i[0]= i[0]*math.cos(theta) - i[1]* math.sin(theta)
    i[1]= i[1]*math.cos(theta) + i[0]* math.sin(theta)

which rotates the node the appropriate amount, but over maybe 5 seconds, or 150 frames, the nodes start to slowly move together, until, about 20 seconds in, they coalesce. my initial thought was that it was a round down on the last two lines, but i am stuck. any ideas anyone?

Upvotes: 0

Views: 91

Answers (1)

laurencevs
laurencevs

Reputation: 923

It looks like the problem is that you're changing the value of i[0] when you need the old value to set i[1]:

i[0]= i[0]*math.cos(theta) - i[1]*math.sin(theta) <-- You change the value of i[0]
i[1]= i[1]*math.cos(theta) + i[0]*math.sin(theta) <-- You use the changed value of i[0], not the original

So the value of i[0] gets replaced, when you still want to keep it.

You can solve this by using separate variables (as Peter Collingridge does):

for i in ords:
    x = i[0]
    y = i[1]
    i[0]= x*math.cos(theta) - y*math.sin(theta)
    i[1]= y*math.cos(theta) + x*math.sin(theta)

This way, you should not get the "feedback loop" which results in the points gradually floating together.

Upvotes: 2

Related Questions