Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Geek Culture / Vector Question

Author
Message
BMacZero
19
Years of Service
User Offline
Joined: 30th Dec 2005
Location: E:/ NA / USA
Posted: 7th Nov 2010 16:46 Edited at: 7th Nov 2010 16:46
I have what I hope will be a quick question about a vector problem. In the diagram below, I have the red and black vectors. I want to find the green ones.



I'm trying to do this with a bunch of trigonometry and such, and it isn't working quite right. Is there an easier way (perhaps dot products, which I've never really been able to figure out)?


Latest progress: Fog of War implemented; frustrated by pathfinding >.>

Attachments

Login to view attachments
thenerd
16
Years of Service
User Offline
Joined: 9th Mar 2009
Location: Boston, USA
Posted: 7th Nov 2010 21:52 Edited at: 7th Nov 2010 21:54
Well, I don't think you can find out those green vectors without having more information...the main ones I'd use to calculate it would be these:

-X being the angle between the red and first green vector
-Y being the intersection of the green and black vectors
-Z being the length of the second green vector
But, I may be totally wrong, I suck at trig...

Attachments

Login to view attachments
CoffeeGrunt
17
Years of Service
User Offline
Joined: 5th Oct 2007
Location: England
Posted: 7th Nov 2010 21:55
You could try looking into the Sine and Cosine rules, but I'm dubious on them working...

JLMoondog
Moderator
16
Years of Service
User Offline
Joined: 18th Jan 2009
Location: Paradox
Posted: 7th Nov 2010 21:59
Are you trying to calculate projectile/target offset based on their velocity on a vector?


...seriously, my sig was deleted because of 8 pixels? Oh well, no hard feelings to the mod who did it.
thenerd
16
Years of Service
User Offline
Joined: 9th Mar 2009
Location: Boston, USA
Posted: 7th Nov 2010 22:00 Edited at: 7th Nov 2010 22:00
Wait, is this a right angle? If so, that's easy!


Attachments

Login to view attachments
JLMoondog
Moderator
16
Years of Service
User Offline
Joined: 18th Jan 2009
Location: Paradox
Posted: 7th Nov 2010 22:01
You can't assume it's a right angle because he doesn't have the vector angle for the green line.


...seriously, my sig was deleted because of 8 pixels? Oh well, no hard feelings to the mod who did it.
thenerd
16
Years of Service
User Offline
Joined: 9th Mar 2009
Location: Boston, USA
Posted: 7th Nov 2010 22:03 Edited at: 7th Nov 2010 22:10
Well, it really depends on what he's calculating. Because without any additional information, those two green vectors could be any number of lengths...

JLMoondog
Moderator
16
Years of Service
User Offline
Joined: 18th Jan 2009
Location: Paradox
Posted: 7th Nov 2010 22:56
Ok, I'm really rusty at trig so let's break this down into steps. First thing we need to do is find the angle between The red and black vectors. Let's do that first, then finding the other vectors will be easy.

Step 1:
Let's get the vector's components.

Step 2:
Find the scalar product:

Step 3:
Get the angle where vectors A and B cross:

Step 4: Since we want an acute angle next we need to check if it's >180, if so subtract 180 from the angle.

With this info you should be able to use the scalar projection formula to get both green vectors(pretty sure, still rereading my trig book).


...seriously, my sig was deleted because of 8 pixels? Oh well, no hard feelings to the mod who did it.
Jimpo
20
Years of Service
User Offline
Joined: 9th Apr 2005
Location:
Posted: 7th Nov 2010 23:06
The smaller green vector is just the projection of the red vector onto the black one. And the larger green vector is the red vector minus the smaller green one. It's been a while since I took vector calculus, so this might be a little off; you might have to flip the vector before you do the projection, then flip the result.

Neuro Fuzzy
17
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 7th Nov 2010 23:26 Edited at: 7th Nov 2010 23:41
I don't know if what Josh Mooney said is what you're looking for, but the vector dot product is supah important and useful! I'll show you the "derivation of its importance"

Say you have a setup as illustrated in this diagram:

(a letter with an arrow over it means its a vector)

As we can see:
b.x/|b|=cos(k)
b.y/|b|=sin(k)
a.x/|a|=cos(s)
a.y/|a|=sin(s)

([/b]|n| is the length of vector n, so |n|=sqrt(n.x^2+n.y^2) )
We can also see that s-k=θ.
Since s-k=θ, cos(s-k)=cos(θ[b]
).

This is where trigonometric identities come in. If you've learned a fair bit of this in classes then this should be a cool connection. Otherwise it might be totally out of left field. Anyways, the angle sum formula (one possible derivation here) states that
cos(a-b)=cos(a)*cos(b)+sin(a)*sin(b).

Applying this to our situation:

cos(s-k)=cos(θ[/b])=cos(s)*cos(k)+sin(s)*sin(k).
As seen above, cos(s) and cos(k) are easy to find!

cos(s)*cos(k)+sin(s)*sin(k)=(b.x/|b|)*(a.x/|a|) + (b.y/|b|)*(a.y/|a|)
If you multiply both sides by |b|*|a|, and keep in mind that this is all equal to cos(θ[b]
), then:

|b|*|a|*cos(θ[/b])=a.x*b.x+a.y*b.y
!!!

The cool thing is how this can be interpreted. If you have a direction vector, A (meaning the length is 1), and another vector B. A.B=|B|*cos(θ[b]
). If you draw a diagram or picture it in your head, you can figure out that this literally means "the magnitude of B in the direction of A".



[edit]
AHhahaahh the image saved in the wrong folder so i uploaded the wrong thing >.>

1 sec!
[edit2]
thar we go. Also, thats supah annoying! θ is parsed as a character code with a ; at the end, so if you have θ and then ), that gets parsed as ;[/b]), which in turn is changed to a smiley. So θ[b]) gets parsed as θ. (in teh other ones i put [[/b]b][[b]/b] inbetween 'em)

[edit3]
and if what josh mooney said didn't solve the problem, and what I said is totally useless (), could you post a diagram *in the coordinate plain*? I'm not really sure what information you're trying to get, or what information you're given. What you're trying to do might also be useful info, just in case you're trying to do it the wrong way.

BMacZero
19
Years of Service
User Offline
Joined: 30th Dec 2005
Location: E:/ NA / USA
Posted: 8th Nov 2010 01:45 Edited at: 8th Nov 2010 01:49
I'm still reading your posts. Sorry for not giving more information, the green vectors are the components/projections of the red vector relative to the black line. Therefore thenerd is right, the green vectors are at a right angle to each other and are parallel and perpendicular to the black line.

I have a ball (its velocity is the red vector), and if its velocity is low enough going toward the line (green vector perpendicular to the black line) then I want it to simply roll along the line - rather than bouncing - at a new velocity (the green vector parallel to the black line).

I thought this would be easy enough but two sheets of paper and a few dozen lines of code later it isn't working right, so I wondered if there was an easier way.


Latest progress: Fog of War implemented; frustrated by pathfinding >.>
thenerd
16
Years of Service
User Offline
Joined: 9th Mar 2009
Location: Boston, USA
Posted: 8th Nov 2010 02:08
I was thinking of what an easy way to code this would be, as a challenge to myself I'm gonna try to write up a function. I'm not the best at trig though....

Phaelax
DBPro Master
22
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 8th Nov 2010 03:55 Edited at: 8th Nov 2010 03:57
You can find the normal of the black line very easily.

If the vector of the black line is [x,y] then its normal would be [y,-x] or [-y,x], depending on which direction you want the normal to face.

In this case, based on the image given and assuming the red and green lines start at coordinate [0,0], the vector of the first green line would be [-y, x]. Find the distance from point [0,0] (beginning of red and green lines) from the black line AB and this will give you the magnitude of the vector for that first green line.


Once you know the points along the black line where the red and green hit, then you can calculate the final green line which projects along the black line. Simply subtract the two points and you're done. If you want, you can normalize the vector and keep the direction and magnitude separate.


Here's some DBP code demonstrating the problem and solution:


My example refers to the black line as "white" for obvious visual reasons and represented by the vector components BX and BY, which is normalized.

Your signature has been erased by a mod please reduce it to 600 x 120.
AndrewT
18
Years of Service
User Offline
Joined: 11th Feb 2007
Location: MI, USA
Posted: 8th Nov 2010 04:41 Edited at: 8th Nov 2010 04:41
What Neuro Fuzzy gave you should be exactly what you're looking for. The dot product of two vectors is equal to |A||B|Cosθ, where θ is the angle between the two vectors. This value can be calculated by multiplying the corresponding components and adding them. If you take this value and divide by the magnitude of A, you get the magnitude of B that is in the direction of A, which is exactly what you want in this situation. Call the red vector A and the black vector B. Multiply their corresponding components and add them together. Then divide it by the magnitude of vector B, and you get the magnitude of the smaller green vector, and you already know it's direction because it's the same as the direction of vector B.

i like orange
BMacZero
19
Years of Service
User Offline
Joined: 30th Dec 2005
Location: E:/ NA / USA
Posted: 8th Nov 2010 08:34
Wow, thanks everyone, there are definitely a lot of ways to do this sort of thing! Neuro Fuzzy's method (along with AndrewT's nice, simple explanation) worked like a treat, nice and simple .

I'm going to come read this thread again next time I have problems with angles and vectors (which often do this too me ).


Latest progress: Fog of War implemented; frustrated by pathfinding >.>
Neuro Fuzzy
17
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 8th Nov 2010 18:18
just to clarify:

where n|| is the length of the green vector parallel to the black vector, and n (with the sign that means "perpendicular") is the length of the green vector perpendicular to the black vector.


If you want to find the actual *vectors* that are n, things are a bit different (you can subtract the vector x times one of the n from the red vector to get either n vector), but for the magnitudes of n, its prdy EZ

Login to post a reply

Server time is: 2025-05-23 10:49:08
Your offset time is: 2025-05-23 10:49:08