Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Following and attempt at collision detection.
Index -> Programming, Turing -> Turing Tutorials
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
RaPsCaLLioN




PostPosted: Thu Jan 09, 2003 3:14 am   Post subject: 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.

code:
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)
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Thu Jan 09, 2003 3:20 am   Post subject: (No subject)

thats some interesting code you got there

thx for sharing Very Happy
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
azndragon




PostPosted: Sat Jan 25, 2003 7:35 pm   Post subject: (No subject)

What's RTS?
Tony




PostPosted: Sat Jan 25, 2003 8:43 pm   Post subject: (No subject)

my guess is Real Time Strategy...
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
the_short1




PostPosted: Wed Dec 03, 2003 6:25 pm   Post subject: 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




PostPosted: Thu Dec 04, 2003 10:04 pm   Post subject: (No subject)

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




PostPosted: Thu Dec 04, 2003 10:08 pm   Post subject: (No subject)

dodge_tomahawk wrote:
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 or Very Mad
Computer Science Canada Help with programming in C, C++, Java, PHP, Ruby, Turing, VB and more!
Mazer




PostPosted: Thu Dec 04, 2003 10:18 pm   Post subject: (No subject)

lol!
Sponsor
Sponsor
Sponsor
sponsor
the_short1




PostPosted: Sat Dec 06, 2003 11:58 pm   Post subject: 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 <<<<under contruction....
Thanks Kevin The Short One

p.s. my game has totaled 1700 lines of code so far..... i know LOTS!!! but i am a Novice so yea....dont want to make it complicated and advanced...
Tony




PostPosted: Sun Dec 07, 2003 3:24 am   Post subject: (No subject)

for credits, just state user's username and "www.compsci.ca"

and yes, you do get bits for submiting stuff Smile If you ask nicely (and if your name does not contain dodge) you usually get more Wink
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Andy




PostPosted: Sun Dec 07, 2003 7:56 pm   Post subject: (No subject)

hey... oh well, me dont need bits anymore...
TheDarkArcher




PostPosted: Thu Dec 11, 2003 2:04 pm   Post subject: It is one ery interesting RTS I like the collision detection

Great collision detection but maybe it should bounce off or something but whayt do I know!
Thuged_Out_G




PostPosted: Sat Dec 13, 2003 3:34 am   Post subject: (No subject)

ok, so its alotta code, but does it work?
well, it seems i can pass that blue circle right through the red ones Confused
if you move the mouse fast enough it works, but if you go just a little slower, it passes right through
DanShadow




PostPosted: Sat Dec 13, 2003 5:22 pm   Post subject: (No subject)

Hmm...thats pretty good. I dont remember where I put the topic, but I worked on an RTS thing too. You could probably learn a thing or two. Your game, you have 1 unit who follows your mouse, I had it, so you select a unit (left click), then move it (right click), the deselect it (left click where it isnt). It proved useful, and th en I learned more, and if you use a bunch of arrays, you can make the most elite RTS EVER! Here are a few you might use:
unit_hp,unit_x,unit_y,unit_attack,unit_destination_x,unit_destination_y,unit_reached_x,unit_reached_y,unit_moving....and so forth
You could have seriously as good a game as Age of Empires with this kind of stuff. I decided to drop that for an even bigger project.......
*(drumroll)*
Im making a scroller RPG, fully graphical, similar to the first Diablo. Very Happy
It is an extremely hard project, and im going to use it as my Grade 11 Computer Science Final Project. (Supposedly the best thing my teacher has ever seen is snakes and ladders, so this is gonna give him a heart attack!)
DanShadow




PostPosted: Sat Dec 13, 2003 5:24 pm   Post subject: (No subject)

Oh and Dodge, is your pic a screenshot from the new "Starcraft, ghost" thing? Ive never played it, but 'just' noticed the cloaking thing on your item spot.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 16 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: