Jump to content

Angle between 2 Vectors


Schoppy0384
 Share

Recommended Posts

Hi,

I need the Angle between a Plane and the Camera:

 


Leadwerks::Vec3 V1 = mPlane->GetPosition();
Leadwerks::Vec3 V2 = camera->GetPosition();

// turn vectors into Normals
Leadwerks::Vec3 V1n = V1.Normalize();
Leadwerks::Vec3 V2n = V2.Normalize();

//the Dot Product
float dot = V1n.Dot(V2n);

//And the Angle
float angle = Leadwerks::Math::ACos(dot);

 

But the Angle is not correct.

Link to comment
Share on other sites

The dot product ranges from -1 to 1, so I think the angle might something like this:

angle = (1.0 + a.Dot(B)) * 90.0

 

That way -1 would give an angle of zero and +1 would give an angle of 180.

 

You might have to flip the sign. +/-

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Hi,

I need the Angle between a Plane and the Camera:

 


Leadwerks::Vec3 V1 = mPlane->GetPosition();
Leadwerks::Vec3 V2 = camera->GetPosition();

// turn vectors into Normals
Leadwerks::Vec3 V1n = V1.Normalize();
Leadwerks::Vec3 V2n = V2.Normalize();

//the Dot Product
float dot = V1n.Dot(V2n);

//And the Angle
float angle = Leadwerks::Math::ACos(dot);

 

But the Angle is not correct.

 

The angle is correct, you are just feeding it the wrong values for what I assume you want to do.

This is the angle you are actually calculating:

https://docs.google.com/file/d/0B3jPhxJap2Y3QUN2aTBldWZMZGc/

Link to comment
Share on other sites

That video explains it quite nicely, as you can see, the plane is the point from which everything is calculated.

 

One correction about the dot product

a*b=abs(a)*abs(b )*cos(phi) thus acos(a*b/(abs(a)*abs(b )))=phi

(Watch for divide by zero situations!)

which is essentailly what you do already but with the wrong values.

 

As the video shows, you need the normal of that plane and the distance plane to camera.

 

That nebula effect is really cool btw.

Link to comment
Share on other sites

Ok, thanks all for help.

 

I think I have the solution:

 

//Get the Plane Normal
Surface* surface = mPlane->GetSurface(0);
Leadwerks::Vec3 V1 = surface->GetVertexNormal(1);
//Get the Differenz between Camera and Plane
Leadwerks::Vec3 V2 = camera->GetPosition() - mPlane->GetPosition();

//Normalize the Vectors
V1 = V1.Normalize();
V2 = V2.Normalize();

//The Dot Product
float dot = V1.Dot(V2);

//ABS
float dotAbsolute = Leadwerks::Math::Abs(dot);

cout << dotAbsolute << endl;

mPlane->SetColor(1, 1, 1, dotAbsolute);

  • Upvote 2
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...