Pygrr - who you looking at?

 As I promised, today's post will be about rotation! Well, I say 'today', but this is a two-post day, as unfortunately I haven't been able to post recently, so here's making up for it!

So! Rotation... How should we do it? Well, if you have the model as a polygon (the only current type of model, stay tuned for other model types...), all you need to do is rotate the shape, by changing the coordinates around. There's some scary maths here!

Imagine that you have an object, and you want to rotate it clockwise. 

Here's our object, the little dot being the "origin" (the anchor it rotates around):


Step 1: start with corner A, create a circle with radius of the distance to the origin.


Move it along the circle, to the desired angle of movement.


Repeat for corner B...


...and corner C.


Et voila!


 

 







Written down, this maths is a bit more confusing. And in code, even more confusing...

Stupidly (this is turning into a habit!), I messily wrote the code several times, causing me to choose to delete it all and rewrite it, as I couldn't see it working very well in the future! Scalability and maintainability, after all...

This delete and rewrite cycle repeated several times, and here's some funny bugs I got!

Draft #1: The "wiggler"

Draft #2: The 'I have no idea what's going on'

Draft #3: The "expander"



And, finally, 3 hours in, we got this:


There were still some errors with irregular shapes, which you can see I fixed here:

That was a rollercoaster! Here's all the commands added today:

point_towards(x,y) # rotates object so it is pointing towards the coordinates given

set_rotation(angle) # sets the object's rotation

rotate(angle) # rotates the object clockwise

rotate_left(angle)  # rotates the object anti-clockwise

rotate_right(angle) # rotates the object clockwise

rotate_clockwise(angle) # rotates the object clockwise

rotate_anticlockwise(angle) # rotates the object anti-clockwise

get_rotation() # returns the rotation of the object (always between 0 and 360)

Next step: input!

Isaac, over and out...

Popular posts from this blog

Messing around with procedural art - Turtle graphics

The fossils of Morocco | Mosasaurus beaugei

Blog post #0 - Hello, world!