Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #-----------------------------#
- # COLLISION DETECTION METHODS #
- #-----------------------------#
- def Shape.collide(s1,s2) # Compute the method name from shapes classes
- if s1.is_a? Proxy or s2.is_a? Proxy
- return Shape.ProxyVsShape(s1,s2) if s1.is_a? Proxy and !s2.is_a? Proxy
- return Shape.ProxyVsShape(s2,s1) if !s1.is_a? Proxy and s2.is_a? Proxy
- return Shape.ProxyVsProxy(s1,s2) if s1.is_a? Proxy and s2.is_a? Proxy
- else
- return Shape.method((s1.class.to_s+'Vs'+s2.class.to_s).to_sym).call(s1,s2)
- end
- end
- def self.CircleVsCircle(c1, c2)
- (c1.pos.x-c2.pos.x)**2 + (c1.pos.y-c2.pos.y)**2 <= (c1.r + c2.r)**2
- end
- def self.AABBVsAABB(a1, a2)
- (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
- end
- def self.CircleVsAABB(c,a)
- return Shape.AABBVsCircle(a, c)
- end
- def self.AABBVsCircle(a, c) # From Gamasutra tutorial
- dmin = 0
- for i in 0..1
- if( c[i] < a.min(i) )
- dmin += (c[i] - a.min(i))**2
- elsif( c[i] > a.max(i) )
- dmin += (c[i] - a.max(i))**2
- end
- end
- return (dmin <= c.r**2)
- end
- def self.ProxyVsShape(p, s)
- p.shapes.each { |sh| return true if Shape.collide(sh,s) }
- false
- end
- def self.ProxyVsProxy(p1,p2)
- p1.shapes.each do |sh1|
- p2.shapes.each do |sh2|
- return true if Shape.collide(sh1,sh2)
- end
- end
- false
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement