View Single Post
Old 06-28-2009, 06:22 PM   #1 (permalink)
JulianDave
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]