News:

Printed Amstrad Addict magazine announced, check it out here!

Main Menu
avatar_AMSDOS

Collision Detection

Started by AMSDOS, 10:48, 14 January 14

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

AMSDOS

I've rigged up this little example of a moving ship which moves through my ship (not going anywhere) and as it does the test comes into play to register on the Left Side as the moving ship travels through it, once it's completed the reverse is carried out which triggers the test for the lower side of the ship passing through the lower corner, however what's unusual with that test is a line below the actual moving ship is deleting my drawn ship and yet the character matrix for that pattern in the Amstrad Manual suggests the short 3 pixel stem at the bottom of that pattern matrix is the bottom of it yet TAG is deleting the line below it.

10 MODE 1:INK 1,26
20 GOSUB 2000
30 x=400:y=178:dir=2
40 LOCATE 20,1:PRINT "Intruder Base:"
50 LOCATE 1,1:PRINT "Intruder Top:"
60 WHILE 1
70 oldx=x:oldy=y
80 TAG:MOVE x,y:PRINT CHR$(226);:TAGOFF
90 x=x-2
100 LOCATE 35,1:PRINT TEST(x-6,y-14)
110 LOCATE 15,1:PRINT TEST(x-2,y)
120 IF x=250 THEN GOSUB 1000
130 IF y=200 AND dir=2 THEN y=212:dir=-2:GOSUB 2000
140 IF y=190 AND dir=-2 THEN y=178:dir=2:GOSUB 2000
150 WEND
1000 TAG
1010 MOVE oldx,oldy
1020 PRINT CHR$(32);
1030 TAGOFF
1040 x=400
1050 oldy=y
1060 y=y+dir
1070 RETURN
2000 PLOT 320,198,1
2010 DRAWR 10,-10
2020 DRAWR -10,-10
2030 DRAWR 0,20
2040 RETURN
* Using the old Amstrad Languages :D   * with the Firmware :P
* I also like to problem solve code in BASIC :)   * And type-in Type-Ins! :D

Home Computing Weekly Programs
Popular Computing Weekly Programs
Your Computer Programs
Updated Other Program Links on Profile Page (Update April 16/15 phew!)
Programs for Turbo Pascal 3

Bruce Abbott

#1
Congratulations, you found an error in the manual!

CHR$(226) is supposed to be the playing card 'Clubs' symbol. The manual shows a 5 pixel wide bar at the top, but this is wrong. The rest of the pattern should be moved up 1 line and the last line is blank (even though it is blank, it will still wipe out anything under it).

AMSDOS

Hmmm, so that explains why the bottom line was being deleted, just assumed the manual was correct and I had the right symbol.
* Using the old Amstrad Languages :D   * with the Firmware :P
* I also like to problem solve code in BASIC :)   * And type-in Type-Ins! :D

Home Computing Weekly Programs
Popular Computing Weekly Programs
Your Computer Programs
Updated Other Program Links on Profile Page (Update April 16/15 phew!)
Programs for Turbo Pascal 3

AMSDOS

Two more programs I've added carry out a 3 Point Collision Test using the Point Junctions of the Moving image to detect where it passes through the other image seems to produce a fairly accurate way of detecting where collision has occurred.

Initially I made 3PCOLSON.BAS which produces a flickery moving ship which works well, however I've made 3PCLSN2.BAS to simulate something with less flicker if anyone were interested, in that example I've removed the WAIT FRAME FLYBACK which seems to work well without.
* Using the old Amstrad Languages :D   * with the Firmware :P
* I also like to problem solve code in BASIC :)   * And type-in Type-Ins! :D

Home Computing Weekly Programs
Popular Computing Weekly Programs
Your Computer Programs
Updated Other Program Links on Profile Page (Update April 16/15 phew!)
Programs for Turbo Pascal 3

AMSDOS

