swift - Giving a physicsBody a constant force -
i have ball bouncing , down. i've accomplished setting it's physics body this:
lineardamping = 0 friction = 0 restitution = 1
and applyforce(cgvectormake(0, 10))
in setup call. ball bouncing , down, , down not interruption.
now want add player control. player can move ball sideways. setting balls velocity when controls pressed. doing lose stuff physics engine gives me.
here's short snippet of code have right now:
override func update(currenttime: cftimeinterval) { /* apply damping in x */ if !movingleft && !movingright { let dx = ball!.physicsbody!.velocity.dx * xalpha let dy = ball!.physicsbody!.velocity.dy ball!.physicsbody?.velocity = cgvectormake(dx, dy) } if movingright { ball!.physicsbody?.applyimpulse(cgvectormake(ballvelocityx, 0)) } if movingleft { ball!.physicsbody?.applyimpulse(cgvectormake(-ballvelocityx, 0)) } }
the problem, might imagine, applying impulse upon impulse on each frame makes ball go superfast, not want, when controls held down. if apply force on keydown
ball stop moving in direction once ball hits something. , keydown
event works wonky on os x (because of key repeat settings).
so want apply impulse in update
function in way makes force of ball not exceed limit not go away if ball hits something.
Comments
Post a Comment