
-----------------------------------
mike200015
Sat Apr 09, 2005 4:35 pm

Ball Moving Towards Mouse
-----------------------------------
Im working on a pong game, and what i have now is you can move the ball around on the paddle, and then when u press UP then the ball releases, but it always goes in the same direction because i have the ballx + 1 and bally + 1 , but i want to change it so that the user moves their mouse somewhere on the screen , and the ball shoots in that direction.
Heres the paddle and ball part of my game:

There is also a little problem with this, dont know how to fix it; if the paddle is moving and the ball hits it, then for some reason the ball doesnt bounce off, it stays on it and moves with it :? 
var ballx := maxx div 2
var bally := 15
var pos := maxx div 2
var xmod := 1
var ymod := 1
var chars : array char of boolean
setscreen ("offscreenonly")
loop
    cls
    drawfillbox (pos - 25, 0, pos + 25, 5, 1) %user paddle
    drawfilloval (ballx, bally, 10, 10, 12) %ball
    Input.KeyDown (chars)
    if chars (KEY_LEFT_ARROW) and pos - 25 > 0 then %Moves paddle left
        pos -= 2
        ballx -= 2
    elsif chars (KEY_RIGHT_ARROW) and pos + 25 < maxx then   %Moves paddle right
        pos += 2
        ballx += 2
    elsif chars (KEY_UP_ARROW) then
        exit
    end if
    delay (2)
    View.Update
end loop
loop
    ballx += xmod
    bally += ymod
    if ballx >= maxx - 5 then
        xmod := -xmod             %ball goes in opposite x-direction
        sound (500, 10)
    elsif ballx = maxy - 5 then
        ymod := -ymod         %ball goes in opposite y-direction
        sound (500, 10)
    elsif bally  0 then    %Moves paddle left
        pos -= 2
    elsif chars (KEY_RIGHT_ARROW) and pos + 25 < maxx then   %Moves paddle right
        pos += 2
    end if
    case whatdotcolour (ballx + 11, bally) or whatdotcolour (ballx - 11, bally) of     
        label blue :
            xmod := -xmod
        label :
            ballx += xmod
    end case
    case whatdotcolour (ballx, bally + 11) or whatdotcolour (ballx, bally - 11) of          
        label blue :
            ymod := -ymod
        label :
            bally += ymod
    end case
    View.Update
    delay (5)
    cls
end loop


-----------------------------------
Cervantes
Sat Apr 09, 2005 5:17 pm


-----------------------------------
I didn't feel like working with your code, so I started fresh:

View.Set ("offscreenonly")
Mouse.ButtonChoose ("multibutton")

var ball :
    record
        x, y, vx, vy : real
        speed : int
    end record
ball.x := maxx / 2
ball.y := maxy / 2
ball.vx := 0
ball.vy := 0
ball.speed := 2

var deltaX, deltaY, hyp : real

var mx, my, btn : int
var keys : array char of boolean

loop

    cls

    Input.KeyDown (keys)
    if keys (KEY_LEFT_ARROW) then
        ball.x -= 2
    end if
    if keys (KEY_RIGHT_ARROW) then
        ball.x += 2
    end if
    if keys (KEY_DOWN_ARROW) then
        ball.y -= 2
    end if
    if keys (KEY_UP_ARROW) then
        ball.y += 2
    end if
    Mouse.Where (mx, my, btn)
    if btn = 1 then
        deltaX := mx - ball.x
        deltaY := my - ball.y
        hyp := (deltaX ** 2 + deltaY ** 2) ** 0.5
        ball.vx := deltaX / hyp * ball.speed
        ball.vy := deltaY / hyp * ball.speed
        Draw.Line (round (ball.x), round (ball.y), round (ball.x + deltaX / hyp * 30), round (ball.y + deltaY / hyp * 30), black)
    end if
    if btn = 100 then
        ball.vx := 0
        ball.vy := 0
    end if

    ball.x += ball.vx
    ball.y += ball.vy


    drawfilloval (round (ball.x), round (ball.y), 5, 5, black)
    View.Update
    delay (10)

end loop


As for your collision problem, my first suggestion would be to use something other than whatdotcolour.  Coordinate comparisons would be nice and easy.

-----------------------------------
mike200015
Sat Apr 09, 2005 5:23 pm


-----------------------------------
lol .. so confused with that  :? . Do you mind explaining it??
wats the hyp, vx and vy for? And dnt understand the whole section when btn=1

-----------------------------------
Cervantes
Sat Apr 09, 2005 6:04 pm


-----------------------------------
the vx and vy are the x and y velocities of the ball.
hyp = hypoteneuse.  I calculated it by Pythagoras.
given the point (ball.x, ball.y) and (mx, my), you can draw a hypotenuse from the one point to the other and then make a right angle triangle based on the hyp.  If you take the horizontal side (deltaX) and divide it by the hyp, you'll get a decimal that is proportional to the length of deltaX.  Therefore, the bigger deltaX, the bigger the ball.vx will be.  Do the same for the verticle.  I multiplied by ball.speed to make the ball move a bit faster.

-----------------------------------
mike200015
Sat Apr 09, 2005 8:48 pm


-----------------------------------
:? thats some crazy stuff lol.. with the user choosing the direction there could be a problem, cuz if the user's mouse is straight up, then the ball will jus keep going straight up and down wont it?

-----------------------------------
Cervantes
Sat Apr 09, 2005 9:09 pm


-----------------------------------
Yes, unless you've got some curved surfaces, and account for those in your collisions.  
So, if the user chooses to fire it straight up, they lose.  :)
