Sync rotation of a hadron collider shape to a rectangle with a moving offset (rotate origin)

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Marty
Citizen
Posts: 89
Joined: Mon Dec 04, 2017 1:47 am
Location: Germany

Sync rotation of a hadron collider shape to a rectangle with a moving offset (rotate origin)

Post by Marty » Sun Apr 01, 2018 10:11 pm

Hello dear lövers,

I have a rotation problem when it comes to moving offsets and HC. I kinda have a solution to this, but I consider this not a smart solution, so I wonder what you think about it and maybe you know how to make it better.

Basically, when I draw a rectangle while the offset changes (moving rotate origin) and the rotation itself changes, too, the rectangle will move it's way like I want it to. Now this rectangle gets a hadron collider (HC) shape that I want to be synced on the rotation. For this I call shape:setRotation on update.

This will cause a problem, because I sync it after moving the rotation origin, so my HC shape (blue in my example) does not rotate and keep up with my rectangle (green in my example).

The only solution I came up with is resetting the rotation of my shape to 0 before modifying the origin to sync the rotation again. In summery I get another setRotation call that I really dislike, because I fear for some serious performance drop. I have two major questions now:

1. How expensive is a additional rotation-reset call (it's out-commented in the code below) on many game objects
2. If the answer to 1. is "expensive", is there a way (math is fun) to do this in only one shape:setRotation call?

Here is the code:

Code: Select all

hc = require "lib.hc"

local x, y, w, h -- rectangle location and size
local ox, oy -- rectangle absolute origin
local deg -- degree of rotation around origin
local vel -- speed of the animation

local collider -- HC collider
local shape -- HC shape

function love.load(args)
    x, y, w, h = 250, 250, 100, 100 -- rectangle location starts centered on the left side, this won't be changed
    ox, oy = 300, 300 -- absolute origin starts at center of the rectangle, this will be changed
    deg = 0 -- no rotation on start
    vel = 20 -- moderate speed

    collider = hc(64)
    shape = collider:rectangle(x, y, w, h)
end

function love.update(dt)
    --shape:setRotation(0, ox, oy) -- reset the rotation of the shape before changing the offset !!! ANY WAY TO SOLVE IT WITHOUT THIS ADDITIONAL setRotation CALL?
    ox = ox + vel * dt -- move absolute origin to right
    deg = deg + vel * dt -- increase rotation
    if deg <= 0 or deg >= 180 then vel = vel * -1 end -- invert velocity after half cycle
    shape:setRotation(math.rad(deg), ox, oy) -- sync the rotation of the shape to the rotation of my rectangle
end

function love.draw()
    love.graphics.push()
    love.graphics.translate(ox, oy) -- move to absolute origin
    love.graphics.rotate(math.rad(deg)) -- rotate by the changing rotation value

    love.graphics.setColor({ 255, 0, 0 }) love.graphics.circle("fill", 0, 0, 3) -- draw a red dot for the absolute origin that is moving

    love.graphics.setColor({ 0, 255, 0 }) love.graphics.rectangle("line", x - ox, y - oy, w, h) -- draw the greem rectangle with the proper rotation
    love.graphics.pop()

    love.graphics.setColor({ 0, 0, 255, 100 }) shape:draw("fill") -- draw the HC shape
end

Thanks in advice.
Attachments
game.love
(94.25 KiB) Downloaded 34 times
Visual Studio Code TemplateRichLÖVE Mobile (AdMob+UnityAds+PlayGamesServices+GameCenter)Add me on Discord

───▄▀▀▀▄▄▄▄▄▄▄▀▀▀▄───
───█▒▒░░░░░░░░░▒▒█───
────█░░░░░░░░░█────
▄▄──█░░░▀█▀░░░█──▄▄
█░░█▀▄░░░░░░░▄▀█░░█

lachlaan
Prole
Posts: 30
Joined: Sun Jun 30, 2013 7:23 pm

Re: Sync rotation of a hadron collider shape to a rectangle with a moving offset (rotate origin)

Post by lachlaan » Wed Apr 04, 2018 2:54 pm

Not sure if still a relevant question, but i think you're meant to update the shape's origin/center and then omit rotation coordinates as it's meant to just rotate around its own center and translate along the trajectory. As it stands your green rectangle is both translating and rotating on itself while the shape is rotating itself around the center of the rectangle, as if trying to orbit it, but lagging behind.

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 27 guests