Primitive Collision Detection – Circular objects

Consider the following –

If you are designing a game, and have the players shaped as circles, how would you detect bullet collisions with the players?

Well, the problem with bullet collision detection is that you can’t just use traditional collision detection algorithms with static objects. The bullet itself can be seen as a continuous object, thus we need to devise a new way to solve the problem

Cognitive process

An example implementation in python

from math import sqrt, atan, pi
class Point(object):
    def __init__(self, x = None, y = None):
        self.x = x
        self.y = y
class Vector(object):
    def __init__(self, *args):
        self.points = []
        for obj in args:
            if isinstance(obj, Point):
                self.points.append(obj)
    def dx(self):
        _dx_ = self.points[0].x
        for i in range(1, len(self.points)):
            _dx_ -= self.points[i].x
        return -_dx_
    def dy(self):
        _dy_ = self.points[0].y
        for i in range(1, len(self.points)):
            _dy_ -= self.points[i].y
        return -_dy_
    def dist(self):
        return sqrt(self.dx()**2+self.dy()**2)
    def theta(self):
        try:
            return atan(self.dy()/self.dx())
        except ZeroDivisionError:
            return 0

me = Point(0,0)
enemy = Point(1,sqrt(3))

vec = Vector(me, enemy)
a = atan(0.50/(2*vec.dist()))

print-1-+a)*360/(2*pi),-1--a)*360/(2*pi) #Theta for which you will be able to hit the enemy.

Footnotes and References

  1. vec.theta( [^] [^]
This entry was posted in Blog, Programming, Python, Snippets. Bookmark the permalink.

This website uses IntenseDebate comments, but they are not currently loaded because either your browser doesn't support JavaScript, or they didn't load fast enough.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>