Rotate a dynamic object with Physics

Hey Everyone,

Let say we have a ball as images, this ball is a dynamic object with density, friction and bounce value. Also I have create static ground/line.

This ball needs to rotate, and roll on a static ground.

I set the images as local Ball = display.newImage(“Ball.png”)

The behavior of the ball has to be rotate and roll constantly to one direction only(left or right). If the ball rolls to the right and hit a line that I draw or a static object, the ball needs to turn the roll path from left to right or vice versa.

Can anyone point me in the right direction, thanks.

Grz.
-Mike
[import]uid: 21175 topic_id: 22508 reply_id: 322508[/import]

For the appearance of rotating see here; http://developer.anscamobile.com/reference/index/bodyangularvelocity

The movement would likely be done by setting linear velocity.

Peach :slight_smile: [import]uid: 52491 topic_id: 22508 reply_id: 89809[/import]

Thanks Peach :slight_smile:

Almost…the ball does rotate, but after it hits a static wall it stops rotating and loss his angularVelocity, see sample code:

config.lua

application = {  
 content =   
 {   
 width = 320,  
 height = 480,  
 fps = 60,  
 scale = "letterbox"  
 }  
}  

main.lua

local physics = require("physics")  
physics.start()  
--physics.setDrawMode( "hybrid" )   
display.setStatusBar( display.HiddenStatusBar )  
-- Horizontal Rectangles  
local myRectangle = display.newRect(0, 100, 300, 10)  
myRectangle.strokeWidth = 3  
physics.addBody(myRectangle, "static")  
  
local myRectangle2 = display.newRect(360, 100, 300, 10)  
myRectangle2.strokeWidth = 3  
physics.addBody(myRectangle2, "static")  
  
local myRectangle3 = display.newRect(-180, 200, 300, 10)  
myRectangle3.strokeWidth = 3  
physics.addBody(myRectangle3, "static")  
  
local myRectangle4 = display.newRect(180, 200, 300, 10)  
myRectangle4.strokeWidth = 3  
physics.addBody(myRectangle4, "static")  
  
local myRectangle5 = display.newRect(0, 300, 480, 10)  
myRectangle5.strokeWidth = 3  
physics.addBody(myRectangle5, "static")  
-- Vertical Rectangles  
local myRectangle = display.newRect(5, 145, 10, 50)  
myRectangle.strokeWidth = 3  
physics.addBody(myRectangle, "static" )  
  
local myRectangle2 = display.newRect(465, 145, 10, 50)  
myRectangle2.strokeWidth = 3  
physics.addBody(myRectangle2, "static" )  
  
local myRectangle3 = display.newRect(5, 245, 10, 50)  
myRectangle3.strokeWidth = 3  
physics.addBody(myRectangle3, "static" )  
  
local myRectangle4 = display.newRect(465, 245, 10, 50)  
myRectangle4.strokeWidth = 3  
physics.addBody(myRectangle4, "static" )  
--Create a Circle  
local myCircle = display.newCircle( 44, 15, 10 )  
   
--Add a body to the Circle  
physics.addBody(myCircle, "kinemetic", { density = 0.8, friction = 0.2, bounce = 0.3, radius = 10 })  
   
--Set the Circle angular velocity  
myCircle.angularVelocity = 1550  
   
-- (Optional) assign a local variable to hold the angular velocity value  
local d = myCircle.angularVelocity  

The ball need constantly movement or speed, if the ball hits the wall, or I as described in the code “Vertical Rectangles”, it must rotate the upset way.

Grz.
-Mike

PS. Thanks for the support :wink: [import]uid: 21175 topic_id: 22508 reply_id: 89971[/import]

Hey again,

Well when it hits a wall you will apply the angularVelocity again, so it stopping shouldn’t be an issue.

What you’d do is have a function for collision on the ball. On collision if it hits something with a > x then ball.angularVeloctiy = -1550 and if it hits something with a < x then make it 1550.

Does that make sense?

Try running this as a rough example;

[lua]display.setStatusBar (display.HiddenStatusBar)

local physics = require(“physics”)
physics.start()
physics.setDrawMode( “hybrid” )
display.setStatusBar( display.HiddenStatusBar )

– Horizontal Rectangles
local myRectangle = display.newRect(0, 100, 300, 10)
myRectangle.strokeWidth = 3
physics.addBody(myRectangle, “static”)

local myRectangle2 = display.newRect(360, 100, 300, 10)
myRectangle2.strokeWidth = 3
physics.addBody(myRectangle2, “static”)

local myRectangle3 = display.newRect(-180, 200, 300, 10)
myRectangle3.strokeWidth = 3
physics.addBody(myRectangle3, “static”)

local myRectangle4 = display.newRect(180, 200, 300, 10)
myRectangle4.strokeWidth = 3
physics.addBody(myRectangle4, “static”)

local myRectangle5 = display.newRect(0, 300, 480, 10)
myRectangle5.strokeWidth = 3
physics.addBody(myRectangle5, “static”)

– Vertical Rectangles
local myRectangle = display.newRect(5, 145, 10, 50)
myRectangle.strokeWidth = 3
myRectangle.type = “wall”
physics.addBody(myRectangle, “static” )

local myRectangle2 = display.newRect(465, 145, 10, 50)
myRectangle2.strokeWidth = 3
myRectangle2.type = “wall”
physics.addBody(myRectangle2, “static” )

local myRectangle3 = display.newRect(5, 245, 10, 50)
myRectangle3.strokeWidth = 3
myRectangle3.type = “wall”
physics.addBody(myRectangle3, “static” )

local myRectangle4 = display.newRect(465, 245, 10, 50)
myRectangle4.strokeWidth = 3
myRectangle4.type = “wall”
physics.addBody(myRectangle4, “static” )

–Create a Circle
local myCircle = display.newCircle( 44, 15, 10 )

–Add a body to the Circle
physics.addBody(myCircle, “kinemetic”, { density = 0.8, friction = 0.2, bounce = 0.3, radius = 10 })

–Set the Circle angular velocity
myCircle.angularVelocity = 1550

– (Optional) assign a local variable to hold the angular velocity value
local d = myCircle.angularVelocity

function circleCol (event)
if event.other.type == “wall” and event.other.x > event.target.x then
event.target.angularVelocity = -1550
elseif event.other.type == “wall” and event.other.x < event.target.x then
event.target.angularVelocity = 1550
end
end
myCircle:addEventListener(“collision”, circleCol)[/lua]

Peach :slight_smile: [import]uid: 52491 topic_id: 22508 reply_id: 90086[/import]

Hi Peach,

That makes sense :wink:

Now I can adjust the angularVelocity for any typ of wall or any type of object to make the movement & speed natural.

Thanks

Grz.
-Mike [import]uid: 21175 topic_id: 22508 reply_id: 90274[/import]

Not a problem :slight_smile: Good luck with your project! [import]uid: 52491 topic_id: 22508 reply_id: 90408[/import]