Rotation Q

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
hlship
Prole
Posts: 11
Joined: Mon Dec 05, 2011 8:39 pm

Rotation Q

Post by hlship »

I'm doing a top-down shooter game, in the Asteroid vein.

I'm quite the newbie for Love2d.

I'm having a bit of trouble with rotation ... my high-school trig knowledge has atrophied.

What's gotten me is that although we're working in radians, it seems backwards. As the number increases towards 2*PI, it rotates my object CLOCKWISE when it should be counter-clockwise as I understand Radians.

Is it a fair statement that this is due to the world be rotated 180 degrees, so that origin is the upper left (not lower left) BUT I can't quite figure out how that affects clockwise vs. counter-clockwise. Or am I interpreting love.graphics.draw() incorrectly?
Attachments
2018-05-05_1028.png
2018-05-05_1028.png (24.04 KiB) Viewed 2433 times
User avatar
pgimeno
Party member
Posts: 3550
Joined: Sun Oct 18, 2015 2:58 pm

Re: Rotation Q

Post by pgimeno »

hlship wrote: Sat May 05, 2018 2:34 pm What's gotten me is that although we're working in radians, it seems backwards. As the number increases towards 2*PI, it rotates my object CLOCKWISE when it should be counter-clockwise as I understand Radians.

Is it a fair statement that this is due to the world be rotated 180 degrees, so that origin is the upper left (not lower left) BUT I can't quite figure out how that affects clockwise vs. counter-clockwise. Or am I interpreting love.graphics.draw() incorrectly?
This quirk is due to the fact that the Y coordinate grows downwards rather than upwards, therefore mirroring the coordinate system over the horizontal X axis with respect to the normal conventions.
hlship
Prole
Posts: 11
Joined: Mon Dec 05, 2011 8:39 pm

Re: Rotation Q

Post by hlship »

Got it working.

Current (incomplete / incorrect) code:

Code: Select all

-- utilities for dealing with angles and rotation

local abs = math.abs;
local atan2 = math.atan2

local CIRCLE = 2 * math.pi

local angles = {CIRCLE = CIRCLE}

local function normalize(r)
    if r < 0 then
        r = r + CIRCLE
    elseif r > CIRCLE then
        r = math.mod(r, CIRCLE)
    end

    return r
end

function angles.rotateby(r, deltar)
    return normalize(r + deltar)
end

-- angle from fx, fy to tx, ty
function angles.to(fx, fy, tx, ty)
    return normalize(atan2(ty - fy, tx - fx))
end


-- Adjust r towards targetr by amount
-- this may increase or decreate targetr
-- returns normalized value
function angles.turnto(r, targetr, amount)
    local delta = targetr - r

    if delta > 0 and delta <= math.pi then
        r = r + amount 
    else    
        r = r - amount
    end

    return normalize(r)
end

return angles
Attachments
2018-05-05_1619.png
2018-05-05_1619.png (23.62 KiB) Viewed 2397 times
Post Reply

Who is online

Users browsing this forum: No registered users and 65 guests