Posted: 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.
Sponsor Sponsor
mirhagk
Posted: 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
The Lone Ranger
Posted: 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?
mirhagk
Posted: 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
The Lone Ranger
Posted: 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?
The Lone Ranger
Posted: 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.
Tony
Posted: 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.
Posted: Tue Dec 15, 2009 9:32 pm Post subject: Re: Collsion Detection Help For My Game
Yes... So can you help?
Sponsor Sponsor
mirhagk
Posted: 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
but basically you use Math.Distance which returns the distance between two points which you specify. For example:
Turing:
ifMath.Distance(x1,y1,x2,y2)<5then put"THEY HAVE COLLIDED" endif
if two points, (x1,y1) and (x2,y2) are within 5 pixels then they collide.
The Lone Ranger
Posted: 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.
mirhagk
Posted: Wed Dec 16, 2009 4:15 pm Post subject: RE:Collsion Detection Help For My Game
code:
put hit
thats your problem, its going to keep telling you it's false.
that resets the text cursor to the top (so it only says false at the top)
The Lone Ranger
Posted: 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.
mirhagk
Posted: 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
Turing:
proc collision (x :int, y :int, w :int, h :int, x1 :int, y1 :int, w1 :int, h1 :int) if x+w>x1 and x<x1+w1 and y+h>y1 and y<y1+h1 then
hit:=true endif
put hit
end collision
that's rectangular collision detection, try that out in your game and see if it works.
The Lone Ranger
Posted: 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?
mirhagk
Posted: 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
Turing:
exitwhen hit
now if hit is true then it will exit, just make sure that little code is in all the loops okay.