Let’s go with linearDamping…
[lua]Line 38: ball.linearDamping = .5[/lua] [import]uid: 8271 topic_id: 34116 reply_id: 135763[/import]
Let’s go with linearDamping…
[lua]Line 38: ball.linearDamping = .5[/lua] [import]uid: 8271 topic_id: 34116 reply_id: 135763[/import]
The ball goes simply slower down, but it doesn’t reduce the speed to the left or right [import]uid: 203192 topic_id: 34116 reply_id: 135764[/import]
linearDamping slows the ball, as if it is moving through something, like golden syrup. Fine tune the value to get the right effect. It reduces the speed in all directions.
If you want to reduce the amount of force applied to the ball, just adjust the first parameter on line 47. Maybe divide by 3 or something?
Are you really sure you always want to apply a -ve value for the y force? The upwards force will not be relative to the touch location if you don’t use the local y value.
[lua]ball:applyLinearImpulse( x / 3, -math.abs(y), ball.x, ball.y )[/lua]
The line above reduces the horizontal force and always ensures the vertical force is upwards, but still relative to the vertical distance of the touch from the centre of the ball. [import]uid: 8271 topic_id: 34116 reply_id: 135770[/import]
Hi all,
I have a similar problem to @borisbroekie. I am using jump sensors as explained in this tutorial. http://coronalabs.com/blog/2013/02/19/more-physics-tricks-explained/
For some reason when i have objects that have different isFixedRotation values (box.isFixedRotation = true to false) they behave completely different in the speed at which they move on a box:applyLinearImpulse.
I have tried to use the comments above to alter my code to use dynamic objects instead of static, or using zero friction etc to limit torque effects, but nothing works.
I haven’t applied linear damping as i’d rather not have to fudge a result.
I have looked at these postings as well for inspiration.
http://forums.coronalabs.com/topic/12662-isfixedrotation-bug/
http://forums.coronalabs.com/topic/11491-is-isfixedrotation-working-as-intended/
This really seems like some sort of bug, as a linear impulse i am assuming is not like a normal physics collision but a centreOfGravity effect?? the masses of each object report equal
Heres my test code you can use to see the effect I am talking about. Test it out in an iPad simulation window, and see if it happens to you.
Corona Build - Version 2014.2189 (2014.3.6)
Any help would be greatly appreciated. Can any corona Admins comment?
Thanks
Nick
Code:
[lua]-- setup physics
local physics = require( “physics” )
physics.setDrawMode(“debug”)
physics.start()
physics.setGravity(0, 10)
physics.setPositionIterations( 10 )
– Added these to isolate the boxes from “static” objects
local peg1 = display.newRect( 100, 1000, 50, 50)
local peg2 = display.newRect( 600, 1000, 50, 50)
physics.addBody(peg1, “static”, { bounce=0.1, friction=1.0 })
physics.addBody(peg2, “static”, { bounce=0.1, friction=1.0 })
– create ground
local ground = display.newRect( 400, 850, 800, 100)
– physics.addBody(ground, “static”, { bounce=0.1, friction=1.0 }) – FRICTION
– physics.addBody(ground, “static”, { bounce=0.1, friction=0 }) – ZERO FRICTION
physics.addBody(ground, “dynamic”, { bounce=0.0, friction=0 })
ground.objType = “ground”
– Create Identical Boxes
local box1 = display.newRect( 300, 500, 50, 30 )
box1:setFillColor( 0.0, 1.0, 0.0 )
local box2 = display.newRect( 500, 500, 50, 30 )
box2:setFillColor( 1.0, 0.0, 0.0 )
– Add box 1 to physics engine
physics.addBody( box1, “dynamic”,
– { density=1.0, friction=1.0, bounce=0.1 }, – FRICTION
{ density=1.0, friction=0, bounce=0.1 }, – ZERO FRICTION
{ shape={20,10, 20,20, -20,20, -20,10}, isSensor=true }
)
box1.isFixedRotation = true – THIS LINE IS THE ONLY DIFFERENCE BETWEEEN BOXES
box1.canJump = 0
– Add box 2 to physics engine
physics.addBody( box2, “dynamic”,
– { density=1.0, friction=1.0, bounce=0.1 },
{ density=1.0, friction=0, bounce=0.1 },
{ shape={20,10, 20,20, -20,20, -20,10}, isSensor=true }
)
box2.isFixedRotation = false
box2.canJump = 0
– add touch listeners
function box1:touch(event)
if ( event.phase == “began” and self.canJump > 0 ) then
self:applyLinearImpulse( 0, -10 )
print("box1.mass: ", box1.mass)
end
return true
end
box1:addEventListener( “touch”, box1 )
function box2:touch(event)
if ( event.phase == “began” and self.canJump > 0 ) then
box2:applyLinearImpulse( 0,-10 )
print("box2.mass: ", box2.mass)
end
return true
end
box2:addEventListener( “touch”, box2)
– collision detect using element 2 sensors to set canJump flag
function charCollide( self,event )
if ( event.selfElement == 2 and event.other.objType == “ground” ) then
if ( event.phase == “began” ) then
self.canJump = self.canJump+1
elseif ( event.phase == “ended” ) then
self.canJump = self.canJump-1
end
end
end
box1.collision = charCollide ; box1:addEventListener( “collision”, box1 )
box2.collision = charCollide ; box2:addEventListener( “collision”, box2 )
[/lua]
Hi @online2,
I tested your code. I can’t fully explain why Box2D is doing this, but if you specify the point within the body to apply the linear impulse on, both of them begin working the same. Right now, you are stating to apply 0,-10 impulse, but you don’t say where in the body to apply it. I tried declaring it for both, and the behavior becomes consistent:
[lua]
self:applyLinearImpulse( 0, -10, self.x, self.y )
[/lua]
Take care,
Brent
Also, just FYI, the documentation states that the point within the body is required. So it may not be producing an outright error when you omit those parameters, but they should be stated to avoid the behavior you were seeing.
http://docs.coronalabs.com/api/type/Body/applyLinearImpulse.html
Hi Brent,
I just tested this code and it works as you described. Not sure where I picked up the habit of not adding the point within the body, but thanks for letting me know.
Much appreciated.
cheers
Nick
PS sorry for the name typo calling you Brett
Hi all,
I have a similar problem to @borisbroekie. I am using jump sensors as explained in this tutorial. http://coronalabs.com/blog/2013/02/19/more-physics-tricks-explained/
For some reason when i have objects that have different isFixedRotation values (box.isFixedRotation = true to false) they behave completely different in the speed at which they move on a box:applyLinearImpulse.
I have tried to use the comments above to alter my code to use dynamic objects instead of static, or using zero friction etc to limit torque effects, but nothing works.
I haven’t applied linear damping as i’d rather not have to fudge a result.
I have looked at these postings as well for inspiration.
http://forums.coronalabs.com/topic/12662-isfixedrotation-bug/
http://forums.coronalabs.com/topic/11491-is-isfixedrotation-working-as-intended/
This really seems like some sort of bug, as a linear impulse i am assuming is not like a normal physics collision but a centreOfGravity effect?? the masses of each object report equal
Heres my test code you can use to see the effect I am talking about. Test it out in an iPad simulation window, and see if it happens to you.
Corona Build - Version 2014.2189 (2014.3.6)
Any help would be greatly appreciated. Can any corona Admins comment?
Thanks
Nick
Code:
[lua]-- setup physics
local physics = require( “physics” )
physics.setDrawMode(“debug”)
physics.start()
physics.setGravity(0, 10)
physics.setPositionIterations( 10 )
– Added these to isolate the boxes from “static” objects
local peg1 = display.newRect( 100, 1000, 50, 50)
local peg2 = display.newRect( 600, 1000, 50, 50)
physics.addBody(peg1, “static”, { bounce=0.1, friction=1.0 })
physics.addBody(peg2, “static”, { bounce=0.1, friction=1.0 })
– create ground
local ground = display.newRect( 400, 850, 800, 100)
– physics.addBody(ground, “static”, { bounce=0.1, friction=1.0 }) – FRICTION
– physics.addBody(ground, “static”, { bounce=0.1, friction=0 }) – ZERO FRICTION
physics.addBody(ground, “dynamic”, { bounce=0.0, friction=0 })
ground.objType = “ground”
– Create Identical Boxes
local box1 = display.newRect( 300, 500, 50, 30 )
box1:setFillColor( 0.0, 1.0, 0.0 )
local box2 = display.newRect( 500, 500, 50, 30 )
box2:setFillColor( 1.0, 0.0, 0.0 )
– Add box 1 to physics engine
physics.addBody( box1, “dynamic”,
– { density=1.0, friction=1.0, bounce=0.1 }, – FRICTION
{ density=1.0, friction=0, bounce=0.1 }, – ZERO FRICTION
{ shape={20,10, 20,20, -20,20, -20,10}, isSensor=true }
)
box1.isFixedRotation = true – THIS LINE IS THE ONLY DIFFERENCE BETWEEEN BOXES
box1.canJump = 0
– Add box 2 to physics engine
physics.addBody( box2, “dynamic”,
– { density=1.0, friction=1.0, bounce=0.1 },
{ density=1.0, friction=0, bounce=0.1 },
{ shape={20,10, 20,20, -20,20, -20,10}, isSensor=true }
)
box2.isFixedRotation = false
box2.canJump = 0
– add touch listeners
function box1:touch(event)
if ( event.phase == “began” and self.canJump > 0 ) then
self:applyLinearImpulse( 0, -10 )
print("box1.mass: ", box1.mass)
end
return true
end
box1:addEventListener( “touch”, box1 )
function box2:touch(event)
if ( event.phase == “began” and self.canJump > 0 ) then
box2:applyLinearImpulse( 0,-10 )
print("box2.mass: ", box2.mass)
end
return true
end
box2:addEventListener( “touch”, box2)
– collision detect using element 2 sensors to set canJump flag
function charCollide( self,event )
if ( event.selfElement == 2 and event.other.objType == “ground” ) then
if ( event.phase == “began” ) then
self.canJump = self.canJump+1
elseif ( event.phase == “ended” ) then
self.canJump = self.canJump-1
end
end
end
box1.collision = charCollide ; box1:addEventListener( “collision”, box1 )
box2.collision = charCollide ; box2:addEventListener( “collision”, box2 )
[/lua]
Hi @online2,
I tested your code. I can’t fully explain why Box2D is doing this, but if you specify the point within the body to apply the linear impulse on, both of them begin working the same. Right now, you are stating to apply 0,-10 impulse, but you don’t say where in the body to apply it. I tried declaring it for both, and the behavior becomes consistent:
[lua]
self:applyLinearImpulse( 0, -10, self.x, self.y )
[/lua]
Take care,
Brent
Also, just FYI, the documentation states that the point within the body is required. So it may not be producing an outright error when you omit those parameters, but they should be stated to avoid the behavior you were seeing.
http://docs.coronalabs.com/api/type/Body/applyLinearImpulse.html
Hi Brent,
I just tested this code and it works as you described. Not sure where I picked up the habit of not adding the point within the body, but thanks for letting me know.
Much appreciated.
cheers
Nick
PS sorry for the name typo calling you Brett