Want to create interactive content? It’s easy in Genially!
AUDIO TUTORIAL
Preetham C
Created on April 17, 2023
Over 30 million people create interactive content in Genially
Check out what others have designed:
Transcript
2D-Rotation
Let's say we are playing an 2D space ship game and the space ship is about to hit a asteroid. So, in order to avoid this collosion the ship has to rotate by some angle theta.
rotated_point.x = point.x * cos(angle) - point.y * sin(angle) rotated_point.y = point.x * sin(angle) + point.y * cos(angle) Applying this to our three points gives us the following shape:(here rotated by 49 degree)
So, what if we had space ship of complex shape? Let's say it looks like this :
So, this sine and cosine operations for these many points would make it slow. What if we rotate the model's x and y axis?
When we talk about the the point (3,2), we are saying that its position is three times the x-axis plus two times the y-axis. The default axes are (1,0) for the x-axis and (0,1) for the y-axis, so we get the position 3(1,0) + 2(0,1). But the axes don't have to be (1,0) and (0,1). If we rotate these axes, then we can rotate every point at the same time.
This is more elegantly expressed in matrix form. To find the matrix version, we can apply the function to the axes (1,0) and (0,1) for angle θ, and then plug the new axes into the columns of our matrix. Let's start with the x axis: (1,0). If we plug it into our function we get: (1*cos(θ) - 0*sin(θ), 1*sin(θ) + 0*cos(θ)) = (cos(θ), sin(θ)) Next, we plug in the y axis: (0,1). This gives us: (0*cos(θ) - 1*sin(θ), 0*sin(θ) + 1*cos(θ)) = (-sin(θ), cos(θ)) Plugging these new axes into the columns of a matrix gives us our 2D rotation matrix: [cos(θ) -sin(θ) sin(θ) cos(θ)]