Old 06-28-2009, 06:22 PM   #1 (permalink)
Jr. Zuner
 
Join Date: Sep 2008
Location: Julian, CA
Posts: 47
JulianDave is on a distinguished road
Default A Simple Collision Method

I don't know if this has been posted before, but this is an easy way to do collisions. It will only work in some applications, but it's a good thing to file away until needed. You just calculate the distance between the player and all of the other objects it could collide with to see if they get too close. I usually pick a point that is the center of the object. Then compare distances to see if anything gets within a set distance. Just play with this value to get something realistic. This won't work, for example, if the objects very in size, unless you add a little more complexity by keeping a custom distance for each object in an array. Performance wise, I don't know how bad this is, but it's fairly straight forward.

distance = Math.Sqrt(Math.Pow(PlayerPosition.X - AlienPosition[i].X, 2) + Math.Pow(PlayerPosition.Y - AlienPosition[i].Y, 2));

I am using this in a newer version of aMAZEing that I haven't released yet and it works great.

You can also use this to vary how objects move in relation to the player. For example, a "ball of death" that is attracted to a player picks random directions to move and compares the distance to the player vs. it's current distance. It then chooses a direction that gets it closer to the player.

Dave
__________________



JulianDave is offline   Reply With Quote

Advertisement [Remove Advertisement]
Old 06-28-2009, 07:58 PM   #2 (permalink)
lost in paradise with rae
Support Team
Moderator
Expert Zuner
 
Red Sky's Avatar
 
Join Date: Nov 2007
Location: Pennsylvania
Posts: 2,660
Red Sky is just really niceRed Sky is just really niceRed Sky is just really nice
Send a message via MSN to Red Sky
Default

Based off of what you are saying, it would seem that a distance equal to half the width of the 'player' would be a good 'set distance', since you are going from the center of the 'player'. Also, your 'ball of death' is sort of like how the red ghost in pacman works. He always aims for 2 spaces ahead of pacman, and that dictates his movement.





Red Sky is offline   Reply With Quote
Old 06-28-2009, 08:42 PM   #3 (permalink)
Jr. Zuner
 
Join Date: Sep 2008
Location: Julian, CA
Posts: 47
JulianDave is on a distinguished road
Default

Quote:
Originally Posted by Red Sky View Post
Based off of what you are saying, it would seem that a distance equal to half the width of the 'player' would be a good 'set distance', since you are going from the center of the 'player'.
You have to add half the width of the player with half the width of the object. They would then be just touching. Now that I think about it, this could be done by adding .Height or .Width after the sprite texture to get the specifics of each object, without an array that keeps track of that.

Dave
__________________



JulianDave is offline   Reply With Quote
Old 06-28-2009, 08:46 PM   #4 (permalink)
lost in paradise with rae
Support Team
Moderator
Expert Zuner
 
Red Sky's Avatar
 
Join Date: Nov 2007
Location: Pennsylvania
Posts: 2,660
Red Sky is just really niceRed Sky is just really niceRed Sky is just really nice
Send a message via MSN to Red Sky
Default

Yep. Yay for Texture2Ds built in properties.





Red Sky is offline   Reply With Quote
Old 06-28-2009, 10:43 PM   #5 (permalink)
zB Programmer
Member
 
DiNoGames's Avatar
 
Join Date: May 2008
Location: Bremen, germany
Posts: 578
DiNoGames is a name known to allDiNoGames is a name known to allDiNoGames is a name known to allDiNoGames is a name known to all
Send a message via Skype™ to DiNoGames
Default

Quote:
Originally Posted by JulianDave View Post
distance = Math.Sqrt(Math.Pow(PlayerPosition.X - AlienPosition[i].X, 2) + Math.Pow(PlayerPosition.Y - AlienPosition[i].Y, 2));

There are built-in functions to calculate distances of several types like:

- float MathHelper.Distance(float, float) (to get the non negative distance between two values)
- several overloads of Vector2.Distance (to get the distance between two 2D positions)
- several overloads of Vector3.Distance (to get the distance between two 3D positions)

You can also use BoundingSpheres for the purpose you are explaining here. BoundingSpheres and BoundingBoxes have built-in collision detection routines.

So your calculation is a good start, but XNA has all this stuff built in. You don't have to reinvent the wheel
__________________

Valgard's Fate Blog







DiNoGames is offline   Reply With Quote
Old 06-29-2009, 08:20 AM   #6 (permalink)
Jr. Zuner
 
Join Date: Sep 2008
Location: Julian, CA
Posts: 47
JulianDave is on a distinguished road
Default

Thank you for posting that. I'll have to get a little more familiar with all of the built-in functions.
Dave
__________________



JulianDave is offline   Reply With Quote
Old 06-29-2009, 10:17 AM   #7 (permalink)
Purger of Ignorance
zB Programmer
Retired Staff
Expert Zuner
 
Netrix's Avatar
 
Join Date: Jun 2008
Location: In my own world
Posts: 2,804
Netrix is a name known to allNetrix is a name known to allNetrix is a name known to allNetrix is a name known to allNetrix is a name known to all
Send a message via MSN to Netrix
Default

You could also create Rectangles that have the same positions and sizes as the relevant textures and use the built-in function to determine if they collide or not. Though, this is limited in that it only works for rectangles.

Code:
Rectangle player, thing;
 
//Initalize the positions and sizes.
 
Boolean collides = player.Intersects(thing);
__________________
"Against logic there is no armor like ignorance." - Laurence J. Peter

Solitaire for your Zune! http://www.zuneboards.com/forums/dow...ne-v2-0-a.html

Zune Book Reader! http://www.zuneboards.com/forums/app...ew-thread.html




Netrix is offline   Reply With Quote
Reply

Bookmarks

Thread Tools