Tuesday, August 11, 2009

New Hit Test

I have appended a new hit test that will help users tell the difference between size and shape, although taking only two shapes into consideration (a cube from a sphere)

Here's how the new function works:

The new function gets passed the following parameters:
  • Type of object
  • Object's world coordinates
  • Object's size (depth/width/height for cubes and radius for spheres)
  • My camera position

The function then takes the camera position and determines if it is within the given object in the world.

First it looks at the type of object; if it is a cube, it takes its world coordinates (x,y,z), which we shall call vector1 or V1, and adds the width, height and depth to them. The resulting would be a vector in the form (x,y,z), which we shall call V2. As long as my camera position, which is also in the form of a vector (x,y,z), which we shall call V0, falls in between V1 and V2, we are hitting a cube.
If V0 <= V2 and V0 >= V1 we are within that cube, so return true and send the serial trigger



If the object type is a sphere, we still set V0 to the camera's coordinates. V1 would be set to the sphere's world coordinates, which is the center of the sphere. Then we use the formula

V2 = (4/3) * pi * (radius^3)


Now taking into account these are vectors, meaning they have both a value and a direction we test to see if our position (V0) falls within +/- V2, so we have a test somewhat like this:

If V0 <= (1)*V2 and V0 >= (-1)*V2 we are within the sphere's volume, so return true and send the serial trigger
This part of the function is undergoing testing on my part, I shall keep you updated on how it works, but I hope this provides some insight

The above mentioned logic was irrational and I do apologize, a new revised hit test function for detecting a sphere has been implemented.

To do this, you have to use Processing's PVector class. Create two vectors, namely V1 and V2, and set V1 to the sphere's center, which is the sphere's coordinates. Set V2 to your camera position. PVector.dist() is method of the vector class which computes the Euclidean distance between two points. This is exactly what you need, so the syntax would be similar to:

myDist = V2.dist(V1);
if myDist<=myRadius //in this case the radius of your sphere
//you are within the sphere
trigger Serial;
Hope this information comes in handy.

No comments:

Post a Comment