I'm build an Arkanoid type of game, and what I've done is when the ball hit the paddle, I change the speed depends on the point of contact. I'm not using physic commands whatsoever, and here is the code snipet:
Function HandleCollision()
if ball.flag = 0 and GetSpriteCollision ( player.sprite, ball.sprite ) = 1
PlaySound(hit_sound)
ball.flag = 1 // That way the collision will be tested just once
// invert ball y and x
ballmid = GetSpriteXByOffSet(ball.sprite)
playermid = GetSpriteXByOffSet(player.sprite)
diff = playermid - ballmid
ball.yv# = ball.yv# * -1
if (ballmid < playermid-)
// left edge
diff = playermid - ballmid
ball.xv# = - INITIAL_VELOCITY_BALL_X - (diff/ 5) - velocity_increase
else
if (ballmid > playermid + 5)
diff = ballmid - playermid
ball.xv# = INITIAL_VELOCITY_BALL_X + (diff/ 5) + velocity_increase
else
// perfect middle -5 +5
ball.xv# = INITIAL_VELOCITY_BALL_X + velocity_increase
// 50% chance of inverting the signal
RandomSign(ball.xv#)
endif
endif
Log("Ball X Player collision. (" + Str(playermid) + "/" + Str(ballmid) + ") Diff = " + str(diff) + ", xv=" + str(ball.xv#))
EndIf
// turn off flag for collision check
If ball.flag = 1 and GetSpriteCollision ( player.sprite, ball.sprite ) = 0
// ball had collided with player, and now just left it
ball.flag = 0
EndIF
EndFuntcion
INITIAL_VELOCITY_BALL_X and INITIAL_VELOCITY_BALL_Y are set to 5.
It is working, but I don't think it has the right behavior, because the diff variable changes the ball X speed to actually change its angle. Sometimes when I want to increase the global speed (velocity_increase), the angle also changes.
Another problem is that, when the ball hits the paddle close to the center, I'm setting its speed to the default value without the diff factor, which makes hard to get an angle close to 90 degrees as the speed gets higher.
What I 'd like to do is be able to:
- make the angle and the speed independent
- make the angle close to 90 degrees when hit the center
- (opcional) make the angle and speed vary depends on the speed that the paddle were moving while hitting the ball.
I know I'm ask a lot and this is math/physics not really AppGameKit, but I have no idea where to start. I tried few experiments with Box2D, but I couldn't get anywere.
Any help will be appreciated.
Thanks
----------
Paulo
http://www.mobilecreators.com