I have a jump button which makes a ball jump, and it works fine except when the ball is on the corner of an object, just about to roll if it. When I press the jump button then, the ball goes sideways and sometimes downwards. How can I fix this? the code for the jump is:
local ballIsGrounded = true
x, y = ball:getLinearVelocity()
local hold = false
local function pressjump (event)
if event.phase == "began" and ballIsGrounded == true then
ball:applyForce ( 0, 1000, ball.x, ball.y)
jumptimer = timer.performWithDelay(45,function() ball:applyForce(0,y -30,ball.x,ball.y)end,3)
ballIsGrounded = false
hold = true
elseif event.phase == "ended" then
timer.cancel(jumptimer)
hold = false
end
return true
end
jumpbutton:addEventListener ("touch", pressjump)
local function on_hit (event)
if event.phase == "began" and hold == false then
ballIsGrounded = true
ball.angularDamping = 0
elseif event.phase == "began" and hold == true then
ballIsGrounded = false
ball:applyForce (0, 2000, ball.x, ball.y)
end
return true
end
You could try using ball.isFixedRotation = true - can you let me know if that helps?
Peach :)
That helps alot thanks. I've put the ball.isFixedRoation at the start of the jump function and then ball.isFixedRotation = false in the on_hit function. I did notice though that when i put the physics draw mode to hybrid, the ball still appears to rotate? is this a bug or is it meant to happen?
I've been testing using it with no rotation and in some situations it doesn't work great. Is there any way of getting the same effect but with rotation still on?
In what phase of the event on_hit are you turning it off?
Is there a specific reason why you're forcing the ball downward at 1000 Y force, then quickly performIng a timer to check its new velocity? I suppose this causes the ball to bounce and then you read the new upward velocity to apply a new force (3 times)?
If the ball is rolling around on angled surfaces, you could calculate the directional angle using basic trigonometry, then apply a force perpendicular to that angle, so the ball would jump "away" from that slope, no matter what angle the slope is.
Corona doesn't have a built-in "object:getDirectionalAngle" function, but there are many simple routines (I have one) that will give you the angle based on the object's linear X and Y velocity.
Please describe your goal in more detail and the solution should be clearer. :)
Brent
Ignis - thanks for your reply. I stupidly forgot to put a "-" before the 1000 y force!. That explains why I had to have the value so high. My goal is that the ball always jumps upwards, regardless of the angle ground that it is on.
Damn, sorry, I should have spotted that.
Nice catch Ignis!
Peach :)