Computer Science Canada

Shooting Problem

Author:  upthescale [ Wed Jun 21, 2006 10:24 pm ]
Post subject:  Shooting Problem

When you shoot (Enter) and move (Arrow keys) at the same time, the bulleyt will folloe the direction of your aimer, why?


code:

View.Set ("offscreenonly;graphics:600;250;nobuttonbar;position:100;100")
var xPos, yPos : real
var Angle : int := 0
var Move : array char of boolean
var xAim, yAim : real
var xBul, yBul : real
var Shoot1 : boolean := false
xPos := maxx div 2 - 10
yPos := 60
xAim := xPos
yAim := yPos
xBul := round (xPos)
yBul := round (yPos)
procedure Movement
    Input.KeyDown (Move)
    Draw.FillBox (round (xPos - 13), round (yPos), round (xPos + 13), round (yPos - 29), red)
    Draw.ThickLine (round (xAim), round (yAim), round (xAim + cosd (Angle) * 30), round (yAim + sind (Angle) * 30), 9, red)
    if Move (KEY_RIGHT_ARROW) then
        if Angle > 0 then
            Angle -= 2
        end if
    end if
    if Move (KEY_LEFT_ARROW) then
        if Angle < 180 then
            Angle += 2
        end if
    end if
end Movement
procedure Shoot


    if Move (KEY_ENTER) then
        if yBul > maxy or yBul < 0 or xBul > maxx or xBul < 0 then
            xBul := round (xPos)
            yBul := round (yPos)
        end if
        Shoot1 := true
    end if





    if Shoot1 = true then
        Draw.FillOval (round (xBul), round (yBul), 5, 5, red)
        xBul += cosd (Angle) * 3
        yBul += sind (Angle) * 3

    end if





end Shoot
loop
    Text.ColorBack (7)
    cls
    Draw.FillBox (0, 0, maxx, 30, 8)
    Movement
    Shoot
    Time.DelaySinceLast (5)
    View.Update
end loop


Author:  [Gandalf] [ Wed Jun 21, 2006 11:24 pm ]
Post subject: 

Because your x and y velocities of the bullet are being based off the angle of the cannon at that exact time, ie.
code:
        xBul += cosd (Angle) * 3
        yBul += sind (Angle) * 3

You would be better off having the angles at the time of shooting being stored in a variable, and then using that.


: