What is the code used for predictive aim in the Turret behaviour

0 favourites
  • 5 posts
From the Asset Store
5 levels with simple coding Source-code (.c3p) + HTML5 Exported
  • Just a bit interested as this applies to my game and usage in other engines.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Basically you start with equations for the path the objects would go if the stayed at the same speed.

    Position = start_position + velocity*time

    That can be split into two formulas for x and y motion. And you’d also have one equation for the target and one from the bullet. Then you just solve the equations for the bullet velocity. Or just the bullet angle if you make the speed fixed. It’s just a matter of using algebra to solve it.

  • This is the code from the behavior, if you don't know anything about code, I can try to step you through what is happening:

    let targetWi = this._currentTarget.GetWorldInfo();
     let targetAngle = C3.angleTo(wi.GetX(), wi.GetY(), targetWi.GetX(), targetWi.GetY());
     if (this._predictiveAim) {
     	const Gx = wi.GetX();
     	const Gy = wi.GetY();
     	const Px = targetWi.GetX();
     	const Py = targetWi.GetY();
     	const h = C3.angleTo(Px, Py, this._oldTargetX, this._oldTargetY);
     	if (!this._firstTickWithTarget)
     		this.AddSpeed(C3.distanceTo(Px, Py, this._oldTargetX, this._oldTargetY) / dt);
     	const s = this.GetSpeed();
     	const q = Py - Gy;
     	const r = Px - Gx;
     	const w = (s * Math.sin(h) * (Gx - Px) - s * Math.cos(h) * (Gy - Py)) / this._projectileSpeed;
     	const a = Math.asin(w / Math.hypot(q, r)) - Math.atan2(q, -r) + Math.PI;
     	if (!isNaN(a))
     		targetAngle = a
     }

    worldinfo has all the important bits about an objects size, location, etc... and you can look it up in the addon sdk manual. The important math parts here are at the bottom with const s, q, r, and w. (const just means a variable declaration is happening)

    If you ever want to take a deep dive into what is happening behind the scenes, you can launch a game and press f12 to bring up the devmode for the browser. You can browse all the runtime code to your hearts content, and it is indeed a really valuable tool to begin understanding precisely what is happening, and how things work under the hood. You can learn alot, though, I would recommend a code base with comments if you are just learning coding.

  • You can also add in predicting aim for more accuracy and handle things like predicting acceleration.

    So, for example, since the turret behavior doesn't track or care about the difference over time in the delta between target positions, a constantly accelerating target will never be hit.

    Currently the behavior tracks changes in position from one frame to the last, and uses that to extrapolate where to shoot. It also accounts for its own speed I believe.

    But if you add a variable that stores one more previous position, you can find the difference in deltas between the 2 steps between the 3 positions and roughly account for any continuous acceleration.

    You can get this working with events by using a "ghost" target. Instead of tracking a target itself, you track a sprite that you then set to the position of the intended target. you then monitor the intended targets acceleration from frame to frame, and the distance to the turret. Using the same sort of math, you can place the ghostTarget ahead of the actual target and have the turret shoot at that. The turret will practically never miss except when an object is changing directions... and you can add another layer for dealing with delta differences of the differences, and so on recursively as many frames as you like. If the target takes time to accelerate and decelerate, it won't stand a chance.

  • Yo thanks, really appreciate it. Now for the final frontier for some of the stuff i wanna make

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)