Jump Help

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

[import]uid: 116264 topic_id: 20922 reply_id: 320922[/import]

You could try using ball.isFixedRotation = true - can you let me know if that helps?

Peach :slight_smile: [import]uid: 52491 topic_id: 20922 reply_id: 82650[/import]

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? [import]uid: 116264 topic_id: 20922 reply_id: 82741[/import]

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? [import]uid: 116264 topic_id: 20922 reply_id: 82751[/import]

In what phase of the event on_hit are you turning it off? [import]uid: 52491 topic_id: 20922 reply_id: 82885[/import]

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. :slight_smile:

Brent [import]uid: 9747 topic_id: 20922 reply_id: 82895[/import]

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. [import]uid: 116264 topic_id: 20922 reply_id: 82974[/import]

Damn, sorry, I should have spotted that.

Nice catch Ignis!

Peach :slight_smile: [import]uid: 52491 topic_id: 20922 reply_id: 83080[/import]