Circle outline as a physic object ?

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
Ulydev
Party member
Posts: 445
Joined: Mon Nov 10, 2014 10:46 pm
Location: Paris
Contact:

Circle outline as a physic object ?

Post by Ulydev »

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 ! :awesome:
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Circle outline as a physic object ?

Post by davisdude »

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
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Circle outline as a physic object ?

Post by ivan »

One approach is to create a "ring" of static boxes.
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
User avatar
Ulydev
Party member
Posts: 445
Joined: Mon Nov 10, 2014 10:46 pm
Location: Paris
Contact:

Re: Circle outline as a physic object ?

Post by Ulydev »

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.
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Circle outline as a physic object ?

Post by s-ol »

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.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
User avatar
Ulydev
Party member
Posts: 445
Joined: Mon Nov 10, 2014 10:46 pm
Location: Paris
Contact:

Re: Circle outline as a physic object ?

Post by Ulydev »

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.
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Circle outline as a physic object ?

Post by s-ol »

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.
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:

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

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
User avatar
Ulydev
Party member
Posts: 445
Joined: Mon Nov 10, 2014 10:46 pm
Location: Paris
Contact:

Re: Circle outline as a physic object ?

Post by Ulydev »

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 :awesome:
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Circle outline as a physic object ?

Post by s-ol »

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 :awesome:
Of course it has a relation to the center! Just substitute your keyboard code for the getMousePosition and it should work.

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.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
User avatar
Ulydev
Party member
Posts: 445
Joined: Mon Nov 10, 2014 10:46 pm
Location: Paris
Contact:

Re: Circle outline as a physic object ?

Post by Ulydev »

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 :megagrin:
Post Reply

Who is online

Users browsing this forum: K2-XT and 196 guests