Posted: 10th Oct 2011 0:49
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:

+ Code Snippet
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
Posted: 10th Oct 2011 21:30
Ok,

after burning the midnight oil, I think I've came up with the right solution. Here is the code, if you are curious:

+ Code Snippet
if ball.flag = 0 and GetSpriteCollision ( player.sprite, ball.sprite ) = 1

        anglel = 15.0
        angleh = 165.0
        factor as float
        ballmid as float
        angle_rad as float
        angle as float

        PlaySound(hit_sound)
        IncreaseBallSpeed()
        ball.flag = 1    // That way the collision will be tested just once
        // invert ball y and x
        ballmid = (ball.x#) - GetSpriteX(player.sprite)
        factor = ballmid / player.width
        // keep factor constrained
        if (factor < 0)
            factor = 0.001
        else
            if (factor > 1)
                factor = 0.999
            endif
        endif
        if (factor = 0.5)
            //exactly in the middle
            factor = 0.49
        endif
        // get the angle calculation
        angle = angleh - factor * (angleh - anglel)
        ball.angle = angle * (3.141517 / 180)
        ball.xv = velocity_increase * CosRad(ball.angle)
        ball.yv = -(velocity_increase * SinRad(ball.angle))
endif


I'm not considering the paddle speed at all, but I like the way it is working now.

Cheers
Posted: 11th Oct 2011 12:41
nice