Handmade Hero»Forums»Code
popcorn
70 posts
æ„›
[Day 91] Rotation problem
I been stuck on this for a bit and since I'm not too great at math, I am wondering why it's not rotating around the center circle.

Here's a video:
https://www.youtube.com/watch?v=IRa4U2gZLLE

Both of squares seem to be circling a center of their own and end up hitting the first block (looks kind of cool but it's wrong).

I had my all of my code on github but I took it off because it might cause problems with Handmade hero. Here's the code in pasebin,but
before looking, peeking or whatever, I want to let you know that I'm using Linux, SDL by David Kim on Mint Linux (sorry if I'm wrong) and I also try to avoid copying Casey's code so the code is different from the actual source with the almost same concepts (I say almost because I think I'm doing some things wrong).

http://pastebin.com/4BP5NZRL


Thanks!
Casey Muratori
801 posts / 1 project
Casey Muratori is a programmer at Molly Rocket on the game 1935 and is the host of the educational programming series Handmade Hero.
[Day 91] Rotation problem
The video seems to be marked private?

Slightly off topic: doesn't the fact that HH preorders now come with a GitHub access code mean you can still use GitHub, you just do it in the Handmade Hero repository or whatever? And it remains private? I feel like that is how that works? You just fork the cpp repo, and then replace whatever you want, or something like this?

- Casey
Bryan Taylor
55 posts
[Day 91] Rotation problem
Edited by Bryan Taylor on Reason: I'm terrible at linear algebra, apparently.
Without video it's hard to be sure (and I'm not really sure what's going on in your code with player.x.x), but from your description it sounds like you're rotating around the wrong center.

When you apply a rotation, it *always* rotates around the origin (0, 0). So if you want to rotate around some other point, you need to translate to the origin, do your rotation, and translate back.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
vec2 p = ...
vec2 center = ...

// Translate the center of rotation to the origin.  (p - center)
float x = p.x - center.x;
float y = p.y - center.y;

// Do the rotation
float angle = ...
float ct = cosf(angle);
float st = sinf(angle);

vec2 new_p;
new_p.x = (x * ct) + (y * -st);
new_p.y = (x * st) + (y * ct);

// Translate back.  (p + center);
new_p += center;

(If you do a lot of these, you'll want to bake them into a matrix instead. But it's the same process.)

What you are actually rotating is not the point, but the *vector* to that point from the center of rotation.
popcorn
70 posts
æ„›
[Day 91] Rotation problem
Oops I forgot to push "publish", sorry about that.

Thanks, btaylor,2401, I'll try it out!!