Physics object inside a circular boundary

Hello, I am new to Corona and experimenting with my first app. I am trying to construct a simple app to implement a ball that bounces around inside a circular boundary. I am using the Corona game in 8 minutes tutorial as a starting point and have the following code so far:

[lua]–> Add physics engine
local physics = require(“physics”)
physics.start()

–> Set gravity to act “down”
physics.setGravity(0, 9.8)

–> Add circular boundary
local circleRadius = display.contentWidth/2
local circleBoundary = display.newCircle(display.contentWidth/2, display.contentHeight/2, circleRadius)
circleBoundary:setFillColor(255,255,255, 255)

–> Turn circle boundary into physics body
physics.addBody(circleBoundary, “static”, {bounce = 0.2})

–> Add ball image
local ball = display.newImage(“ball.png”)
ball.x = circleBoundary.contentWidth/2
ball.y = circleBoundary.contentHeight/3

–> Turn ball into physics body
physics.addBody(ball, {bounce = 0.8})

–> Hide status bar
display.setStatusBar(display.HiddenStatusBar)[/lua]

When run, the ball drops down into the area of the circular boundary then bounces up past the top of the circular boundary and stops.

The behavior I am trying to affect is as follows:

  1. the ball bounces off the inner perimeter of the circular boundary
  2. the ball stays contained within the circular boundary

Questions:

  1. is this behavior possible with the physics engine?
  2. is there any sample code that shows how to affect the desired behavior?

I have searched the site and forums but could not find anything – hence my query. Any pointers are appreciated!

[import]uid: 1855 topic_id: 4740 reply_id: 304740[/import]

The physics bodies have solid centers, so rather than using a single object you need to build a border. Try this:

  
--\> Add physics engine  
local physics = require("physics")  
physics.start()  
   
--\> Set gravity to act "down"  
physics.setGravity(0, 9.8)  
   
--\> Add circular boundary  
local circleRadius = 100  
local circleBoundary = display.newCircle(100, 300, circleRadius)  
circleBoundary:setFillColor(255,255,255, 255)  
  
local circleBoundary2 = display.newCircle(275, 200, circleRadius)  
circleBoundary2:setFillColor(255,255,255, 255)  
  
local circleBoundary3 = display.newCircle(450, 300, circleRadius)  
circleBoundary3:setFillColor(255,255,255, 255)  
  
local circleBoundary4 = display.newCircle(450, 500, circleRadius)  
circleBoundary4:setFillColor(255,255,255, 255)  
  
local circleBoundary5 = display.newCircle(275, 600, circleRadius)  
circleBoundary5:setFillColor(255,255,255, 255)  
  
local circleBoundary6 = display.newCircle(100, 500, circleRadius)  
circleBoundary6:setFillColor(255,255,255, 255)  
   
--\> Turn circle boundary into physics body  
physics.addBody(circleBoundary, "static", {friction=0, bounce = 1})  
physics.addBody(circleBoundary2, "static", {friction=0, bounce = 1})  
physics.addBody(circleBoundary3, "static", {friction=0, bounce = 1})  
physics.addBody(circleBoundary4, "static", {friction=0, bounce = 1})  
physics.addBody(circleBoundary5, "static", {friction=0, bounce = 1})  
physics.addBody(circleBoundary6, "static", {friction=0, bounce = 1})  
  
   
--\> Add ball image  
local ball = display.newCircle( 250, 300, 20, 20 )  
  
ball:setFillColor(255,0,0)  
   
--\> Turn ball into physics body  
physics.addBody(ball, {friction=0, bounce = 1})  
ball:setLinearVelocity( 50, 20 )  
   
--\> Hide status bar  
display.setStatusBar(display.HiddenStatusBar)  
  

I messed with the friction and the bounce values a bit to get this working as I imagined. [import]uid: 10903 topic_id: 4740 reply_id: 15163[/import]

Oh, also set the hardware to view as iPhone 4 for my code. [import]uid: 10903 topic_id: 4740 reply_id: 15164[/import]

Thanks crssmn! This is very helpful. I will experiment further and post the results and solution.

Happy new year! [import]uid: 1855 topic_id: 4740 reply_id: 15211[/import]

happy new year!!!

we can always approximate the inner perimeter of the circular boundary with a series of straight line segments. the more points along the perimeter we use to create the segments, the more accurate the approximation.

[import]uid: 6459 topic_id: 4740 reply_id: 15234[/import]

Happy New Year to you as well tetu!

Thanks for the suggestion. Do the line segments need to be made into separate physics bodies, or can they be grouped or constructed as a single, poly-segmented line? [import]uid: 1855 topic_id: 4740 reply_id: 15240[/import]

separate physics bodies (with the same friction and bounce properties)
[import]uid: 6459 topic_id: 4740 reply_id: 15242[/import]

It’s essentially the same as my suggestion, just up the number of objects to create the ring. [import]uid: 10903 topic_id: 4740 reply_id: 15263[/import]