Collision Detection help Now with more complications!!!
Author |
Message |
Zerimas
|
Posted: Sat Jan 22, 2005 6:30 pm Post subject: Collision Detection help Now with more complications!!! |
|
|
EDIT (I forgot to post this earlier): I am using version 3.1.1a of turing
I'm working on a turing version of space invaders and I'm have trouble with collision detection.
Note in this progam "1" signifies your ship and "2" signifies and enemy ship.
These procedures are used for checking if the ship has crashed
code: |
proc coldecup
for col : 1 .. 13
for row : 1 .. 8
crash := false
if memory (col, row).shptype = 1 and memory (col, row +
1).shptype = 2 then
crash := true
end if
end for
end for
end coldecup
proc coldecdown
for col : 1 .. 13
for decreasing row : 9 .. 2
crash := false
if memory (col, row).shptype = 1 and memory (col, row -
1).shptype = 2 then
crash := true
end if
end for
end for
end coldecdown
proc coldecright
for col : 1 .. 12
for row : 1 .. 9
crash := false
if memory (col, row).shptype = 1 and memory (col + 1,
row).shptype = 2 then
crash := true
end if
end for
end for
end coldecright
proc coldecleft
for decreasing col : 13 .. 2
for row : 1 .. 9
crash := false
if memory (col, row).shptype = 1 and memory (col - 1,
row).shptype = 2
then
crash := true
end if
end for
end for
end coldecleft
|
I've included the proceudres in a process to move the ship
code: |
process moveship
loop
locate (1, 1)
showmem
if hasch then
getch (ch)
if ord (ch) = 203 then %left
for col : 1 .. 13
for row : 1 .. 9
coldecleft %check for collision
if memory (col, row).shptype = 1 and col > 1 then
memory (col - 1, row).shptype := 1
memory (col, row).shptype := 0
end if
end for
end for
elsif ord (ch) = 205 then %right
for decreasing col : 13 .. 1
for row : 1 .. 9
coldecright
if memory (col, row).shptype = 1 and col < 13
then
memory (col + 1, row).shptype := 1
memory (col, row).shptype := 0
end if
end for
end for
elsif ord (ch) = 200 then % up
for col : 1 .. 13
for decreasing row : 9 .. 1
coldecup
if memory (col, row).shptype = 1 and row < 9 then
memory (col, row + 1).shptype := 1
memory (col, row).shptype := 0
end if
end for
end for
elsif ord (ch) = 208 then % down
for col : 1 .. 13
for row : 1 .. 9
coldecdown
if memory (col, row).shptype = 1 and row > 1 then
memory (col, row - 1).shptype := 1
memory (col, row).shptype := 0
end if
end for
end for
end if
end if
BLACKBOXXX
%changepol
drawship
end loop
end moveship
|
Sometime my ship will be right next to the enemy ship and when I "collide " with the ship everything works properly. However, most of the time I'll be two "squares" (the ships are drawn based on the numbers in the array) away and mu ship will have "collided".
Any help would doubleplusappriciated!
Description: |
If you want the complete source code. . . |
|
Download |
Filename: |
gin-jin with crazy array.dat |
Filesize: |
8.1 KB |
Downloaded: |
157 Time(s) |
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
Cervantes
|
Posted: Sat Jan 22, 2005 6:53 pm Post subject: (No subject) |
|
|
Welcome to CompSci.ca.
The first thing I can think of is that you're using procedures for your collision detection rather than functions.
Let's take an example: coldecup. See, when you use procedures, and have them set up like that, it's really only checking if memory (13,8 ).shptype = 1 and memory (13, 9) = 2. The reason is that regardless of what happens before in the for loops, crash is reset to false. Then, when col = 13 and row = 8, it checks if there's a collision. You could overcome this by not using those two for loops and just checking at (shipRow, shipCol + 1) to see if there's an enemy ship there. Even if you do this, you should still be using a function (that returns a boolean variable).
Second, don't use processes. In Turing, they suck.
-Cervantes
|
|
|
|
|
|
Unreal_Origin
|
Posted: Sat Jan 22, 2005 6:55 pm Post subject: hey just post all of your code |
|
|
i would suggest
your ship should has a var for its top right (x2, y2) coordinate and top left (x1, y2)
also you should have bottom left and bottom right of your the enemy (e_x1, e_y1) and the bottom left (e_x2,e_y1)
have an if statement that if the y coordinates of the enemies is in the area of the players ship
THIS IS BLIND PROGRAMING
code: |
if e_y1> y2 and e_y1< y1 then
if e_x1> x1 and e_x1=< x2 or e_x1=> x1 and e_x2< x2 then % it may need to be <= instead
%they die
end if
end if
|
THIS IS ALL FOR IF YOUR ENEMIES ARE COMING TOWARDS YOU (top to bottom) IF NOT THEN CHANGE IT A LITTLE BIT
|
|
|
|
|
|
Zerimas
|
Posted: Sat Jan 22, 2005 7:22 pm Post subject: Thanks |
|
|
Thanks for all the help! I have one more question: how would I go about using a function for collision detection? I've never used a function before. . .
|
|
|
|
|
|
Cervantes
|
Posted: Sat Jan 22, 2005 7:29 pm Post subject: (No subject) |
|
|
let's modify your coldecup procedure.
Turing: |
function coldecup : boolean %the : boolean means the function returns a boolean value
for col : 1 .. 13
for row : 1 .. 8
if memory (col, row).shptype = 1 and memory (col, row +
1).shptype = 2 then
result true
end if
end for
end for
result false
end coldecup
|
As soon as the result line is reached, the function returns that value and exits. So, if the if statement nested in the two for loops is true, the function will return true and quit. If the for loops finish and the if statement never was true, the function will return false.
-Cervantes
|
|
|
|
|
|
Zerimas
|
Posted: Sat Jan 22, 2005 8:40 pm Post subject: (No subject) |
|
|
Okay, I've converted my collision detection procedures to functions, now what?
|
|
|
|
|
|
Cervantes
|
Posted: Sat Jan 22, 2005 8:46 pm Post subject: (No subject) |
|
|
Awesome. Now, I guess you'll want to check, each time through your loop, if your ship has collided with an enemy ship. If so, do something, such as display a "you suck" screen.
Turing: |
var winYouSuck : int
loop %main loop
if coldecup or coldecdown or coldecright or coldecleft then
winYouSuck := Window.Open
put "You suck"
end if
end loop
|
Or, maybe you'd want to take a life away from the player. Whatever you want
|
|
|
|
|
|
Zerimas
|
Posted: Sat Jan 22, 2005 9:03 pm Post subject: (No subject) |
|
|
Gak, no matter what I do the functions seem to be returning a true value all the time.
code: |
loop
if coldecup or coldecdown or coldecleft or coldecright = true then
over := Window.Open ("position: 300;300, graphics 300;300")
put "FISSION MAILED"
end if
drawstarz
drawenemy
randcol := Rand.Int (1, 13)
%- spawnenemy
end loop
|
[/code]
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
Cervantes
|
Posted: Sat Jan 22, 2005 9:33 pm Post subject: (No subject) |
|
|
Then there's a problem with the function, or the information that the function uses.
|
|
|
|
|
|
Zerimas
|
Posted: Sat Jan 22, 2005 9:56 pm Post subject: (No subject) |
|
|
Today is not a good day. . . I keep getting and illegal pic identifier error every time the program attempt s to draw and enemy ship. . . It was working fine before.
|
|
|
|
|
|
Zerimas
|
Posted: Sat Jan 22, 2005 10:39 pm Post subject: (No subject) |
|
|
Hurray, I got my program working. . . for the most part. One more question how do you make the ships "collide" only when the "good" ship actually moving onto the enemy "square" not when both ships are next to each other?
Description: |
An updated copy of the code. . . |
|
Download |
Filename: |
gin-jin with crazy array.dat |
Filesize: |
8.41 KB |
Downloaded: |
136 Time(s) |
|
|
|
|
|
|
Cervantes
|
Posted: Sun Jan 23, 2005 9:10 am Post subject: (No subject) |
|
|
The whole way you're going about this is a little inefficient. See, you're storing information as to where your ship and all the enemy ships are based on a value of a grid 2D array. That means, to draw your ship, you have to sift through all the elements of that grid array to find which element = 1, then draw your ship there. Also, it prevents you from Having more than one thing in the same spot.
What you should probably be doing is store the location of the ship in two variables (or, better yet, one variable that uses a record), x and y (or row and column). Store the information of the enemy ships in an array of record. You'll want to store x and y, and perhaps direction as well.
If you do this, collision detection suddenly becomes a whole lot easier. Also, as an added benefit, the program will run faster, since you don't have to go through a million for loops.
Turing: |
for i : 1 .. upper (enemyShip )
if player.row = enemyShip (i ).row and player.col = enemyShip (i ).col then
%you suck!
end if
end for
|
|
|
|
|
|
|
Zerimas
|
Posted: Sun Jan 23, 2005 12:01 pm Post subject: (No subject) |
|
|
Thanks for all your help! But I don't know if I'll have the time to modify my program to the extent you're suggesting as I have to get this done for tommorow. . . but I'll give your suggestion a try.
|
|
|
|
|
|
Zerimas
|
Posted: Sun Jan 23, 2005 3:34 pm Post subject: (No subject) |
|
|
Alright I've taken Cervante's advice and begun modifing my program. Now another problem has arisen: my enemy ships just stop moving at a certain point on the screen for no apparent reason.
Here's and updated copy of the code:
Once again I'd like to thank everyone for all their help
Description: |
|
Download |
Filename: |
Project.t |
Filesize: |
3.82 KB |
Downloaded: |
145 Time(s) |
|
|
|
|
|
|
|
|