- Home /
Using a screen-drawn line to check for a collision [Vectrosity]
So, I have been learning some of the basics of programming for the Android, and one of the things I'm trying to find out now is how to check and see if a "drawn GUI line" (Vectrosity Line) has overlapped a 3D object and if so to count it as a collision. I also want to let the 3D object to be notified of this (Like a SendMessage)
Edit: I am now using Vectrosity to draw my lines, which is a BIG help, but now I need to use check and see if a line was drawn over a particular object on screen
Also, i'm not trying to cut anything or looking for a complex collision, i just want to see if the line overlaps a certain object.
Before I write a lengthy answer that doesn't answer your question: with a line 'over' a 3d object do you mean that the line intersects or that the line is 'above' the object?
Like the line is traced on the object like in the diagram.
What exactly are you doing? Do you have basic shapes (sphere, box) that you just want to know were intersected by the line and you don't care where? Or do you have complex geometry, where you need to know exactly where the cut is? Or something else? 2D or 3D?
Answer by Cyb3rManiak · Apr 21, 2011 at 09:10 AM
Well, as I see it you don't really have to calculate if the line intersected the objects. Your input is the touch interface. All you have to do is check if at any time did the user moved his finger over one of the objects (unless you don't want to consider a half-way cut object as a success - in which case you'll need some math).
You can just Raycast with the help of Camera.ScreenPointToRay and get the list of objects the user touched.
I don't know how worried you are with performance, but if a raycast is too expensive for some reason, you might even try and use the OnMouseOver event. It might just work.
On$$anonymous$$ouseOver is just raycasting anyway (except with GUITextures), so there is no difference.
I imagined so, but I was never sure about it :) Thanks.
I built a game with this exact mechanic just last month. This method of raycasting worked like a charm.
Additionally, it is important to note that if the player swipes quickly and great distances across the screen, there is a chance of the target object being "skipped over" because the finger might be over the collider in between raycasts. The solution for this was to insure a high frame rate.
Okay, i see what you are saying in the first part of the question. Really all I would want is a simple swipe to actually hit the object Now, Eric might understand this, but as far as I can see, the line is made up of an array of points, so what would be the simplest (as process lite) way of raycasting each point and looking for a collision of that object? Code implementation would be nice :D
I figured since I am already drawing a line, it would be best to utilize the points it generates. [For those who have vectrosity, look at the Draw Lines Touch script as a ref, that's how my line is drawn]
Answer by StephanK · Apr 21, 2011 at 08:50 AM
I don't think there is an easy solution for this, especially if your objects have a complex geometry and you need very accurate information about where the object was slashed.
However the solution might be to calculate an "on-screen-bounding-box" for your object. To do this you could transform the vertices of your object's bounding box to ScreenSpace (using Camera.WorldToScreenPoint). That leaves you with 8 points. Now find the 4 that form the bounding box (contain the other 4 points). Now you have a rectangle. Checking if your line intersects this rectangle should be straightforward maths.
Answer by Jeston · Apr 21, 2011 at 08:44 PM
I would approach this by transforming the collider of your 3d objects into screen space, and using a Line to Line collision test for each polygon in your model.
I also assume that your collision info for the line is an actual line or an array of line segments for your curvature. You can do some early logic to help the performance:ie: segment.bounds overlaps model.bounds
Answer by burgunfaust · Apr 23, 2011 at 03:16 AM
If you were to resort to onmouseover for your raycasting, why not use onmousedrag and just get the start and stop points?