Rotate quads

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
mojojojo
Prole
Posts: 4
Joined: Sun Sep 02, 2018 6:15 pm

Rotate quads

Post by mojojojo »

Hello I need help because I don't know what's wrong with my code or if this can be be fixed.

I want to draw quad, and set the symetric center so that it looks like it's spinning. I can do that with pictures but not quads. I looked on Google and apparently we can only move around the actual center, like if the symetric center revolves itself aroundthe real one. Weird stuff. I'm noob btw.

But the most important is I'd like to move quads at will and let love2d change the center automatically so it keeps rotating normally. I'll join main.lua so that someone helps or points out why I'm being clueless. It's not importantbutI'd like to draw cool figures. Thanks in advance.
main.lua
(2.35 KiB) Downloaded 102 times
TheHUG
Citizen
Posts: 61
Joined: Sun Apr 01, 2018 4:21 pm

Re: Rotate quads

Post by TheHUG »

The issue is that when you are reversing your translation, you are reversing by a different amount than the initial translation, so instead of

Code: Select all

love.graphics.translate(-150, -150)
do

Code: Select all

 love.graphics.translate(-rotatex, -rotatey)
, which reverses

Code: Select all

 love.graphics.translate(rotatex, rotatey) 
one point to consider going forward is that love.graphics.rotate rotates the entire image - so if you want to add anything else to your scene it won't be appropriate.
I haven't used it yet, but maybe Transform from love.math.newTransform has what you need. alternatively, you can write the math for it yourself.

I have some more general coding advice if you want to hear it,
Make your code easy to read for others, not just in case you need help, but also in case you come back to it six months from now! the variables ax_{}_{x/y} aren't very descriptively named, a different name would make their purpose more clear.
Indentation! get yourself an editor that automatically indents for the sake of readability (this lets you see more easily what lines are inside a function, table, etc). I use emacs, though it has a steep learning curve with different keyboard shortcuts than most programs. If you're on windows I believe notepad++ is a nice, minimalistic solution for coding.

the DRY principle: Don't Repeat Yourself

Code: Select all

...
rotatex = (axe_5_x+axe_8_x)/2
rotatey = (axe_5_y+axe_8_y)/2 
...
function love.update(dt)
    ...
    rotatex = (axe_5_x+axe_8_x)/2
    rotatey = (axe_5_y+axe_8_y)/2
    ...
end
Instead, abstract it to a function such as :

Code: Select all

function set_rotation_center()
    rotatex = (axe_5_x+axe_8_x)/2
    rotatey = (axe_5_y+axe_8_y)/2
end
(though in this case you can probably just delete the first one, as you won't need it until love.draw is called)

finally, what's a little harder to explain/give examples, but is really important when you build something of substantial size is to minimize reliance on global state (the collection of variables available locally). If you have a lot of global variables which can be modified by a lot of different functions it can be difficult to track what line of code brought about what change. for example, If rotatex, rotatey are only required in love.draw, calculate them there and keep them inside the scope of that function as local variables.

good luck!
User avatar
zorg
Party member
Posts: 3441
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Rotate quads

Post by zorg »

Also, the love.graphics.draw function, that you use to draw basically everything, has parameters you can use for transformations; position (translation), orientation (rotation), scale, rotation offset (origin offset) and shearing factors.
From love.graphics.draw:
The origin is by default located at the top left corner of Image and Canvas.
All scaling, shearing, and rotation arguments transform the object relative to that point.
It's possible to rotate an object about its center by offsetting the origin to the center.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 71 guests