Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Collision not working? What am i doing wrong
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Abbas_M




PostPosted: Sat Jan 16, 2010 7:07 pm   Post subject: Collision not working? What am i doing wrong

What is it you are trying to achieve?
I was really confused about collisions but now i understand them a little, im trying to make my moving spaceship hit a non moving zombie and the result
becoming an exit.


What is the problem you are having?
I have alll the code written but its not working. My space ship only move vertically (up and down) on the left side of the screen. only the y1 value changes of the space ship and nothing else. the zombie stays still. but my spaceship just passes the zombie and no collison



Describe what you have tried to solve this problem
Repeatdly read the tutorial used other ones got examples and tried changing around the values still nothing


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)


Turing:


 x1 := 15
    y1 := 220
    x2 := 100 + 15
    y2 := 100 + 220
    a1 := 15
    b1 := 400
    a2 := 65 + 15
    b2 := 65 + 400
    fork ingamemusic

    loop

        Input.KeyDown (chars)

        %Main Movement Seqeunce
        if chars (KEY_UP_ARROW) then
            y1 := y1 + 5
        end if
        if chars (KEY_DOWN_ARROW) then
            y1 := y1 - 5
        end if
       
        %ship is the variable for the image of the ship
        %picture4 is the background
        %picture2 is my zombie
        Pic.Draw (picture4, 3, -50, picMerge)
        Pic.Draw (ship, x1, y1, picMerge)
        delay (2)
        Pic.Draw (picture2, 15, 400, picMerge)

        View.UpdateArea (0, 0, maxx, maxy)
        Pic.Draw (ship, x1, y1, picMerge)

     
        if x2 > a1 and x1 < a2 and y2 > b1 and y1 < b2 then
            put "They have collided!"
            exit
        end if
        cls

        %Boundrie to Up
        if y1 > 419 then
            y1 := y1 - 10
        end if

        %Boundrie to Down
        if y1 < -15 then
            y1 := y1 + 10
        end if

    end loop




Please specify what version of Turing you are using
4.1.1

for some reason its not working. my variables are decleared and everything. no errors. when it runs it doesnt detect collide just goes through. I hope someone can help me out.
Sponsor
Sponsor
Sponsor
sponsor
TheGuardian001




PostPosted: Sat Jan 16, 2010 7:48 pm   Post subject: Re: Collision not working? What am i doing wrong

you never actually change y2 at any point in this program. The image might be moving, but as far as the collision detection is concerned it's sitting right back where it started.

Every time you change y1, you should be resetting y2 to the new location, IE

code:


y1 += 5

y2 = y1 + Pic.Width(img)


which should fix your problem
Abbas_M




PostPosted: Sat Jan 16, 2010 9:23 pm   Post subject: Re: Collision not working? What am i doing wrong

it dint work still have same problem it just passes the zombie without any detection. would it be better if i attach my program so people can take a look at the entire code?
TheGuardian001




PostPosted: Sat Jan 16, 2010 10:50 pm   Post subject: Re: Collision not working? What am i doing wrong

Probably, however upon further inspection:

code:

if x2 > a1 and x1 < a2 and y2 > b1 and y1 < b2 then
            put "They have collided!"
            exit
end if


is stating that the ship must be fully inside of the zombie. This is probably not what you want. You need to be less specific and check if any part overlaps, so for example

code:

if player_upper_y > enemy_lower_y and player_upper_y < enemy_upper_y then %top edge y axis collides
    if player_right_x > enemy_left_x and player_right_x < enemy_right_x then %right edge y axis collides
        collision! (top right corner of ship only)
    end if
end if
Abbas_M




PostPosted: Sun Jan 17, 2010 11:14 am   Post subject: Re: Collision not working? What am i doing wrong

still not working out. i tried repositiong the zombie so he would be a bit away from the ship and edges would collide but nothing. it didn't work out. Ima attach my game and you can see and tell me if something is wrong. it very cheasy but its my first ever game so...


Game.rar
 Description:
its cheasy but its my first game

Download
 Filename:  Game.rar
 Filesize:  3.66 MB
 Downloaded:  70 Time(s)

TerranceN




PostPosted: Sun Jan 17, 2010 3:01 pm   Post subject: Re: Collision not working? What am i doing wrong

From reading your updated code, I can see all you changed was moving some stuff around.

Like what TheGuardian001 said, make sure you are updating the top right co-ordinates. If you don't change them, you are not checking collision for the right co-ordinates, and the collision between the ship and alien will never be detected.

Second, read richcash's Collision Detection Tutorial, it tells you how to check for collision.

Finally, try adding this before your View.UpdateArea, it will help you visualize the problem.

Turing:
drawfilloval(x1, y1, 5, 5, blue)
drawfilloval(x2, y2, 5, 5, blue)
drawline(x1, y1, x1, y2, blue)
drawline(x1, y2, x2, y2, blue)
drawline(x2, y2, x2, y1, blue)
drawline(x2, y1, x1, y1, blue)
       
drawfilloval(a1, b1, 5, 5, green)
drawfilloval(a2, b2, 5, 5, green)
drawline(a1, b1, a1, b2, green)
drawline(a1, b2, a2, b2, green)
drawline(a2, b2, a2, b1, green)
drawline(a2, b1, a1, b1, green)


Hope that helps.
Abbas_M




PostPosted: Sun Jan 17, 2010 5:41 pm   Post subject: Re: Collision not working? What am i doing wrong

Omg thank you so much that diagram you made helped alot it explained a bunch to me and i got collision working. I do have 1 more question which would be better for my rectangular collision or circular. thanks for the help.

Edit: i tested both rectangular and circular collision detection method and circular was more accurate. in rectangular the alien and spaceship were not even touching and it said crash. circular there is physical contact before it says crash so i think il go with circular. if someone has a reason i should not please let me know. Thanks for all the help TheGuardian001 and TerranceN really appreciate it
TerranceN




PostPosted: Sun Jan 17, 2010 6:33 pm   Post subject: Re: Collision not working? What am i doing wrong

Really just use whatever collision you feel more confident using, because your game will probably be going fast enough that small imprefections in collision detection will not be noticed. And if it is a big problem just decrease the size in your collision calculations.

Also, look at how much blank space there is for your ship. If you resize the image so there is less blank space, the rectangular collision will be more precise than it was before.
Try this image:



ship2.gif
 Description:
 Filesize:  3.13 KB
 Viewed:  1945 Time(s)

ship2.gif


Sponsor
Sponsor
Sponsor
sponsor
Abbas_M




PostPosted: Sun Jan 17, 2010 6:56 pm   Post subject: RE:Collision not working? What am i doing wrong

yeah the circular collision is working pretty good right now but if it starts to act up later il switch to rectangular thanks for the help
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 9 Posts ]
Jump to:   


Style:  
Search: