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.

DarkBASIC Professional Discussion / advanced vector problem help

Author
Message
noobnerd
15
Years of Service
User Offline
Joined: 30th Nov 2010
Location:
Posted: 19th Sep 2011 17:51
ok so what i am trying to do is get the angle at which the ai should shoot to hit a moving target.

so i have the vector a (ai bullet) which is unknown except its length, which is of course the constant bullet velocity. its starting point is also known.

then we have the vector b (moving target) which is completely known.

so how do i calculate the shortest distance where these vectors intersect?

what i have come up with :

where xdistance = x distance between target and ai
ydistance = y distance between target and ai
t = "frames" how many frames it will take
z = angle of ai vector
v1x = ai bullet xvelocity
v1y = ai bullet yvelocity
v2 = target velocity

xdist+t*v1x = t*v2*sin(z)
ydist+t*v1y = t*v2*cos(z)

this should solve it?
but i cant solve that system with any variables for the ones i will know... any help? I think i could make a program that iterates it for me... but that wouldnt be precise and neat enough )

also i hope my explanation was clear enough
noobnerd
15
Years of Service
User Offline
Joined: 30th Nov 2010
Location:
Posted: 19th Sep 2011 18:33 Edited at: 19th Sep 2011 22:37
slow program that iterates it


very slow and un accurate program. works though.
press space to test
click on mouse to change target vector
the v1# and v2# variables adjust bullet & targe speed

oh it uses the advanced2d plugin and matrix1 utils
Scraggle
Moderator
23
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 20th Sep 2011 10:44
noobnerd
15
Years of Service
User Offline
Joined: 30th Nov 2010
Location:
Posted: 20th Sep 2011 14:27
no offence, that wasnt very helpfull and if you would have read my post you would understand why...

i have googled like hell and i do know how to calculate an intersection. my problem is that i do not know what angle/slope one of the lines will have. THAT is what i want to know.
WLGfx
18
Years of Service
User Offline
Joined: 1st Nov 2007
Location: NW United Kingdom
Posted: 20th Sep 2011 16:20 Edited at: 20th Sep 2011 16:29


The above code returns a true if two lines intersect, you can use the x# and y# values to find the actual intersection coordinates.

If you have your original x, y, angle and distance then you can easily calculate the new coords like this:



Having coords with just a distance is no use, but you can find the angle if you have two sets of coords:



Using it that way means using Cos() on the x coords and the Sin() on the Y coords.

Warning! May contain Nuts!
noobnerd
15
Years of Service
User Offline
Joined: 30th Nov 2010
Location:
Posted: 20th Sep 2011 20:22
okay nice code there feeling a litle noobish now

but how does it help me? im not that good at math ( im just 17 ) so i dont really see how that helps me

could you explain how to implement that code to my problem ? thanks
WLGfx
18
Years of Service
User Offline
Joined: 1st Nov 2007
Location: NW United Kingdom
Posted: 20th Sep 2011 23:15
The first function tests if two lines intersect and returns true (or 1) if so. If you actually want the coords of were the intersection takes place then you will have to butcher the code and save the x# and y# from within the function. (probably store them in temporary global variables)

The other two snippets are just helpers.

I think what you are after (I hope) is to get the XY positions for each trajectory, for the start and end of the lines, and then check for the intersection (collision).

That would be 4 sets of coordinates.

Line 1 (bullet) x1,y1 to x2,y2
Line 2 (moving target) x3,y3 to x4,y4

I started off learning to program by copying programs from a magazine in the 80's.

Warning! May contain Nuts!
Neuro Fuzzy
19
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 21st Sep 2011 01:21 Edited at: 21st Sep 2011 01:32
^hey, I'm about that old too! xD
Anyways, I don't think that line-line intersection is the way to go, but instead to use simple dead reckoning.

So, you have your player's direction vector (X), your player's velocity (v1), your player's position (XPos), your AI's direction vector (Y), your AI's position vector (YPos), and your bullets velocity (v2).

v1 and v2 are scalars, X, P, and Y are vectors.

So, we're using simple dead reckoning, which basically means that we're going to assume that, in t amount of time, the player's position will be:
NewPlayerPos=t*v1*X+P

if you're not familiar with vector math, that means that:
NewPlayerPos.x=t*v1*X.x+P.x
NewPlayerPos.y=t*v1*X.y+P.y
NewPlayerPos.z=t*v1*X.z+P.z

