Computer Science Canada Collsion Detection Help For My Game |
Author: | The Lone Ranger [ Tue Dec 15, 2009 12:31 pm ] |
Post subject: | Collsion Detection Help For My Game |
I am trying to use a collision for my game in Turing. I added something that should use collison detection but it's not working. The program should also exit after the collision, but that to doesn't work. Please help. [syntax="turing"] View.Set ("graphics:640,427") var name : string var chars : array char of boolean var pic : int := Pic.FileNew ("E:\\images2.jpg") var backGround : int := Pic.FileNew ("E:\\back.jpg") var asteroid : int := Pic.FileNew ("E:\\Asteroid.jpg") var x, y : int var x2, y2 : int const ship := "x" colorback (white) x := 10 y := 10 x2 := 100 y2 := 100 procedure DoMusic loop Music.PlayFile ("E:\\pathetic.WAV") end loop end DoMusic put "Please input your name: " .. get name cls process downFall loop randint (x2, 0, 600) for decreasing i : maxy .. 0 by 20 Pic.Draw (asteroid, x2, i, picCopy) delay (100) cls if i <= 20 then delay (10) end if end for end loop end downFall fork downFall loop Pic.Draw (backGround, 0, 0, picCopy) if backGround = 0 then put "Unable to load JPEG: ", Error.LastMsg end if Music.PlayFile ("DoMusic") loop if x2 < x and y2 = y then put "They have collided!" exit end if loop Input.KeyDown (chars) if chars (KEY_RIGHT_ARROW) then x := x + 5 elsif chars (KEY_LEFT_ARROW) then x := x - 5 end if if pic = 0 then put "Unable to load JPEG: ", Error.LastMsg end if Pic.Draw (pic, x, y, picCopy) delay (10) end loop end loop end loop The bolded section is the collision detection. |
Author: | mirhagk [ Tue Dec 15, 2009 2:49 pm ] |
Post subject: | RE:Collsion Detection Help For My Game |
your collision detection is pretty ghastly so first I want to point out that it only runs the collsion detection part once because it's outside of the innermost loop (which never exits) remove the middle loop and then your on your way to making it work |
Author: | The Lone Ranger [ Tue Dec 15, 2009 2:59 pm ] |
Post subject: | Re: Collsion Detection Help For My Game |
Which one is the middle loop? Also if I remove the middle loop will the collision detection work? |
Author: | mirhagk [ Tue Dec 15, 2009 3:08 pm ] |
Post subject: | RE:Collsion Detection Help For My Game |
the one that is highlighted in black is the middle loop (move the code below it into the same loop as the Pic.Draw) no I don't believe it will work in spite of that because right now as long as the asteroids y position is the same as the player's and the x position is less than his position (ie. to the left of it) what you want is x2=x and y2=y (but that only checks for the middle of the asteroid. Look here for better collision detection) I strongly suggest you check out the above link |
Author: | The Lone Ranger [ Tue Dec 15, 2009 3:52 pm ] |
Post subject: | Re: Collsion Detection Help For My Game |
Do you want me to use the tutorial on the first page? |
Author: | The Lone Ranger [ Tue Dec 15, 2009 5:07 pm ] |
Post subject: | Re: Collsion Detection Help For My Game |
Sorry for the double post, but could someone please help me improve my collision detection, so that I can finish my program. In my you play as a ship and are trying to dodge falling asteroids. Unfortunatley I haven't figured out how to properly use a collision. Any help at all would be greatly appreciated. |
Author: | Tony [ Tue Dec 15, 2009 6:47 pm ] |
Post subject: | RE:Collsion Detection Help For My Game |
I find it interesting how you post a question, use the answer to change your code a bit, but not enough to actually solve your problem, and so you create a new thread asking essentially the same question. |
Author: | The Lone Ranger [ Tue Dec 15, 2009 9:32 pm ] |
Post subject: | Re: Collsion Detection Help For My Game |
Yes... So can you help? |
Author: | mirhagk [ Wed Dec 16, 2009 8:18 am ] | ||
Post subject: | RE:Collsion Detection Help For My Game | ||
if you had clicked on the link I had given you then you wouldn't need our help. First off there's many different kind's of collision detection There's rectangular collision detection (if two boxes intersect or not), circular collision detection (if two circles collide or not), mix and match(if a circle and a box collide), whatdotcolour(very precise, not very effecient), or linear collision detection (the most difficult to learn but the best in my opinion). In your program you will likely want circular collision detetion, it's very easy to learn. go to this tutorial http://compsci.ca/v3/viewtopic.php?t=13661 but basically you use Math.Distance which returns the distance between two points which you specify. For example:
if two points, (x1,y1) and (x2,y2) are within 5 pixels then they collide. |
Author: | The Lone Ranger [ Wed Dec 16, 2009 4:08 pm ] |
Post subject: | Re: Collsion Detection Help For My Game |
I have changed my collision detection once again. I haven't gotten very far. Here it is, View.Set ("graphics:640,427") % The variables for our program var name : string var chars : array char of boolean var pic : int := Pic.FileNew ("F:\\images2.jpg") var backGround : int := Pic.FileNew ("F:\\back.jpg") var asteroid : int := Pic.FileNew ("F:\\Asteroid.jpg") var x, y : int var x2, y2 : int const ship := "x" colorback (white) % The values of x, y, x2, and y2 x := 10 y := 10 x2 := 100 y2 := 100 var hit : boolean := false % The procedure to play music procedure DoMusic loop Music.PlayFile ("E:\\pathetic.WAV") % This is the music file end loop end DoMusic put "INSTRUCTIONS" put "To play this game you must use the left and right arrow keys to move your space ship, and dodge the falling asteroids." put "Good Luck!" put "Please input your name: " .. get name cls proc collision (x : int, y : int, w : int, h : int, x1 : int, y1 : int, w1 : int, h1 : int) if ((x >= x1 and x <= (x1 + w1)) and (y >= y1 and y <= (y1 + h1))) then hit := true elsif (((x + w) >= x1 and (x + w) <= (x1 + w1)) and (y >= y1 and y <= (y1 + h1))) then hit := true elsif ((x >= x1 and x <= (x1 + w1)) and ((y + h) >= y1 and (y + h) <= (y1 + h1))) then hit := true elsif(((x + w) >= x1 and (x + w) <= (x1 + w1)) and ((y + h) >= y1 and (y + h) <= (y1 + h1))) then hit := true end if put hit end collision % The process to make the asteroids fall process downFall loop randint (x2, 0, 600) % This randomizes the x position of the asteroid for decreasing i : maxy .. 0 by 20 % This command draws the asteroids Pic.Draw (asteroid, x2, i, picCopy) delay (100) cls if i <= 20 then delay (10) end if end for end loop end downFall fork downFall loop %This command draws the background Pic.Draw (backGround, 0, 0, picCopy) if backGround = 0 then put "Unable to load JPEG: ", Error.LastMsg end if % The call to the DoMusic procedure Music.PlayFile ("DoMusic") % This loop is for the controls in the game loop collision (x, y, 158, 72, x2, y2, 70, 73) Input.KeyDown (chars) if chars (KEY_RIGHT_ARROW) then %If the right arrow key is pressed, then the space ship will move to the right x := x + 5 elsif chars (KEY_LEFT_ARROW) then %If the left arrow key is pressed, then the space ship will move to the left x := x - 5 end if % This error message displays if the picture is unable to load if pic = 0 then put "Unable to load JPEG: ", Error.LastMsg end if % This is the command to draw the space ship Pic.Draw (pic, x, y, picCopy) delay (10) end loop end loop The bolded section is my collsion detection. When I run the program all it says in the run screen is false over and over again, while my game is playing. I really need help, please. |
Author: | mirhagk [ Wed Dec 16, 2009 4:15 pm ] | ||||
Post subject: | RE:Collsion Detection Help For My Game | ||||
thats your problem, its going to keep telling you it's false. Put the following just before that line:
that resets the text cursor to the top (so it only says false at the top) |
Author: | The Lone Ranger [ Wed Dec 16, 2009 4:28 pm ] |
Post subject: | Re: Collsion Detection Help For My Game |
Now in the run window it doesn't say false continously, it just says it once and it stay there the whole game. But it never says true or that a collision has occured. How do I get it to say true. Could you please help me out with that. |
Author: | mirhagk [ Wed Dec 16, 2009 4:35 pm ] | ||
Post subject: | RE:Collsion Detection Help For My Game | ||
if it doesn't say true then they never collide. Looking at your collision detection I'm not exactly sure what's wrong. It's kinda a little messy. Here I'll give you the code you probably want
that's rectangular collision detection, try that out in your game and see if it works. |
Author: | The Lone Ranger [ Wed Dec 16, 2009 4:43 pm ] |
Post subject: | Re: Collsion Detection Help For My Game |
Sorry, but the new procedure that you gave me didn't do anything. I may be going crazy but I think when the asteroid touches the ship the text that says false flashes for a second. I don't know if that has anything to do with it or not. Also I need to make the program stop running if there is a collision. Could you also help me with that please? |
Author: | mirhagk [ Wed Dec 16, 2009 4:49 pm ] | ||
Post subject: | RE:Collsion Detection Help For My Game | ||
well to make it exit you would simply put
now if hit is true then it will exit, just make sure that little code is in all the loops okay. |
Author: | The Lone Ranger [ Wed Dec 16, 2009 5:00 pm ] |
Post subject: | Re: Collsion Detection Help For My Game |
Where exactly would that go in my collision procedure. |
Author: | TheGuardian001 [ Wed Dec 16, 2009 9:58 pm ] |
Post subject: | Re: Collsion Detection Help For My Game |
Depends on when you want it to exit. Presumably sometime between when hit is set and the end of the procedure. |
Author: | The Lone Ranger [ Thu Dec 17, 2009 11:43 am ] |
Post subject: | Re: Collsion Detection Help For My Game |
I put exit when hit into my program,but it still doesn't work. I feel like I've tried everything to make this collision work, could you please offer me some more help. |
Author: | mirhagk [ Thu Dec 17, 2009 1:26 pm ] |
Post subject: | RE:Collsion Detection Help For My Game |
you need to put the exit when code in all the loops you have (so that it will exit all the loops once it's hit) |
Author: | The Lone Ranger [ Thu Dec 17, 2009 7:58 pm ] |
Post subject: | Re: Collsion Detection Help For My Game |
Thank you so much for that. I am almost done my collisions, I have some of it working but I still need a little bit more help. The program ends when it says there is a collision, but the collision occurs before the asteroid and the ship touch. Here is the code, % Mohammad Malik and Ronny Blanco % The Asteroid Game % December 1st, 2009 View.Set ("graphics:640,427") % The variables for our program var name : string var chars : array char of boolean var pic : int := Pic.FileNew ("F:\\images2.jpg") var backGround : int := Pic.FileNew ("F:\\back.jpg") var asteroid : int := Pic.FileNew ("F:\\Asteroid.jpg") var x, y : int var x2, y2 : int const ship := "x" colorback (black) % The values of x, y, x2, and y2 x := 10 y := 10 x2 := 100 y2 := 100 var hit : boolean := false % The procedure to play music procedure DoMusic loop Music.PlayFile ("E:\\pathetic.WAV") % This is the music file end loop end DoMusic put "INSTRUCTIONS" put "To play this game you must use the left and right arrow keys to move your space ship, and dodge the falling asteroids." put "Good Luck!" put "Please input your name: " .. get name cls proc collision (x : int, y : int, w : int, h : int, x1 : int, y1 : int, w1 : int, h1 : int) locate (1, 1) if x1 - x <= 70 and y1 - 73 - y < 65 then hit := true end if Text.Locate (1, 1) put hit end collision % The process to make the asteroids fall process downFall loop exit when hit = true randint (x2, 0, 600) % This randomizes the x position of the asteroid for decreasing i : maxy .. 0 by 20 % This command draws the asteroids Pic.Draw (asteroid, x2, i, picCopy) delay (100) cls if i <= 20 then delay (10) end if end for end loop end downFall fork downFall loop exit when hit = true %This command draws the background Pic.Draw (backGround, 0, 0, picCopy) exit when hit if backGround = 0 then put "Unable to load JPEG: ", Error.LastMsg end if % The call to the DoMusic procedure Music.PlayFile ("DoMusic") % This loop is for the controls in the game loop collision (x, y, 70, 73, x2, y2, 158, 72) exit when hit = true Input.KeyDown (chars) if chars (KEY_RIGHT_ARROW) then %If the right arrow key is pressed, then the space ship will move to the right x := x + 5 elsif chars (KEY_LEFT_ARROW) then %If the left arrow key is pressed, then the space ship will move to the left x := x - 5 end if % This error message displays if the picture is unable to load if pic = 0 then put "Unable to load JPEG: ", Error.LastMsg end if % This is the command to draw the space ship Pic.Draw (pic, x, y, picCopy) delay (10) end loop end loop It's definetly got something to do with my collision procedure, but I just can't figure out what, could you please help me? |
Author: | mirhagk [ Thu Dec 17, 2009 11:22 pm ] |
Post subject: | RE:Collsion Detection Help For My Game |
Use Math.Distance (enter two points and it returns the distance between the two of them). Your collision detection is a little unreliable |
Author: | The Lone Ranger [ Fri Dec 18, 2009 10:26 am ] |
Post subject: | Re: Collsion Detection Help For My Game |
YES! I have finally finshed my collsions! The game is not perfect, but at least I finshed on the deadline, thanks for all your help guys. |