
-----------------------------------
RaPsCaLLioN
Thu Jan 09, 2003 3:14 am

Following and attempt at collision detection.
-----------------------------------
Here's a little of my code that I'm using as a basis for an RTS style fighting system.  Have some fun with it.  Tell me what you think.

import GUI in "%oot/lib/GUI"
var iWindow : int := Window.Open ("offscreenonly, graphics:640;480, nocursor, noecho ")

type obstacle :
    record
        x : int
        y : int
    end record

var iDotX : int := 320
var iDotY : int := 240
var iM, iB : real
var iX, iY, iButton : int
var iSpeed : int := 5
var bCollision : boolean := false

var aiBlockers : array 1 .. 10 of obstacle
for i : 1 .. 10
    randint (aiBlockers (i).x, 10, 630)
    randint (aiBlockers (i).y, 10, 470)
end for

procedure CheckAndMove (iValue, iIncrement, iDir : int, bIsX : boolean)

    var bCollision : boolean := false

    for i : 1 .. 10
        if bIsX = true then
            if sqrt ((iDotX + (iDir * iIncrement) - aiBlockers (i).x) ** 2 + (iDotY - aiBlockers (i).y) ** 2) < 20 then
                bCollision := true
            end if
        else
            if sqrt ((iDotX - aiBlockers (i).x) ** 2 + (iDotY + (iDir * iIncrement) - aiBlockers (i).y) ** 2) < 20 then
                bCollision := true
            end if
        end if
    end for

    if bCollision = true then
    else
        if bIsX = true then
            iDotX += iDir * iIncrement           
            iDotY := round ((iM * iDotX) + iB)
        else
            iDotY += iDir * iIncrement
            iDotX := round ((iDotY - iB) / iM)
        end if
    end if

end CheckAndMove

procedure Display

    delay (10)
    GUI.SetBackgroundColour (7)
    drawfilloval (iDotX, iDotY, 10, 10, 1)
    for i : 1 .. 10
        drawfilloval (aiBlockers (i).x, aiBlockers (i).y, 10, 10, 4)
    end for

    Window.Update (iWindow)

end Display

loop

    Display

    mousewhere (iX, iY, iButton)

    %  If Dot's location is the same as mouse then do nothing.  If Dot's path has a slope of 0 then this just moves it up the
    % line.  Otherwise this calculates slope.
    if iX = iDotX and iY = iDotY then
    elsif iX = iDotX then
        if iY > iDotY then
            iDotY += iSpeed
        else
            iDotY -= iSpeed
        end if
    elsif iY = iDotY then
        if iX > iDotX then
            iDotX += iSpeed
        else
            iDotX -= iSpeed
        end if
    else
        iM := (iY - iDotY) / (iX - iDotX)
        iB := iDotY - (iM * iDotX)
    end if

    %  This determines whether to move the Dot primarily along the X or Y axis and then calculate the other coordinate.
    if iDotX < iX and iDotY < iY then
        if iX - iDotX < iY - iDotY then
            CheckAndMove (iDotY, iSpeed, 1, false)
            %iDotY += iSpeed
            %iDotX := round ((iDotY - iB) / iM)
        else
            CheckAndMove (iDotX, iSpeed, 1, true)
            %iDotX += iSpeed
            %iDotY := round ((iM * iDotX) + iB)
        end if
    elsif iDotX < iX and iDotY > iY then
        if iX - iDotX < iDotY - iY then
            CheckAndMove (iDotY, iSpeed, -1, false)
            %iDotY -= iSpeed
            %iDotX := round ((iDotY - iB) / iM)
        else
            CheckAndMove (iDotX, iSpeed, 1, true)
            %iDotX += iSpeed
            %iDotY := round ((iM * iDotX) + iB)
        end if
    elsif iDotX > iX and iDotY < iY then
        if iDotX - iX < iY - iDotY then
            CheckAndMove (iDotY, iSpeed, 1, false)
            %iDotY += iSpeed
            %iDotX := round ((iDotY - iB) / iM)
        else
            CheckAndMove (iDotX, iSpeed, -1, true)
            %iDotX -= iSpeed
            %iDotY := round ((iM * iDotX) + iB)
        end if
    elsif iDotX > iX and iDotY > iY then
        if iDotX - iX < iDotY - iY then
            CheckAndMove (iDotY, iSpeed, -1, false)
            %iDotY -= iSpeed
            %iDotX := round ((iDotY - iB) / iM)
        else
            CheckAndMove (iDotX, iSpeed, -1, true)
            %iDotX -= iSpeed
            %iDotY := round ((iM * iDotX) + iB)
        end if
    else
    end if

    %  Moves Dot to mouse location if closer than 5 to avoid jumping when mouse isn't moving.
    if sqrt ((iY - iDotY) ** 2 + (iX - iDotX) ** 2) < iSpeed then
        iDotX := iX
        iDotY := iY
    end if

    exit when hasch
    exit when buttonmoved ("down")

end loop

Window.Close (iWindow)

-----------------------------------
Tony
Thu Jan 09, 2003 3:20 am


-----------------------------------
thats some interesting code you got there

thx for sharing  :D

-----------------------------------
azndragon
Sat Jan 25, 2003 7:35 pm


-----------------------------------
What's RTS?

-----------------------------------
Tony
Sat Jan 25, 2003 8:43 pm


-----------------------------------
my guess is Real Time Strategy...

-----------------------------------
the_short1
Wed Dec 03, 2003 6:25 pm

CAn i use ur code?
-----------------------------------
Can i use this code modified a bit in a game i made... i though i would ask your permission before i make my game available on my website... i will add your name in my credits so also what is your name...i made a PacMan and used for the walls of pacman arena and to follow pac around... i will post in user submited program soon...Thanks

-----------------------------------
Andy
Thu Dec 04, 2003 10:04 pm


-----------------------------------
wow... do u ppl ever learn? USE whatdotcolor... it saves you so much code.... and it wont bug up like urs but aside from that, cool

-----------------------------------
Dan
Thu Dec 04, 2003 10:08 pm


-----------------------------------
wow... do u ppl ever learn? USE whatdotcolor... it saves you so much code.... and it wont bug up like urs but aside from that, cool

grrrrr, why not just throw some goto lines in there dodge  :evil:

-----------------------------------
Mazer
Thu Dec 04, 2003 10:18 pm


-----------------------------------
lol!

-----------------------------------
the_short1
Sat Dec 06, 2003 11:58 pm

Where is Rapscallion
-----------------------------------
I want to post on net SOON!!!! i need to know if hes gona let me or not... and i want to know his name for my credits screen...PacMan so far has one enemy moving and full movement for pacman, all dot vraibles, all score death, and boudaraies in effect.... jsut need to multiply enemy times 4.... and fix wall boundaries for enemy a bit.... soo many people are awaiting the arival of my game.... would i get bits for submitted to Compsci.ca also...???  i wanted to post on my turing site www.geocities.com/the_short22/turing.html   