Advertisement
Guest User

Trebor777

a guest
Nov 16th, 2009
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.45 KB | None | 0 0
  1. #-----------------------------#  
  2. # COLLISION DETECTION METHODS #
  3. #-----------------------------#
  4.   def Shape.collide(s1,s2) # Compute the method name from shapes classes
  5.     if s1.is_a? Proxy or s2.is_a? Proxy
  6.       return Shape.ProxyVsShape(s1,s2) if s1.is_a? Proxy and !s2.is_a? Proxy
  7.       return Shape.ProxyVsShape(s2,s1) if !s1.is_a? Proxy and s2.is_a? Proxy
  8.       return Shape.ProxyVsProxy(s1,s2) if s1.is_a? Proxy and s2.is_a? Proxy
  9.     else
  10.       return Shape.method((s1.class.to_s+'Vs'+s2.class.to_s).to_sym).call(s1,s2)
  11.     end
  12.   end
  13.  
  14.   def self.CircleVsCircle(c1, c2)
  15.    (c1.pos.x-c2.pos.x)**2 + (c1.pos.y-c2.pos.y)**2 <= (c1.r + c2.r)**2
  16.   end
  17.  
  18.   def self.AABBVsAABB(a1, a2)
  19.     (a1.pos.x - a2.pos.x)**2 <= (a1.xw + a2.xw)**2 and (a1.pos.y - a2.pos.y)**2 <= (a1.yw + a2.yw)**2
  20.   end
  21.  
  22.   def self.CircleVsAABB(c,a)
  23.     return Shape.AABBVsCircle(a, c)
  24.   end
  25.   def self.AABBVsCircle(a, c) # From Gamasutra tutorial
  26.     dmin = 0
  27.     for i in 0..1
  28.       if( c[i] < a.min(i) )
  29.         dmin += (c[i] - a.min(i))**2
  30.       elsif( c[i] > a.max(i) )
  31.         dmin += (c[i] - a.max(i))**2
  32.       end
  33.     end
  34.     return (dmin <= c.r**2)
  35.   end
  36.  
  37.   def self.ProxyVsShape(p, s)
  38.     p.shapes.each { |sh| return true if Shape.collide(sh,s) }
  39.     false
  40.   end
  41.  
  42.   def self.ProxyVsProxy(p1,p2)
  43.     p1.shapes.each do |sh1|
  44.       p2.shapes.each do |sh2|
  45.         return true if Shape.collide(sh1,sh2)
  46.       end
  47.     end
  48.     false
  49.   end
  50. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement