Posted: 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
Tony
Posted: Thu Jan 09, 2003 3:20 am Post subject: (No subject)
Posted: 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
Posted: 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
Posted: 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
Computer Science CanadaHelp with programming in C, C++, Java, PHP, Ruby, Turing, VB and more!
Mazer
Posted: Thu Dec 04, 2003 10:18 pm Post subject: (No subject)
lol!
Sponsor Sponsor
the_short1
Posted: 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
Posted: 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 If you ask nicely (and if your name does not contain dodge) you usually get more
Posted: Sun Dec 07, 2003 7:56 pm Post subject: (No subject)
hey... oh well, me dont need bits anymore...
TheDarkArcher
Posted: 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
Posted: 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
if you move the mouse fast enough it works, but if you go just a little slower, it passes right through
DanShadow
Posted: 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.
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
Posted: 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.