makerror
|
  |
| Joined: 29 Aug 2011 |
| Total Posts: 493 |
|
|
| 10 Jan 2014 08:11 AM |
I already made GUI collision detection and response. Right now I need to make similar ray like roblox have. Just need to change brick to GUI frames and 3D to 2D. Any ideas how to do it? I can just run loop and check if bullet is inside frame, but I don't think script performance going to be good. |
|
|
| Report Abuse |
|
|
|
| 10 Jan 2014 09:17 AM |
Checking if a bullet is inside a frame is a terrible way to do it. What if the speed that the bullet is going is faster than the frame?
You could try finding the equation of the bullet's trajectory and the equations of the sides of the frame. If the bullet's trajectory and one of the frame's equations match then you have a hit. |
|
|
| Report Abuse |
|
|
|
| 10 Jan 2014 09:29 AM |
Wikipedia "Ray casting"
Google "Ray casting algorithm"
???
|
|
|
| Report Abuse |
|
|
su8
|
  |
| Joined: 06 Mar 2009 |
| Total Posts: 6334 |
|
|
| 10 Jan 2014 10:25 AM |
you probably want line-line intersection
http://en.wikipedia.org/wiki/Line-line_intersection |
|
|
| Report Abuse |
|
|
| |
|
Oysi
|
  |
| Joined: 06 Jul 2009 |
| Total Posts: 9058 |
|
|
| 10 Jan 2014 11:19 AM |
Here's a function I made some time ago:
function lineIntersection(p, r, q, s) local t = (s.x*(p.y - q.y) - s.y*(p.x - q.x)) / (r.x*s.y - s.x*r.y) -- local u = (r.x*(p.y - q.y) - r.y*(p.x - q.x)) / (r.x*s.y - s.x*r.y)
if t >= 0 and t <= 1 then return p + r*t end end
Which is based on this:
p + t r = q + u s
t = (q - p) × s / (r × s) u = (q - p) × r / (r × s)
And it works much like Roblox's 3D raycasting, in that a line segment is a position and an offset from that position. You give it positionA, offsetA, positionB, offsetB. So if you do, for example:
lineIntersection( Vector2.new(0, 0), Vector2.new(0, 10), -- from (0, 0) to (0, 10) Vector2.new(5, 5), Vector2.new(-10, 0) -- from (5, 5) to (-5, 5) )
Then it should yield (0, 5). |
|
|
| Report Abuse |
|
|
|
| 10 Jan 2014 11:23 AM |
| ...Oysi, whats with "p + t r"? Is that "p + t * r"? |
|
|
| Report Abuse |
|
|