Crazy about friction or dumping or...

Hi,

a simple problem - but… I go crazy!!!

[lua]

local physics = require(“physics”)

physics.start()

physics.setGravity(0, 40)

local bottomWall = display.newRect( 0, display.contentWidth/2, display.contentWidth, 10 )

bottomWall.x = display.contentWidth/2

physics.addBody(bottomWall, “static”, {density = 1, friction = 0, bounce = 0})

ball = display.newCircle( 0, 0, 30 )

physics.addBody(ball, “dynamic”, {density = 1, friction = 0, bounce = 0.5, radius = 15})

player.isFixedRotation = false //important for me

ball.isBullet = true

ball:applyForce(200, 50)

[/lua]

I want that the ball will stop after ra few seconds… but he’s moving on and on… Why? I want to simulate ice, stone (…) grounds by different frictions but nothing worked for me.

Help?! Thank you :slight_smile:

Anthony 

use linearDamping to slow your ball

(friction affects the “stickiness” of the surfaces during collision, imparting angular momentum)

So you want the ball to move but then stop in X amount of time?

Lets say your object is on ice and its moving then the ball stops after 10 seconds but if the object is in mud then it stops after 1 second?

–SonicX278

Thank you very much - how about something like that?

[lua]

local physics = require(“physics”)

physics.start()

physics.setGravity(0, 40)

local ball, bottomWall

bottomWall = display.newRect( 0, display.contentWidth/2, display.contentWidth, 10 )

bottomWall.x = display.contentWidth/2

physics.addBody(bottomWall, “static”, {density = 1, friction = 0, bounce = 0})

local function onLocalCollision( self, event )

ball.linearDamping = 0.3

local vx, vy = ball:getLinearVelocity()

if math.abs(vy) < 50 and math.abs(vx) < 50 then

ball:setLinearVelocity( vy/1.5 , vx/1.8 )

end

if math.abs(vy) < 25 and math.abs(vx) < 40 then

ball:setLinearVelocity( 0 , 0)

end

end

ball = display.newCircle( 0, 0, 30 )

physics.addBody(ball, “dynamic”, {density = 1, friction = 0, bounce = 0.7, radius = 15})

ball.isBullet = true

ball:applyForce(200, 50)

ball.collision = onLocalCollision

ball:addEventListener( “collision”, ball )

[/lua]

I tried to stop the minimal bouncing - is there a better way? @SonicX278 - the timing idea sounds nice - can you explain a litte bit more?

Anthony

I’d reverse your model:  set bounce=0 on the ball, set bounce=<value that varies by material> on the wall.

you may also want to vary the amount of damping based on the material, very high if in “tar” for example, lower if “water” fe

you may also want to monitor the collision phase, and only apply damping while collision is occurring – that is, if wall material is bouncy enough to allow ball to escape, then it probably isn’t “dragging” on it while in air, then reapply when it descends again, etc

Okay, here it is - I updated some things:

[lua]

– #################################

– ### Ground Physics Simulation ###

– ####++++++++#####################

local physics = require(“physics”)

physics.start()

physics.setGravity(0, 40)

local ball, bottomWall

bottomWall = display.newRect( 0, display.contentWidth/2, display.contentWidth, 10 )

bottomWall.x = display.contentWidth/2

– ########### ICE GROUND ################

bottomWall.typ = “ice”

physics.addBody(bottomWall, “static”, {density = 1, friction = 0, bounce = 0.5})

– ######### STONE GROUND ################

–bottomWall.typ = “stone”

–physics.addBody(bottomWall, “static”, {density = 1, friction = 0, bounce = 0.9})

local function onLocalCollision( self, event )

local vx, vy = ball:getLinearVelocity()

vx = math.abs( vx )

vy = math.abs( vy )

    – Collision Start

if ( event.phase == “began” ) then

– Linear Dumping - check ground first!

if event.other.typ == “stone” then

self.linearDamping = 0.35

if vy < 60 then

ball:setLinearVelocity( vx/1.5, vy/2 )

end

end

if event.other.typ == “ice” then

self.linearDamping = 0.25

if vx < 90 then

ball:setLinearVelocity( vx/1.5, vy )

end

end

    elseif ( event.phase == “ended” ) then

    – Collision End

    end

end

ball = display.newCircle( 0, 0, 30 )

physics.addBody(ball, “dynamic”, {density = 1, friction = 0, bounce = 0, radius = 15})

ball.isBullet = true

ball:applyForce(250, 50)

ball.collision = onLocalCollision

ball:addEventListener( “collision”, ball )

[/lua]

Thank you very much vor your help!!!

use linearDamping to slow your ball

(friction affects the “stickiness” of the surfaces during collision, imparting angular momentum)

So you want the ball to move but then stop in X amount of time?

Lets say your object is on ice and its moving then the ball stops after 10 seconds but if the object is in mud then it stops after 1 second?

–SonicX278

Thank you very much - how about something like that?

[lua]

local physics = require(“physics”)

physics.start()

physics.setGravity(0, 40)

local ball, bottomWall

bottomWall = display.newRect( 0, display.contentWidth/2, display.contentWidth, 10 )

bottomWall.x = display.contentWidth/2

physics.addBody(bottomWall, “static”, {density = 1, friction = 0, bounce = 0})

local function onLocalCollision( self, event )

ball.linearDamping = 0.3

local vx, vy = ball:getLinearVelocity()

if math.abs(vy) < 50 and math.abs(vx) < 50 then

ball:setLinearVelocity( vy/1.5 , vx/1.8 )

end

if math.abs(vy) < 25 and math.abs(vx) < 40 then

ball:setLinearVelocity( 0 , 0)

end

end

ball = display.newCircle( 0, 0, 30 )

physics.addBody(ball, “dynamic”, {density = 1, friction = 0, bounce = 0.7, radius = 15})

ball.isBullet = true

ball:applyForce(200, 50)

ball.collision = onLocalCollision

ball:addEventListener( “collision”, ball )

[/lua]

I tried to stop the minimal bouncing - is there a better way? @SonicX278 - the timing idea sounds nice - can you explain a litte bit more?

Anthony

I’d reverse your model:  set bounce=0 on the ball, set bounce=<value that varies by material> on the wall.

you may also want to vary the amount of damping based on the material, very high if in “tar” for example, lower if “water” fe

you may also want to monitor the collision phase, and only apply damping while collision is occurring – that is, if wall material is bouncy enough to allow ball to escape, then it probably isn’t “dragging” on it while in air, then reapply when it descends again, etc

Okay, here it is - I updated some things:

[lua]

– #################################

– ### Ground Physics Simulation ###

– ####++++++++#####################

local physics = require(“physics”)

physics.start()

physics.setGravity(0, 40)

local ball, bottomWall

bottomWall = display.newRect( 0, display.contentWidth/2, display.contentWidth, 10 )

bottomWall.x = display.contentWidth/2

– ########### ICE GROUND ################

bottomWall.typ = “ice”

physics.addBody(bottomWall, “static”, {density = 1, friction = 0, bounce = 0.5})

– ######### STONE GROUND ################

–bottomWall.typ = “stone”

–physics.addBody(bottomWall, “static”, {density = 1, friction = 0, bounce = 0.9})

local function onLocalCollision( self, event )

local vx, vy = ball:getLinearVelocity()

vx = math.abs( vx )

vy = math.abs( vy )

    – Collision Start

if ( event.phase == “began” ) then

– Linear Dumping - check ground first!

if event.other.typ == “stone” then

self.linearDamping = 0.35

if vy < 60 then

ball:setLinearVelocity( vx/1.5, vy/2 )

end

end

if event.other.typ == “ice” then

self.linearDamping = 0.25

if vx < 90 then

ball:setLinearVelocity( vx/1.5, vy )

end

end

    elseif ( event.phase == “ended” ) then

    – Collision End

    end

end

ball = display.newCircle( 0, 0, 30 )

physics.addBody(ball, “dynamic”, {density = 1, friction = 0, bounce = 0, radius = 15})

ball.isBullet = true

ball:applyForce(250, 50)

ball.collision = onLocalCollision

ball:addEventListener( “collision”, ball )

[/lua]

Thank you very much vor your help!!!