[without the z vector if you're working in 2d]

To explain what's going on here, multiplying time by velocity (t*v1) gives us distance. Multiplying a unit vector (length 1) by a distance, gives us a vector with a length t*v1, and then we add that to our position.

So, in a given amount of time:
NewPlayerPos=t*v1*X+XPos

The problem is that the bullet is also moving.
NewBulletPos=t*v2*Y+YPos
(the bullet is fired from position ypos in direction Y, at speed v2, travelling for t time)

Now, since NewPlayerPos=NewBulletPos when the two things collide, we say:
t*v1*X+XPos=t*v2*Y+YPos
Now... we need to solve for X (the direction in which the bullet was fired), and we need to get rid of t - it's just an intermediate variable. The problem is that solving for X is not as simple as it might sound.

we can use multiplication and addition to simplify the equation to this:
t*v1*X-t*v2*Y=Ypos-Xpos
but (v1*X-v2*Y) is a vector quantity. We can't just take out the t and divide by a vector. Since Y is what we want to solve for, we can do this:
t*v2*Y=t*v1*X-Ypos+Xpos
Now, we need to get rid of t, and most other ways of solving for t give us t as a function of Y, but we need to solve for both! What I did was to take the length of both sides. If you weren't aware, the length of a vector <a,b,c> is sqrt(a^2+b^2+c^2). We're also going to square both sides to get rid of the square root.

t^2*v2^2*(Y.x^2+Y.y^2+Y.z^2)=...
(I didn't find the length of the right side of the equation yet, for reasons i'll explain)
Now, we already stated that Y has direction only, meaning it has a length of one! That means that the left side of our equation simplifies to
t^2*v2^2=...

Now the right side of the equation... It's a big term. The vector on the right side is the length squared of t*v1*X-Ypos+Xpos. thats:

We have to square that and add it all up, which will be a pain in the arse. We can simplify it a bit, and create another vector.

Q=Xpos-Ypos

So... ugh, here we go:
t^2*v2^2=(t*v1*X.x+Q.x)^2+(t*v1*X.y+Q.y)^2+(t*v1*X.z+Q.z)^2
=t^2*v1^2*X.x^2 + 2*t*v1*X.x*Q.x + Q.x^2
+t^2*v1^2*X.y^2 + 2*t*v1*X.y*Q.y + Q.y^2
+t^2*v1^2*X.z^2 + 2*t*v1*X.z*Q.z + Q.z^2

well, if we look closely enough, we can simplify that a bit:

t^2*v2^2*(X.x^2+X.y^2+X.z^2)
+ 2*t*v1*(X.x*Q.x+X.y*Q.y+X.z*Q.z)
+ Q.x^2+Q.y^2+Q.z^2

Now, X is also a unit vector, so that coefficient of t^2*v1^2 is equal to one, and it disappears!
Also, we can use some shorthand notation. First of all, the length of a vector V is symbolized as ||V||, so that last term is ||Q||^2. The second term, you'll find a massive pain if you haven't already studied dot product. Basically, in vector math, the dot product of two vectors is as follows dot(<a1,a2,a3>,<b1,b2,b3>=a1*b1+a2*b2+a3*b3. It can also be represented as A.B. So, our equation is now:

t^2*v2^2=t^2*v1^2 + 2*t*v1*X.Q + ||Q||^2
So,

t^2*(v2^2-v1^2)-t*(2*v1*X.Q)-||Q||^2=0

look familiar? It's a quadratic equation!
We can finally solve for t!

t=(2*v1*X.Q +- sqrt(4*v1^2*(X.Q)^2+4*(v2^2-v1^2)*||Q||^4))/(2*(v2^2-v1^2))
which we can simplify a little:

t=(v1*X.Q +- sqrt(v1^2*(X.Q)^2+(v2^2-v1^2)*||Q||^4))/(v2^2-v1^2)

okay, so we have TWO values for t. I'm not sure which to use. You could probably prove that one value of t is always positive and the other is always negative, and then discard the negative value (One solution going back in time, one solution going forward in time), but I don't want to do anything with that giant equation except plug numbers in. My advice: Calculate both values of t, choose the lowest nonnegative one.




Now, of course, solving the vector equation is trivial!
t*v1*X+XPos=t*v2*Y+YPos
Y=(t*v1*X+XPos-YPos)/(t*v2)

and voila. Shoot your bullet in the direction of y.



in summation:
Q=Xpos-Ypos
t=(v1*X.Q +- sqrt(v1^2*(X.Q)^2+(v2^2-v1^2)*||Q||^4))/(v2^2-v1^2)
Y=(t*v1*X+Q)/(t*v2)

and y is your answer.

code would look painful:


[edit]
and if I made a mistake: God help us all.

[edit2]
and the massive post is mostly just because I'm explaining every step I make. If you were already familiar with dot products and direction vectors and vector equations I might not have, but oh well.

It's also worth noting that, because you can have a vector with any number of dimensions (<4,5,1,6,3,5,4> is a valid vector), the above solution works in 1d, 2d, 3d, 4d, 5d, etc. Just increase/decrease the number of arguments, and the calculations of dot product and length.


Why does blue text appear every time you are near?
Neuro Fuzzy
19
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 21st Sep 2011 03:19
If what I said didn't work, or if you can't wrap your head around it, then try an estimation. Take the distance between your Target and your Shooter, and divide it by the bullet velocity to get time. Then move your Target forward by the calculated time*velocity, point your Shooter object at this new position, then move your Target backwards by time*velocity so it's at its starting position. Now your Target will be pointing in approximately the right direction.

"dead reckoning" like this might even be better, because it's a bit faster to calculate, and who needs exact solutions anyways?


Why does blue text appear every time you are near?
noobnerd
15
Years of Service
User Offline
Joined: 30th Nov 2010
Location:
Posted: 21st Sep 2011 14:03
@WLGfx :
Hmm i dont think that was what i meant but i tested your code and it is really helpfull, if not for this particular problem

@neuro fuzzy :
Thank you! It took me a while but i actually did understand your code! Excactly what i needed
Neuro Fuzzy
19
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 21st Sep 2011 22:14
Just make sure to tell me if it works! xD


Why does blue text appear every time you are near?
noobnerd
15
Years of Service
User Offline
Joined: 30th Nov 2010
Location:
Posted: 21st Sep 2011 22:52 Edited at: 3rd Oct 2011 11:04
IT did! i modified the code a bit though.. here are the functions i made

It is a little shorter than yours (might be because of the stacking though )
it also is light on vectors which suits be better
but the idea is the same as in yours


x1#,y1# = target coordinates
xv#,yv# = target x and y velocity
x2#,y2# = gun position
v# = bullet velocity

call the function
then the cx# = ....
cx#,cy# is the coordinates for the collision ( the point where u want to aim )

thx again

Login to post a reply

Server time is: 2026-07-10 20:58:26
Your offset time is: 2026-07-10 20:58:26