How to check what object have you collide?

maria_tachi
01-18-2006, 12:13 AM
Hi guys,

Currently I using bounding box collision detection to make an animal eat some food. How can I use collision detection to differentiate what food I eat? For instance, I have two food grass and milk. I want the animal to know that it is some grass or a bowl of milk. Hence, how do I go about checking what food is it?

I remember in C++ there is this function where you list down the object that you going to collide and check whether is it the one that you want. Hence, how does Vb.net works?

--Maria--

unrealportal
01-18-2006, 05:10 AM
It depends entirely on how you implemented your objects. Probably the easiest way is to put a function into the food objects that returns some value indicating what type of an object it is. When your code detects a collision, you already know what object you are colliding with, then access the object's function to obtain information about what type of food it is.


Public Inheritable Class GenericFood
Public Energy As Single

Sub New()
Energy = 1 ' Some default value
End Sub

Public Overridable Function GetFoodType() As String
Return "Generic"
End Function
End Class

Public Class GrassFood()
Sub New()
Energy = 5.6
End Sub

Public Overrides Function GetFoodType() As String
Return "Grass"
End Function
End Class

Public Class MilkFood()
Sub New()
Energy = 0.3
End Sub

Public Overrides Function GetFoodType() As String
Return "Milk"
End Function
End Class

Public Inheritable Class MyAnimal
' This function is called when the animal touches some food
Public Sub CollideFood(ByRef foodObject As GenericFood)
If (foodObject.GetFoodType() = "Grass" Then
System.Console.WriteLine("Crunch, crunch, crunch...")
End If
End Sub
End Class


Now this was a silly example, but I hope it can help you. It would be better if you could've posted some of your code here. There is no how does VB.Net do this, it's up to the programmer!

maria_tachi
01-19-2006, 08:49 PM
It depends entirely on how you implemented your objects. Probably the easiest way is to put a function into the food objects that returns some value indicating what type of an object it is. When your code detects a collision, you already know what object you are colliding with, then access the object's function to obtain information about what type of food it is.


Public Inheritable Class GenericFood
Public Energy As Single

Sub New()
Energy = 1 ' Some default value
End Sub

Public Overridable Function GetFoodType() As String
Return "Generic"
End Function
End Class

Public Class GrassFood()
Sub New()
Energy = 5.6
End Sub

Public Overrides Function GetFoodType() As String
Return "Grass"
End Function
End Class

Public Class MilkFood()
Sub New()
Energy = 0.3
End Sub

Public Overrides Function GetFoodType() As String
Return "Milk"
End Function
End Class

Public Inheritable Class MyAnimal
' This function is called when the animal touches some food
Public Sub CollideFood(ByRef foodObject As GenericFood)
If (foodObject.GetFoodType() = "Grass" Then
System.Console.WriteLine("Crunch, crunch, crunch...")
End If
End Sub
End Class


Now this was a silly example, but I hope it can help you. It would be better if you could've posted some of your code here. There is no how does VB.Net do this, it's up to the programmer!

Actually I havent start coding on it yet :P just want to know how to detect different object when collide :) Anyway, thanks for your example

Iceplug
01-20-2006, 04:22 AM
In general, you can do it with any structure or class that you make that has a rectangle and an additional property that tells what the object is.
Therefore, you just go through all of your objects, check if their rectangle collides with yours and, if it does, you check the same object's additional property telling what the object is.
You may also want to consider Enumerations. They are much more efficient than passing strings around throughout your application. Strings should only be used for something that will be written to the screen and hold actual text.

gjconely
03-13-2006, 09:15 AM
Depending on the size of your "map", you may want to have a list of objects on the screen (for multiple reasons). You can then just loop through this list of items instead of all objects. This could save you some much needed CPU cycles in your program.

I would also use this list to control what I rendered or not. I wouldn't do just screen, but maybe a 3x3 screen area so that you can pad\buffer it a little.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum