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 / Creating a turret that leads a moving enemy

Author
Message
Image All
20
Years of Service
User Offline
Joined: 30th Dec 2005
Location: Home
Posted: 28th Mar 2012 22:09 Edited at: 28th Mar 2012 22:09
I have finally figured out the algorithm I need to make a turret accurately shoot a moving enemy by leading him off. I just can't figure out the equations to make it work.

If I have a stationary turret, and a moving enemy, then I know some things: 1) the speed of my turret's bullets, 2) the position, speed and direction of the enemy.

Given these values, I can figure out where the enemy will be at a given time, and also how far out my bullets could have traveled at that same time. I don't know the angle that I need to fire at yet, so I'll treat the bullet's distance as a radius of the turret's range.

What I need to figure out is at what time the distance between the enemy and turret equals the distance traveled by the bullet. If I can get that time, then I can calculate the future position of the enemy, and from there I can get the angle I need to shoot at to hit it.

But the equation is a little difficult for me. I have figured out what it is, but I can't solve for time. Can someone help?



Image All
20
Years of Service
User Offline
Joined: 30th Dec 2005
Location: Home
Posted: 28th Mar 2012 22:11
By the way, this is not homework.

Sergey K
22
Years of Service
User Offline
Joined: 4th Jan 2004
Location:
Posted: 28th Mar 2012 23:32 Edited at: 28th Mar 2012 23:34
aint that hard.. and you dont need all of that calculations and math..

the best way and the easy way to make a shot someone who moves is to move the guy few frames forward without rendering it, save the new parameters and move it right back where it was..
thats the best and the easy way.. i hope THIS method opened few new possibilities for you..

more 3d models .x/.obj and more foramts here:
[href]https://www.turbosquid.com/Search/Index.cfm?keyword=gogetax1&x=0&y=0[href]
WLGfx
18
Years of Service
User Offline
Joined: 1st Nov 2007
Location: NW United Kingdom
Posted: 28th Mar 2012 23:55 Edited at: 28th Mar 2012 23:56
Hey Image All, I understand kinda what you're after. And every time I get to playing with that bit of math I keep forgetting it. Just one thing I keep remembering is to subtract one angle from the other, if it's positive then it's in the top 180 degrees, else it's in the lower 180 degrees. Kinda lets you know whether you're left or right...

I'm just at that point now so I'm hoping I'm right here too. I'll keep an eye. (must be old age kicking in as usual)

Mental arithmetic? Me? (That's for computers) I can't subtract a fart from a plate of beans!
Warning! May contain Nuts!
Image All
20
Years of Service
User Offline
Joined: 30th Dec 2005
Location: Home
Posted: 29th Mar 2012 06:59
Sergey K, I appreciate your efforts but that seems to completely miss the point. What data would that give me, other than future coordinates of the enemy? I have that data already.

WLGfx, you could also use wrapangle() and not have to worry about signage

Latch
19
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 30th Mar 2012 00:31 Edited at: 30th Mar 2012 00:48
@Image All
Hello,

Sergey has the right idea with the easy way. You find the position a few frames ahead - move the enemy and move him back then point the turret at that position and shoot.

If you want a more mathy way without too much overhead:

I think the base angle to start with is the slope between the enemy speed and the projectile speed. This will give you an angle that will always be true when the enemy moves at 90 degrees to the turret. Add the slope to the angle between the turret and the enemy and that is the predicted angle.

So, if the enemy moves at a speed of 5 and the projectile moves at a speed of 20 then the angle to lead the enemy when the enemy is moving at 90 degrees to the turret is

5/20 = atan(.25) = 14.04 degrees

This will be true no matter what distance the enemy is at.

What if the enemy isn't going straight sideways but stepping backward or stepping forward at some other angle to the turret?

If the enemy is stepping backwards or forwards, the distance is changing and the angle is changing so the slope won't be the same.

Subtract the angle between the predicted enemy position and the enemy actual position from the lead angle.

Here's the basics:



Enjoy your day.
Image All
20
Years of Service
User Offline
Joined: 30th Dec 2005
Location: Home
Posted: 30th Mar 2012 07:13
Quote: "Sergey has the right idea with the easy way. You find the position a few frames ahead - move the enemy and move him back then point the turret at that position and shoot."

Without knowing how many frames ahead to shoot for, that's useless, and that's what my final unsolved equation would yield. Plus I don't need to move the enemy to find future positions when I can simply calculate that.

However, as I currently do not have DBP installed I can't test your code right now but I will probably be able to this weekend. What I've been attempting to do is to get a 100% guarantee for a hit on an enemy moving in a straight path. Does your code yield 100% accuracy?

MadBit
VIP Member
Gold Codemaster
17
Years of Service
User Offline
Joined: 25th Jun 2009
Location: Germany
Posted: 30th Mar 2012 14:28
i think this can help you. http://www.gamedev.net/topic/122528-calculating-intercept-angle/page__view__findpost__p__1669266

With Computers you can solve Problems that you have never befor.
Share your knowledge. It's a way to achieve immortality. (Tenzin Gyatso)
Diggsey
20
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 30th Mar 2012 17:22 Edited at: 30th Mar 2012 17:26
You can treat time as an extra dimension. The possible trajectories of a projectile make a cone, and the path of the target makes a 3d line. Find the intersections between the cone and the line and you know the angle to shoot at.

[b]
Neuro Fuzzy
19
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 30th Mar 2012 22:24
Aha! Diggsey's method works great.


You have:
x^2+y^2=bulletvel^2*t^2 (circle radius t*bulletvel)

Pos1-Pos2-V*t=(x,y)
where pos1 is the position of the target, V is the velocity vector of the target, and Pos2 is the position of the shooter. I made this code to teset it out:


Image All
20
Years of Service
User Offline
Joined: 30th Dec 2005
Location: Home
Posted: 30th Mar 2012 22:48 Edited at: 30th Mar 2012 22:51
oh snap, Diggsey = blower of minds (wait that sounds wrong)

i will try that, if only i can remember how to find intersections between such things oh wait NF is that it?

Neuro Fuzzy
19
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 31st Mar 2012 02:00
yeah, here's a description of what has to be done:
http://mathbin.net/91634

MadBit
VIP Member
Gold Codemaster
17
Years of Service
User Offline
Joined: 25th Jun 2009
Location: Germany
Posted: 31st Mar 2012 14:45
Quote: "yeah, here's a description of what has to be done:"

I have looked at the description.
As I understand it, that will work only if the target is directly moved toward the gun. Moves the target as discribt in the above sketch, the function does not work 100% reliable.
Then add the advisement - what if the shooter is still moving.

With Computers you can solve Problems that you have never befor.
Share your knowledge. It's a way to achieve immortality. (Tenzin Gyatso)
Neuro Fuzzy
19
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 1st Apr 2012 08:55
Quote: "As I understand it, that will work only if the target is directly moved toward the gun. Moves the target as discribe in the above sketch, the function does not work 100% reliable.
Then add the advisement - what if the shooter is still moving."

See the code snippet above. The method works 100%. (although I admit I haven't taken any care for degenerate division by zero/no solution cases.

Perhaps you're confusing a vector equation with a regular one? If P and M are two-dimensional vectors, then the equation P+M=0 can be interpreted as two equations:

P.x+M.x=0
P.y+M.y=0

Actually, using vector notation you can come up with a more general solution for any number of dimensions:

http://mathbin.net/91680

MadBit
VIP Member
Gold Codemaster
17
Years of Service
User Offline
Joined: 25th Jun 2009
Location: Germany
Posted: 2nd Apr 2012 16:09
oops, sorry, I would probably be better to try out your example. Then I would have seen that it works.

With Computers you can solve Problems that you have never befor.
Share your knowledge. It's a way to achieve immortality. (Tenzin Gyatso)
Mage
Valued Member
19
Years of Service
User Offline
Joined: 3rd Feb 2007
Location:
Posted: 3rd Apr 2012 18:18 Edited at: 3rd Apr 2012 18:18
I think its good to add that precision doesn't have to be absolute here. This is one of those problems where you can cheat, skip most of the math and CPU workload, and use a "good enough" approximation.

aka - "Optimization"

ALso a lot of games will have AI shooting with some intended degree of error. This further makes absolute precision unnecessary.

Latch
19
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 3rd Apr 2012 23:03 Edited at: 3rd Apr 2012 23:08
Quote: "Quote: "Sergey has the right idea with the easy way. You find the position a few frames ahead - move the enemy and move him back then point the turret at that position and shoot.""


Quote: "Without knowing how many frames ahead to shoot for, that's useless, and that's what my final unsolved equation would yield. Plus I don't need to move the enemy to find future positions when I can simply calculate that."


I guess I was thinking simple with less math:


Should give pretty good results. Won't be 100% accurate. You may have to adjust the frame count by 1 or 2 if the projected distance the enemy moves if farther than the speed of the projectile:



And regarding the slope, this is simple indeed: if the enemy is moving at 90 degrees to the turret, the direction to fire is the slope of espeed/pspeed. You might have to cheat a little bit and move the projectile at the speed of the magnitude of the slope's vector. That will improve accuracy in this case, but depending on the size of the projectile and the enemy, you might get a collision without having to move the projectile at an adjusted rate.

Enjoy your day.

Login to post a reply

Server time is: 2026-07-10 00:43:02
Your offset time is: 2026-07-10 00:43:02