I made another example relating to Collision Detection which I'm popping in here. This routine has some similarities to the last example I posted in the Silly Programming Ideas Thread (Attack of the Moving Shapes), though on this occasion I'm moving 3 Objects, a white border is drawn and 100 or so White Squares are randomly drawn onscreen. Initially if the Objects are drawn over the White Squares they'll move on til they find an open area and bounce off the Squares, the moving objects have their own set directions pretty much like a nasty from Roland in Time or Monty on the Run.




100 MODE 0:CALL &BC02:INK 0,11:BORDER 11:INK 1,26:PEN 1
110 FOR y=1 TO 25
120   LOCATE 1,y
130   PRINT CHR$(143);
140   LOCATE 20,y
150   PRINT CHR$(143);
160 NEXT y
170 LOCATE 1,1
180 PRINT STRING$(20,CHR$(143));
190 LOCATE 1,25
200 PRINT STRING$(20,CHR$(143));
210 FOR b=1 TO 100
220   RANDOMIZE TIME:RANDOMIZE RND
230   x=(RND*17)+2
240   y=(RND*23)+2
250   LOCATE x,y
260   PRINT CHR$(143);
270 NEXT b
280 PRINT CHR$(23)+CHR$(1); ' XOR Mode
290 w=320:z=350:u=288:v=238:s=96:t=94
300 a=32 :b=16 :c=a              :d=a
310 c=5:x=w:y=z:GOSUB 1000:x=u:y=v:c=1:GOSUB 1000:x=s:y=t:c=2:GOSUB 1000
320 LOCATE 1,1
330 WHILE INKEY(18)<>0 ' Enter/Return to Exit
340   x=w:y=z:c=5:z=z+b:GOSUB 1000:x=w:y=z:GOSUB 1000
350   IF TEST(w,z+16)=1 THEN b=-16 ELSE IF TEST(w,z-16)=1 THEN b=16
360   x=u:y=v:c=1:u=u+a:GOSUB 1000:x=u:y=v:GOSUB 1000
370   IF TEST(u+32,v)=1 THEN a=-32 ELSE IF TEST(u-32,v)=1 THEN a=32
380   x=s:y=t:c=2:s=s+d:GOSUB 1000:x=s:y=t:GOSUB 1000
390   IF TEST(s+32,t)=1 THEN d=-32 ELSE IF TEST(s-32,t)=1 THEN d=32
400 WEND
410 WHILE INKEY$<>"":WEND:MODE 2:END 
1000 PLOT -2,-2,c:MOVE x,y
1010 TAG
1020 PRINT CHR$(225);
1030 TAGOFF
1040 RETURN





[attachimg=1]



* Using the old Amstrad Languages :D   * with the Firmware :P
* I also like to problem solve code in BASIC :)   * And type-in Type-Ins! :D

Home Computing Weekly Programs
Popular Computing Weekly Programs
Your Computer Programs
Updated Other Program Links on Profile Page (Update April 16/15 phew!)
Programs for Turbo Pascal 3

AMSDOS


This following example I'm adding to this thread deals in having a character which follows your character around the screen, I'm referring to it as "Zombie Style" since it clearly follows your character, this example comes from the Lawn Mower Simulator from AA52 Type-Ins, which plays the dreaded person you must avoid with your mower. At this level though, I have not added the screen data, so no array has been setup let along any checks. The main thing I have here from the Lawn Mower Simulator is the "in" variable in line 120, this works as a real number, the larger the number is divided by 10 the faster the Obstacle moves.

100 MODE 0:INK 0,11:INK 1,26:INK 2,3,6:INK 3,25
110 LOCATE 1,25:PEN 3:PRINT CHR$(248)
120 x=1:y=25:x1=1:y1=1:ox=x:oy=y:in=1/10
130 WHILE 1
140   IF INKEY(1)=0 AND x<20 THEN ox=x:oy=y:GOSUB 1000:x=x+1:GOSUB 1010
150   IF INKEY(8)=0 AND x>1 THEN ox=x:oy=y:GOSUB 1000:x=x-1:GOSUB 1010
160   IF INKEY(0)=0 AND y>1 THEN oy=y:ox=x:GOSUB 1000:y=y-1:GOSUB 1010
170   IF INKEY(2)=0 AND y<25 THEN oy=y:ox=x:GOSUB 1000:y=y+1:GOSUB 1010
180   IF x1<x THEN dix=in
190   IF x1>x THEN dix=-in
200   IF y1<y THEN diy=in
210   IF y1>y THEN diy=-in
220   x2=x1:y2=y1:x1=x1+dix:y1=y1+diy
230   IF x1<>x2 OR y1<>y2 THEN LOCATE x2,y2:PRINT" ";
240   LOCATE x1,y1:PEN 2:PRINT CHR$(225);
250   IF x=ROUND(x1) AND y=ROUND(y1) THEN LOCATE 8,10:PEN 1:PRINT"Rats!!":END
260 WEND
1000 LOCATE ox,oy:PRINT" ";:RETURN
1010 LOCATE x,y:PEN 3:PRINT CHR$(248);:RETURN
* Using the old Amstrad Languages :D   * with the Firmware :P
* I also like to problem solve code in BASIC :)   * And type-in Type-Ins! :D

Home Computing Weekly Programs
Popular Computing Weekly Programs
Your Computer Programs
Updated Other Program Links on Profile Page (Update April 16/15 phew!)
Programs for Turbo Pascal 3

AMSDOS

Relating to Collision Detection, I made a couple of simple examples in BASIC to move a Character around a Maze. A 2D String Array is used to represent the Maze (Subroutine 1000-1230), a Key 'k' is placed within (Subroutine 2000-2070), the WHILE loop within will only place the key when 'w' and 'z' have found a spot represented as '0'.


In the 1st example, the character only moves over the '0' area when keypresses are made. I've added this because the problem with it is Line 920, which becomes a Long IF routine for when your Character has to move around the Key. It's not so much of a problem here because you character only has to grab a key and exit, but if a number of checks with various objects had to be made within a game, lines line 920 would slow the game right down:



100 MODE 0:INK 0,9:INK 1,12:INK 2,3
110 DEFINT a-z:GOSUB 1000:GOSUB 2000
120 x=2:y=2:PEN 2:LOCATE x,y:PRINT CHR$(248)
130 WHILE NOT finished
140   IF INKEY(1)=0 THEN IF s$(x+1,y)=CHR$(48) THEN GOSUB 900:x=x+1:GOSUB 910
150   IF INKEY(8)=0 THEN IF s$(x-1,y)=CHR$(48) THEN GOSUB 900:x=x-1:GOSUB 910
160   IF INKEY(0)=0 THEN IF s$(x,y-1)=CHR$(48) THEN GOSUB 900:y=y-1:GOSUB 910
170   IF INKEY(2)=0 THEN IF s$(x,y+1)=CHR$(48) THEN GOSUB 900:y=y+1:GOSUB 910
180   GOSUB 920
190   IF x=1 AND y=14 THEN finished=-1
200 CALL &BD19:CALL &BD19
210 WEND:LOCATE 1,17:WHILE INKEY$<>"":WEND:PEN 1:END
900 LOCATE x,y:PRINT " ";:RETURN
910 LOCATE x,y:PRINT CHR$(248);:RETURN
920 IF (s$(x+1,y)="k") OR (s$(x-1,y)="k") OR (s$(x,y+1)="k") OR (s$(x,y-1)="k") THEN LOCATE w,z:PRINT" ";:s$(w,z)="0":s$(2,14)="0":LOCATE 2,14:PRINT" ":RETURN
930 RETURN
1000 DIM s$(16,16):RESTORE 1080
1020 FOR y=1 TO 16
1021    READ a$
1030    FOR x=1 TO 16
1040        s$(x,y)=MID$(a$,x,1)
1050        IF s$(x,y)=CHR$(49) THEN LOCATE x,y:PEN 1:PRINT CHR$(143);
1060    NEXT x
1070 NEXT y:RETURN
1080 DATA 1111111111111111
1090 DATA 1000000100010001
1100 DATA 1101111001010101
1110 DATA 1000010011110101
1120 DATA 1010110110000101
1130 DATA 1010010000111001
1140 DATA 1011110111000001
1150 DATA 1000000100000001
1160 DATA 1111111000011111
1170 DATA 1001111001010001
1180 DATA 1010010011011101
1190 DATA 1010010110010101
1200 DATA 1001010111010101
1210 DATA 0101010110010101
1220 DATA 1100000111000001
1230 DATA 1111111111111111
2000 w=INT(RND*14)+2
2010 z=INT(RND*14)+2
2020 WHILE s$(w,z)=CHR$(49)
2030    w=INT(RND*14)+2
2040    z=INT(RND*14)+2
2050 WEND
2060 s$(w,z)="k":LOCATE w,z:PEN 4:PRINT s$(w,z);
2070 RETURN



