Hello, everyone. I discovered Löve2D a few days ago and finally decided to get on with it.
As I mainly work with the Corona SDK, I guess it shouldn't be that hard to learn.
As a first project, I would like to port my iOS/Android game Ballzone to Desktop.
Here's the download link : https://itunes.apple.com/en/app/id820399777
Even though I feel a bit lost concerning Löve2D load, draw and update functions, I'm starting to get used to it.
Now, I need to get an circle body working.
Basically, there's a ball inside a circle, which I can move using the keyboard (setLinearVelocity).
The thing is, I need it not to be able to leave the circle's bounds.
Ideally, I'd like to make the circle's outline a physic body.
Has someone got an idea about this ? Or another workaround to that problem ?
Thank you in advance for your answers !
Circle outline as a physic object ?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: Circle outline as a physic object ?
This should work:
http://love2d.org/forums/viewtopic.php?f=5&t=77140
http://love2d.org/forums/viewtopic.php?f=5&t=77140
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
Re: Circle outline as a physic object ?
One approach is to create a "ring" of static boxes.
Generally, you want to attach the box fixtures to one body:
Generally, you want to attach the box fixtures to one body:
Code: Select all
local r1 = OUTER_RADIUS
local r2 = INNER_RADIUS
local segments = NUMBER_OF_RING_SEGMENTS
for i = 1, segments do
local a1 = math.rad(i/segments*360)
local a2 = math.rad((i + 1)/segments*360)
local x1 = math.cos(a1)*r1
local y1 = math.sin(a1)*r1
local x2 = math.cos(a2)*r1
local y2 = math.sin(a2)*r1
local x3 = math.cos(a2)*r2
local y3 = math.sin(a2)*r2
local x4 = math.cos(a1)*r2
local y4 = math.sin(a1)*r2
-- create a box shape with vertices:
-- (x1, y1, x2, y2, x3, y3, x4, y4)
end
Re: Circle outline as a physic object ?
Thank you for your answers ! I finally got it working.
Ivan, if you need some French translations... Just tell me if I can be of any help.
Ivan, if you need some French translations... Just tell me if I can be of any help.
Re: Circle outline as a physic object ?
Using love.physics is however extremely overpowered for what you are doing (newtonian physics - colliding perfect circles with each other), checking for collisions is as easy as comparing the (sqrt of the) distance (dx^2 + dy^2) with the squared radii; colliding them is some very simple vector calculations.
Re: Circle outline as a physic object ?
Hello, S0lll0s, and thank you for your answer.
I understand that using physics for this kind of job is maybe too intensive.
As for the origin-ball distance comparison with the radius of the circle, how could I implement this into my actual project ?
If the distance between the origin of the circle and the ball is greater than the circle's radius, then... do what ? Change the ball's x and y pos so it stays in the circle ? Apply a force ?
I apologize for asking such dumb questions, but I think it'll take me quite a while to get used to Löve2D.
Thank you in advance.
I understand that using physics for this kind of job is maybe too intensive.
As for the origin-ball distance comparison with the radius of the circle, how could I implement this into my actual project ?
If the distance between the origin of the circle and the ball is greater than the circle's radius, then... do what ? Change the ball's x and y pos so it stays in the circle ? Apply a force ?
I apologize for asking such dumb questions, but I think it'll take me quite a while to get used to Löve2D.
Thank you in advance.
Re: Circle outline as a physic object ?
Well, I don't know your game, but I assume the circle you are talking about here is controlled by the mouse? If so, then like this:Ulydev wrote:Hello, S0lll0s, and thank you for your answer.
I understand that using physics for this kind of job is maybe too intensive.
As for the origin-ball distance comparison with the radius of the circle, how could I implement this into my actual project ?
If the distance between the origin of the circle and the ball is greater than the circle's radius, then... do what ? Change the ball's x and y pos so it stays in the circle ? Apply a force ?
I apologize for asking such dumb questions, but I think it'll take me quite a while to get used to Löve2D.
Thank you in advance.
Code: Select all
local CIRCLE_RAD = 300 -- whatever the radius of that circle is
local CIRCLE_RAD_SQUARED = 90000
local ballx, bally = 0, 0 -- relative to center of screen
-- in love.update
local mx, my = love.mouse.getPosition()
ballx, bally = mx - width/2, my - height/2
if mx ^ 2 + my ^ 2 > CIRCLE_RAD_SQUARED then
local angle = math.atan2(bally, ballx ) -- angle of mouse in radians
ballx, bally = math.cos( angle ) * CIRCLE_RAD, mat.sin( angle ) * CIRCLE_RAD -- clamp mouse value to circle radius
end
Re: Circle outline as a physic object ?
In fact, the player moves the ball using the arrow keys (pressing a key sets the linear velocity of the ball, and releasing causes it to stop). This code is similar to what I used for the phone version of the game. Therefore the position of the ball has no mathematic relation with the center nor the mouse. I appreciate your explanation though
Re: Circle outline as a physic object ?
Of course it has a relation to the center! Just substitute your keyboard code for the getMousePosition and it should work.Ulydev wrote:In fact, the player moves the ball using the arrow keys (pressing a key sets the linear velocity of the ball, and releasing causes it to stop). This code is similar to what I used for the phone version of the game. Therefore the position of the ball has no mathematic relation with the center nor the mouse. I appreciate your explanation though
Also I'm not saying that a physics engine can't handle that "much", it surely can, but it's overly complicated to implement such easy and unrealistic physics in a complicated physically accurate environment like Box2D.
Re: Circle outline as a physic object ?
Hi S0lll0s, just wanted to tell you I decided to implement both mouse and keyboard controls in my game. Thank you again for your time
Who is online
Users browsing this forum: K2-XT and 196 guests