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

Username:   Password: 
 RegisterRegister   
 collision problem
Index -> Programming, Visual Basic and Other Basics -> Visual Basic Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Janssen




PostPosted: Fri Jun 16, 2006 9:39 am   Post subject: collision problem

Im currently creating a snake game and am having issues collecting the eggs, with the code Im having trouble setting the range that the images top and left to collide with the eggs top and left range, they both strech from its initial top and left to top+120 and left+120
code:

    If imgSnake(FrameCount).Left >= imgEgg.Left And imgSnake(FrameCount).Left + 120 <= imgEgg.Left + 120 Or imgSnake(FrameCount).Top >= imgEgg.Top And imgSnake(FrameCount).Top + 120 <= imgEgg.Top + 120 Then
        SpawnEgg
        Score = Score + 10
    End If


Nuty Eyes [/list]
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Fri Jun 16, 2006 10:01 am   Post subject: (No subject)

code:
imgSnake(FrameCount).Left >= imgEgg.Left And imgSnake(FrameCount).Left + 120 <= imgEgg.Left + 120 Or imgSnake(FrameCount).Top >= imgEgg.Top And imgSnake(FrameCount).Top + 120 <= imgEgg.Top + 120


This is just begging to be factored out into a boolean returning function that would take imgSnake and imgEgg as arguments.

Also, why is 120 significant? This is a "magic number," and it's a bad idea. Give it a name that tells people why it's important.
Janssen




PostPosted: Fri Jun 16, 2006 10:05 am   Post subject: (No subject)

Its the length of each side of both snake and egg images
wtd




PostPosted: Fri Jun 16, 2006 11:10 am   Post subject: (No subject)

Well then, let the code say that. Wink
Janssen




PostPosted: Fri Jun 16, 2006 11:35 am   Post subject: (No subject)

I tried that and it doesnt seem to work, could u please show me more of a general line of code for collisions, i think that the problem is that it needs to hit the exact left and top coords instead of range.
wtd




PostPosted: Fri Jun 16, 2006 12:10 pm   Post subject: (No subject)

Oh, I have no idea if your collision code works. I'm just trying to help you with more fundamental issues.
cool dude




PostPosted: Fri Jun 16, 2006 3:33 pm   Post subject: (No subject)

do u have a grid where it perfectly alligns? because if u don't then u will end up eating half an egg. Wink also if 120 is the length, its better to do:

code:

imgSnake(FrameCount).Left + imgSnake(FrameCount).width


This will make sure it takes the full length of the egg!

P.S. post your entire code b/c by the looks of it your if statement for collision is screwed up, and u aren't taking into account both sides. wat i mean by that is that to make sure the snake alligns u have to say if the snake hits the left side and the right side of the egg as well as the top then it eats it.
Drewbie




PostPosted: Sat Jun 17, 2006 9:19 am   Post subject: (No subject)

I think you r looking for sumthing like this

code:
If ((Image1.Left + Image1.Width) < Image4.Left + Image4.Width And (Image1.Left + Image1.Width > Image4.Left)) And (Image1.Top < (Image4.Top + Image4.Height) And Image1.Top > Image4.Top) Then
         ElseIf (Image1.Left < Image4.Left + Image4.Width And (Image1.Left > Image4.Left)) And (Image1.Top + Image1.Height > Image4.Top And Image1.Top < Image4.Top + Image4.Height) Then
        End If


where image1 is yur cnake and image4 is yur egg
u might be able to put it in on if statement but i dont no for sure
shud work
Sponsor
Sponsor
Sponsor
sponsor
Drewbie




PostPosted: Sat Jun 17, 2006 6:02 pm   Post subject: (No subject)

i have found an error in that code but corrected it

code:
If ((Image1.Left + Image1.Width) < Image4.Left + Image4.Width And (Image1.Left + Image1.Width > Image4.Left)) And (Image1.Top < (Image4.Top + Image4.Height) And Image1.Top > Image4.Top) Then

ElseIf (Image1.Left < Image4.Left + Image4.Width And (Image1.Left + Image1.Width > Image4.Left)) And (Image1.Top + Image1.Height > Image4.Top And Image1.Top < Image4.Top + Image4.Height) Then

End If


it wasnt registering the contact of the first images right side and bottom to the second images left side and top
GlobeTrotter




PostPosted: Sat Jun 17, 2006 8:23 pm   Post subject: (No subject)

Here is your code, wrapped up into a private function:

code:

Private Function isCollision(ByVal img1 As Image, ByVal img2 As Image) As Boolean
    isCollision = True Or _
        ((img1.Left + img1.Width) < img2.Left + img2.Width And _
        (img1.Left + img1.Width > img2.Left)) And _
        (img1.Top < (img2.Top + img2.Height) And _
        img1.Top > img2.Top) Or _
        (img1.Left < img2.Left + img2.Width And _
        (img1.Left + img1.Width > img2.Left)) And _
        (img1.Top + img1.Height > img2.Top And _
        img1.Top < img2.Top + img2.Height)
End Function


Now you can just call it like so:
code:

if isCollision(Image1,Image4) then
   '...
end if
Display posts from previous:   
   Index -> Programming, Visual Basic and Other Basics -> Visual Basic Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 10 Posts ]
Jump to:   


Style:  
Search: