Log In  

Cart #40228 | 2017-05-06 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA
16

Just a simple little 2D-rotation example.

The function takes a 2D position (X,Y) and rotates it by ANGLE. CX,CY represents the center/pivot point.

The lines of code in the middle are the rotation matrix:

local rotx=cosa*x-sina*y
local roty=sina*x+cosa*y

This rotates a point around the origin. Before and after doing so, we offset our position by our center coordinate - this lets us rotate around any arbitrary pivot point.

For bonus points: When you're rotating lots of objects by the same angle (like a camera rotating, or a complex prop rotating), you can re-use your SINA and COSA values.

P#40230 2017-05-05 22:07 ( Edited 2017-05-06 02:12)

Nice! I have a serious problem tho. I'm trying to rotate my car (that I've drawn with line()), and the points all keep constricting around the center. Do you know why this is happening?

I've got your code exactly and this is what I'm using to rotate points.

car.body.a.x = rotate_point(car.body.a.x,car.body.a.y,angle)[1]
P#101056 2021-11-30 01:28 ( Edited 2021-11-30 01:30)

cause you should never use previous rotation position to compute next.
what you experience is loss of precision frame after frame.

to fix, keep original car coordinates, use an angle that gets modified over time and use that angle to compute screen/world coordinates

P#101070 2021-11-30 08:43 ( Edited 2021-11-30 08:44)

Useful stuff, @2darray. Can you make a routine now to determine collision between them and concatenation for multi-edged objects such as triangles and squares ?

Gold star, 10-vertices.

P#101115 2021-11-30 18:48 ( Edited 2021-11-30 18:50)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-03-28 21:36:28 | 0.014s | Q:22