The 2nd example removes that so key checks (Lines 140-170) follow by checks to see if a Wall is found 'CHR$(49)'. If it doesn't equal CHR$(49), the Man Moves in the Appropriate Direction, so objects in this example become a simple 's$(x,y)' check as done on Line 180 for the key and has the advantage of automatically deleting the "key" as your character move over it:



100 MODE 0:INK 0,9:INK 1,12:INK 2,3
110 DEFINT a-z:GOSUB 1000:GOSUB 2000
120 x=2:y=2:PEN 2:LOCATE x,y:PRINT CHR$(248)
130 WHILE NOT finished
140   IF INKEY(1)=0 THEN IF s$(x+1,y)<>CHR$(49) THEN GOSUB 900:x=x+1:GOSUB 910
150   IF INKEY(8)=0 THEN IF s$(x-1,y)<>CHR$(49) THEN GOSUB 900:x=x-1:GOSUB 910
160   IF INKEY(0)=0 THEN IF s$(x,y-1)<>CHR$(49) THEN GOSUB 900:y=y-1:GOSUB 910
170   IF INKEY(2)=0 THEN IF s$(x,y+1)<>CHR$(49) THEN GOSUB 900:y=y+1:GOSUB 910
180   IF s$(x,y)="k" THEN s$(x,y)="0":s$(2,14)="0"
190   IF x=1 AND y=14 THEN finished=-1
200 CALL &BD19:CALL &BD19
210 WEND:LOCATE 1,17:WHILE INKEY$<>"":WEND:PEN 1:END
900 LOCATE x,y:PRINT " ";:RETURN
910 LOCATE x,y:PRINT CHR$(248);:RETURN
1000 DIM s$(16,16):RESTORE 1080
1020 FOR y=1 TO 16
1021    READ a$
1030    FOR x=1 TO 16
1040        s$(x,y)=MID$(a$,x,1)
1050        IF s$(x,y)=CHR$(49) THEN LOCATE x,y:PEN 1:PRINT CHR$(143);
1060    NEXT x
1070 NEXT y:RETURN
1080 DATA 1111111111111111
1090 DATA 1000000100010001
1100 DATA 1101111001010101
1110 DATA 1000010011110101
1120 DATA 1010110110000101
1130 DATA 1010010000111001
1140 DATA 1011110111000001
1150 DATA 1000000100000001
1160 DATA 1111111000011111
1170 DATA 1001111001010001
1180 DATA 1010010011011101
1190 DATA 1010010110010101
1200 DATA 1001010111010101
1210 DATA 0101010110010101
1220 DATA 1100000111000001
1230 DATA 1111111111111111
2000 w=INT(RND*14)+2
2010 z=INT(RND*14)+2
2020 WHILE s$(w,z)=CHR$(49)
2030    w=INT(RND*14)+2
2040    z=INT(RND*14)+2
2050 WEND
2060 s$(w,z)="k":LOCATE w,z:PEN 4:PRINT s$(w,z);
2070 RETURN
* Using the old Amstrad Languages :D   * with the Firmware :P
* I also like to problem solve code in BASIC :)   * And type-in Type-Ins! :D

Home Computing Weekly Programs
Popular Computing Weekly Programs
Your Computer Programs
Updated Other Program Links on Profile Page (Update April 16/15 phew!)
Programs for Turbo Pascal 3

Powered by SMFPacks Menu Editor Mod