[solved] Circular Movement

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
Purple Fedora
Prole
Posts: 23
Joined: Fri Oct 26, 2018 8:41 pm

[solved] Circular Movement

Post by Purple Fedora »

I need to move Something in a circular line like an halfcircle. How do move the object in a curve in the directions right and left.
Could you also explain the math behind it because i don`t understand some things in maths.
Last edited by Purple Fedora on Wed Nov 21, 2018 10:48 pm, edited 1 time in total.
User avatar
pgimeno
Party member
Posts: 3548
Joined: Sun Oct 18, 2015 2:58 pm

Re: Circular Movement

Post by pgimeno »

Take a look here, it explains the math too: https://gamedev.stackexchange.com/quest ... cular-path

Note that angles in Lua need to be entered in radians instead of degrees. You can use math.rad(angle in degrees) to convert an angle to radians.

For a semicircle, your angle can vary for example from 0° to 180°. In LÖVE, increasing the angle means turning right, and decreasing it means turning left.
Purple Fedora
Prole
Posts: 23
Joined: Fri Oct 26, 2018 8:41 pm

Re: Circular Movement

Post by Purple Fedora »

Thanks. But could I use thale's theorem?
Last edited by Purple Fedora on Sun Oct 28, 2018 2:42 pm, edited 2 times in total.
User avatar
pgimeno
Party member
Posts: 3548
Joined: Sun Oct 18, 2015 2:58 pm

Re: Circular Movement

Post by pgimeno »

Thales' theorem Is not really useful for this case. To use it in this case, you would need to determine some straight line that starts at one of the endpoints of the diameter and crosses the circle. in order to choose that line, you would be needing an angle, and then you would need trigonometry again (a tangent) to find the slope from the angle.

You would then need to find the intersection between that line and the circle, which is a very convoluted way. Besides all that, the point would not move at a constant angular velocity.

The sine and cosine formulas are natural for this case, because if you increase the angle uniformly, the position also changes uniformly. It's also probably the simplest way.

The cosine is the X coordinate, and the sine is the Y coordinate, of a point within a circle of radius 1 centred at the origin, where the line from that point to the origin forms a given angle with the X axis. By multiplying them by some value r, it allows you to apply them to a circle of any radius. By adding values to them, you can offset the circle anywhere you want. It doesn't get any simpler, really.
Purple Fedora
Prole
Posts: 23
Joined: Fri Oct 26, 2018 8:41 pm

Re: Circular Movement

Post by Purple Fedora »

I have seen a post on stackexchangle wich explains this and it doesn`t work.
An Excample with Löve2D would really help.
Thanks

Code: Select all

function love.load()
values = {}
values.x = love.graphics.getWidth() / 5
values.y = love.graphics.getHeight() / 5
values.cx = love.graphics.getWidth() / 3
values.cy = love.graphics.getHeight() / 3
values.radius = love.graphics.getWidth() / 7
values.angle = 0

end


function love.update()
love.timer.sleep(1)
values.x = values.cx + math.cos(values.angle) * values.radius
values.y = values.cy + math.sin(values.angle) * values.radius
values.angle = values.angle + 2
end


function love.update()
love.graphics.setColor(1, 1, 1, 1)
love.graphics.rectangle("fill", values.x, values.y, 20, 100)
end
What have i done wrong here?
User avatar
pgimeno
Party member
Posts: 3548
Joined: Sun Oct 18, 2015 2:58 pm

Re: Circular Movement

Post by pgimeno »

You're almost there all by yourself, but there are a few problems in your code.

First, you have love.update twice. The second one should be love.draw.

Second, you're not converting degrees to radians, which means the value you're passing to sin and cos is interpreted as radians. 2 radians are about 115° which is almost 2/3 of a semicircle. That's too much for one single step. Try math.cos(math.rad(values.angle)) and the same for math.sin instead of just math.cos(values.angle).

Third, to get a constant velocity no matter the frame rate, and allow your game to work fluidly, you should use the 'dt' parameter of the love.update function, and multiply it by the angular speed (2 in this case). That will allow you to get rid of the love.timer.sleep call too, which would otherwise freeze all other elements of your game. Note that this will drastically reduce the speed, so you may want to adjust the angular velocity from 2 to something like 20, for example (or however many degrees per second you want the object to move).

Other minor issues apart from that: You don't need to set values.x and values.y in love.load because they will be overwritten in love.update. Also, the centre of the screen is getWidth/Height() / 2, not getWidth/Height() / 3, but I guess that's intentional :)
Purple Fedora
Prole
Posts: 23
Joined: Fri Oct 26, 2018 8:41 pm

Re: Circular Movement

Post by Purple Fedora »

What is math.rad?
Thanks.
User avatar
zorg
Party member
Posts: 3441
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Circular Movement

Post by zorg »

Purple Fedora wrote: Sun Oct 28, 2018 5:02 pm What is math.rad?
Thanks.
pgimeno wrote: Fri Oct 26, 2018 11:51 pm ...
Note that angles in Lua need to be entered in radians instead of degrees. You can use math.rad(angle in degrees) to convert an angle to radians.
...
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.
User avatar
pgimeno
Party member
Posts: 3548
Joined: Sun Oct 18, 2015 2:58 pm

Re: Circular Movement

Post by pgimeno »

Angles can be expressed in several units. The two most used ones are degrees and radians. In degrees, a 360° angle is a whole turn. In radians, 2*π radians is a whole turn.

Lua functions that work with angles (i.e. trigonometric functions) use radians. There are good reasons for that, but I'm not going to go into details lest I confuse you.

math.rad() converts a number in degrees to a number in radians. This makes the result directly usable by Lua. Similarly, math.deg() converts a number in radians to a number in degrees. Thanks to it, when some functions return radians (for example, math.atan2), you can convert the result back to degrees, which will probably allow you to use it.

When you are more experienced handling them, you can even work in radians in your program everywhere, and avoid the conversions.
Post Reply

Who is online

Users browsing this forum: No registered users and 135 guests