View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

2 replies to this topic

### #1slacktivist  Members

Posted 10 November 2008 - 06:38 PM

I am sure this is a beginner problem I have, so I decided to post here, if I need to move I will. ********************************************************** Hey guys, I have a school problem that I need help with. I am not going to get on here and ask you for the answer, instead I am going to ask you to help walk me down the right path. I have researched the problem and potential answers but can't seem to figure out how to apply it, this is where I need your help. It is a three part question, if ANYONE can help with ANY of it, I would be very grateful. *** Questions I need help with. 1. A function that calculates the distance between two(2D) points Answer - I was able to figure this one out on my own, it is the three below this one I need help with 2. A function that calculates the angle between two(2D) vectors The formula that I think would work is
float angle = (float)Math.Acos(Vector3.Dot(VectorA, VectorB));
, the problem I am running into is figuring out first if this is the formula and second what needs to be substituted. 3. A function that tells if a point(2D) is within a circle Again, I believe that the forumula is
double x = Math.Cos(theta) * width * length;
double y = Math.Sin(theta) * height * length;

4. A function that tells if a point(2D) is visible, given a view location and direction ??? I am so very lost on this one, and after searching for any help for about 4 hours... I am getting a bit frustrated with this, (to say the least!!) **************************** My code as of late. ****************************
using System;

public class Simple
{
static void Main(string[] args)
{
// test our Normalize function
float nX = 0;
float nY = 0;
Normalize(1, 1, ref nX, ref nY);
Console.WriteLine("Normalizing the vector ( 1, 1 )\nresult - ( " + nX + ", " + nY + " )\n");

// test the GetDistFromRay function
float dist = GetDistFromRay(0, 0, 1, 0, 5, 3);
Console.WriteLine("Finding the smallest distance between a line rayPoint(0, 0), rayDirection(1, 0) and a point (5, 3)\nresult - " + dist + "\n");

// test the RayIntersectsCircle function
Console.WriteLine("Does the ray from above intersect a circle with a radius of 4 at the same spot as the point?");
if (RayIntersectsCircle(0, 0, 1, 0, 5, 3, 4))
Console.WriteLine("result - true\n");
else
Console.WriteLine("result - false\n");

// test the distance between two points function
dist = DistanceFormula(1, 1, 1, 8);
Console.WriteLine("Distance between the two points ( 1, 1 ) is: " + dist);
}

////////////////////////////////////////////

static bool RayIntersectsCircle(float rayX, float rayY, float rayDirX, float rayDirY, float circleX, float circleY, float radius)
{
// find the distance of the circle from the ray
float dist = GetDistFromRay(rayX, rayY, rayDirX, rayDirY, circleX, circleY);

// and compare that distance to the radius
return (true);
else
return (false);
}

////////////////////////////////////////////

static float GetDistFromRay(float rayX, float rayY, float rayDirX, float rayDirY, float ptX, float ptY)
{
// get the vector from the raypoint to the position of the point
float tX = ptX - rayX;
float tY = ptY - rayY;
// get the dot product of the ray direction and our temporary vector
float dot = tX * rayDirX + tY * rayDirY;
// get the point on the line
float lineX = rayX + (rayDirX * dot);
float lineY = rayY + (rayDirY * dot);

// set up variables for the distance test
float dx = lineX - ptX;
float dy = lineY - ptY;

// return the distance
return ((float)Math.Sqrt(dx * dx + dy * dy));
}

////////////////////////////////////////////

static void Normalize(float x, float y, ref float outX, ref float outY)
{
// first find the length of the vector, which is essentially the distance between( x, y ) and ( 0, 0 )
float length = (float)Math.Sqrt(x * x + y * y);

// divide x and y by the length, so that the vector will have a length of 1, and then store the result in the out variables
outX = x / length;
outY = y / length;
}

//////////////////////////////////////////////

static float DistanceFormula(float x1, float y1, float x2, float y2)
{
float dx = x2 - x1;
float dy = y2 - y1;
dx = dx * dx;
dy = dy * dy;

float dist = (float)Math.Sqrt(dx + dy);

return dist;
}

}

I am a graphic design major and would be more then willing to help you any and all questions and would be willing to design something for you in return for your time and help. Thank you for your help!

### #2Mihai Moldovan  Members

Posted 30 April 2012 - 07:03 PM

4. Are you asking if the point is on the half-segment represented by the eye position and the view vector?

float d = dot(view, point - eye);

if (d < 0 && std::abs(d) - view.length() * (point - eye).length() < epsilon)
{
// visible!
}


### #3Stackout  Members

Posted 18 May 2012 - 05:56 AM

The distance between two Vector3d you could use the Euclidian Distance Formula in 3d space.

Try this simple line of code:

public float DistanceBetweenVectors(Vector3 v1, Vector3 v2)
{
var distance = Math.pow((v1.x - v2.x), 2) + Math.pow((v1.y - v2.y), 2) + Math.pow((v1.z - v2.z), 2);
return (float)Math.sqrt(distance);
}

static void Main(string[] args)
{

Console.WriteLine("Distance between two v1 (2, 3, 2) and v2 (3, 3, 3) is: {0:F2}", DistanceBetweenVectors(new Vector3(2, 3, 2), new Vector3(3, 3, 3)));

}


You can also put a Vector2 in the distance formula, just create an overload for the same function and just take out the (v1.z - v2.z) and you will get your distance between two vectors.

Cheers,
Stackout

Edited by Stackout, 18 May 2012 - 09:48 PM.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.