Skip to content

Instantly share code, notes, and snippets.

@lambdalisue
Created August 29, 2011 04:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lambdalisue/1177770 to your computer and use it in GitHub Desktop.
Save lambdalisue/1177770 to your computer and use it in GitHub Desktop.
Collision detection of Circular sector vs Point written in CoffeeScript
#!coffee
#
# required
# - vector.coffee - gist: 1177722
#
vector = require './vector'
isCollided = (sector, point) ->
# Collision detection of circular sector and point
toRadian = (d) -> d * Math.PI / 180
# Ready to calculate
r = sector.r
r2 = Math.pow r, 2
theta = toRadian sector.theta
heading = toRadian sector.heading
# Create vector
v1 = new vector.Vector [
sector.x + r * Math.cos(heading + theta/2),
sector.y + r * Math.sin(heading + theta/2)
]
v2 = new vector.Vector [
sector.x + r * Math.cos(heading - theta/2),
sector.y + r * Math.sin(heading - theta/2)
]
p = new vector.Vector [
point.x - sector.x,
point.y - sector.y
]
# Collision conditions
c1 = v1.cross(p) <= 0 # p is on right side of v1
c2 = v2.cross(p) >= 0 # p is on left side of v2
c3 = p.square() <= r2 # |p| <= r
return c1 and c2 and c3
unittest = ->
assert = (condition, message) ->
console.log message if not condition
sector =
x: 0, y: 0
r: 10, theta: 45, heading: 90
p1 = x: 0, y: 5
p2 = x: 5, y: 0
p3 = x: -5, y: 0
assert isCollided(sector, p1), "sector and p1 is supposed to be collided"
assert not isCollided(sector, p2), "sector and p2 is not supposed to be collided"
assert not isCollided(sector, p3), "sector and p3 is not supposed to be collided"
unittest()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment