[Resolved] Ball doesn't stop rolling

I’m having an issue where the ball doesn’t stop rolling when using corona physics until it collides with something. So the ball can roll VERYYYY slowly from one side of the screen to the other without actually stopping until it hits the wall on the other side.

I’m sure there is a way around this, or I’m possibly doing it wrong as I’m new to this. Doing a search I found someone who posted similar on stackoverflow and was told to turn the friction up (which I have done in testing) but it didn’t help me and the poster of it originally never replied.

Here is my code (well the current version at least, been lots of changes while trying to figure this out)

local physics = require "physics"  
physics.start()   
  
local myRectangle = display.newRect(-200, 300, 800, 800)  
myRectangle:setFillColor(100, 100, 250)  
physics.addBody( myRectangle, "static", { friction = 100} )  
  
local myRectangle2 = display.newRect(490, 0, 800, 800)  
myRectangle2:setFillColor(100, 100, 250)  
physics.addBody( myRectangle2, "static", { friction = 100} )  
  
local myRectangle3 = display.newRect(-30, 250, 50, 50)  
myRectangle:setFillColor(100, 100, 250)  
physics.addBody( myRectangle2, "static", { density=1.6, friction=100, bounce=1} )  
  
myDynamicImage = display.newImageRect( "img/ball.png", 34, 34 )   
myDynamicImage.x = 0  
myDynamicImage.y = 0  
physics.addBody( myDynamicImage, { friction = 100, radius = 17 ,bounce = 0.3} )  
myDynamicImage:applyForce(1, 0, myDynamicImage.x, myDynamicImage.y)  

I thought one option could be to check the current force on the object (though I’m not 100% certain how to do that yet) and once it gets below a certain point to slowly take the force away manually (again not certain how to do that yet). But I’m sure there has to be a better way to handle this considering the physics system seems pretty good, so assuming I’m just doing something wrong!

Can anyone point me in the right direction for this? [import]uid: 133056 topic_id: 24196 reply_id: 324196[/import]

Hey there, this is very rough and the ball may stop too soon, but add this at the end of your code;

[lua]local function checkBall()
vx, vy = myDynamicImage:getLinearVelocity()
print (vx, vy)
if vx < 66 and vx > 0 then
myDynamicImage:setLinearVelocity(vx-4, vy)
end
end
timer.performWithDelay(200, checkBall, 0)[/lua]

Normally you’d have this on a Runtime listener most likely but this should give you something to work from I hope.

If any of the above needs explaining let me know, hope it helps!

Peach :slight_smile: [import]uid: 52491 topic_id: 24196 reply_id: 97647[/import]

Hmmm, so I was doing things correctly then? Good in a way I guess, though I was hoping that the physics side would handle it for me.

What you suggested is pretty much what I was thinking, I have adjusted it a bit though.

local function checkBall()  
 vx, vy = myDynamicImage:getLinearVelocity()  
 print (vx, vy)  
 if vx \< 20 and vy == 0 then  
 myDynamicImage:setLinearVelocity(vx \* 0.8, vy)  
 end  
end  

This one will only start slowing it down once there is no y velocity. Also handles the ball rolling in both directions and slowly brings the ball to a stop. I’m sure I could go the step further an check the friction on the 2 objects… but I’m way to lazy for that :wink:

Thanks again for the help, Peach. If you’re ever on the Central Coast I think I probably owe you a drink for the times you’ve helped me! [import]uid: 133056 topic_id: 24196 reply_id: 97657[/import]

For anyone else, I actually found a few issues where the ball would constantly bounce which caused problems. I also found using 20 just wasn’t good enough as far when to do checks (was for most tests but not all the time)so what I’m using now is:-

local function checkBall()  
 vx, vy = myDynamicImage:getLinearVelocity()  
 print (vx, vy)  
 if vx \< 70 and vx \> -70 and vy \< 70 and vy \> -70 then  
 myDynamicImage:setLinearVelocity(vx \* 0.8, vy \* 0.8)  
 end  
end  

So far I haven’t had any issues with this *knocks on wood* [import]uid: 133056 topic_id: 24196 reply_id: 97662[/import]

Thanks for updating.

You could play around with your physics settings (including density on the ball I think) to get the desired result but I think getting just right would be somewhat of a process.

Ultimately I’d think this was the best solution because it allows for really easy tweaking.

And thanks, one day maybe I’ll take you up on the drink :slight_smile: [import]uid: 52491 topic_id: 24196 reply_id: 97675[/import]

I have a question about making a ball roll on top of a platform. I created a detailed ball in Photoshop so I could tell if it rotated while it moved. For some reason the ball just slides and will not roll. I am using the accelerometer to move the ball left and right. If it helps I can add my code. Ideas? [import]uid: 82872 topic_id: 24196 reply_id: 114340[/import]

At a guess, I would say you will need to add friction to the ball / platform, without friction to grip onto the ball it will just slide across the platform. [import]uid: 133056 topic_id: 24196 reply_id: 114357[/import]