Expensive fix for Cannon aiming?

Expensive fix for Cannon aiming?

Discuss the source code and development of Spring Engine in general from a technical point of view. Patches go here too.

Moderator: Moderators

Post Reply
User avatar
ILMTitan
Spring Developer
Posts: 410
Joined: 13 Nov 2004, 08:35

Expensive fix for Cannon aiming?

Post by ILMTitan »

The aiming direction of the Cannon weapon type is wrong when there is a significant y difference between the weapon and the target. I have a solution, changing this code :

Code: Select all

if(highTrajectory){
			if(predict>maxPredict)
				predict=maxPredict;
			if(predict<minPredict)
				predict=minPredict;

			float3 dif=targetPos-weaponPos;
			float k1=dif.SqLength2D();
			float h=projectileSpeed*predict;
			h=h*h;

			float k2=h-k1;
			if(k2>0){
				k2=sqrt(k2);
				k2+=weaponPos.y-targetPos.y;
				k2/=-gs->gravity*0.5f;
				if(k2>0)							//why is this needed really (i no longer understand my equations :) )
					predict=sqrt(k2);
			}
			wantedDir=targetPos-float3(0,5,0)-weaponPos;
			wantedDir.y-=predict*predict*gs->gravity*0.5f;
			wantedDir.Normalize();
		} else {
			float3 dif=targetPos-weaponPos;
			dif.y-=predict*predict*gs->gravity*0.5f;
			if (projectileSpeed == 0)
				predict = maxPredict;
			else {
				predict=dif.Length()/projectileSpeed;
				if(predict>maxPredict)
					predict=maxPredict;
			}
			wantedDir=dif;
			wantedDir.Normalize();
		}
To this:

Code: Select all

	float Dsq = diff.SqLength();
	float DFsq = diff.SqLength2D();
	float g = gs->gravity;
	float v = projectileSpeed;
	float dy = diff.y;
	float dxz = sqrt(DFsq);
	float Vxz;
	float Vy;
	if(Dsq == 0) {
		Vxz = 0;
		Vy = highTrajectory ? v : -v;
	} else {
		float root1 = v*v*v*v + 2*v*v*g*dy-g*g*DFsq;
		if(root1 >= 0) {
			float root2 = 2*DFsq*Dsq*(v*v + g*dy + (highTrajectory ? -1 : 1)
				* sqrt(root1));
			if(root2 >= 0) {
				Vxz = sqrt(root2)/(2*Dsq);
				Vy = (dxz == 0 || Vxz == 0) ? v : (Vxz*dy/dxz - dxz*g/(2*Vxz));
			} else {
				Vxz = 0;
				Vy = v;
			}
		} else {
			Vxz = 0;
			Vy = v;
		}
	}
	float3 dir(diff.x, 0, diff.z);
	dir.Normalize();
	dir *= Vxz;
	dir.y = Vy;
	dir.Normalize();
        wantedDir = dir;
(there are also a few other non-pertinent changes.)

Because this code will run every emg shot, I'm worried that the two sqrt functions will hurt performance. Maybe I'm just being silly here. Anyone want to alleviate/confirm my fears?
User avatar
rattle
Damned Developer
Posts: 8278
Joined: 01 Jun 2006, 13:15

Post by rattle »

Doesn't the EMG weapon have it's own type?
User avatar
ILMTitan
Spring Developer
Posts: 410
Joined: 13 Nov 2004, 08:35

Post by ILMTitan »

.... yes. Ok, I'm going to commit this.
User avatar
Zpock
Posts: 1218
Joined: 16 Sep 2004, 23:20

Post by Zpock »

Wow, this fixes aiming for turrets on aircraft?
User avatar
KDR_11k
Game Developer
Posts: 8293
Joined: 25 Jun 2006, 08:44

Post by KDR_11k »

What's the difference between EMG and Cannon?
User avatar
ILMTitan
Spring Developer
Posts: 410
Joined: 13 Nov 2004, 08:35

Post by ILMTitan »

EMG projectiles are not affected by gravity, Cannon projectiles are. In a different sense, EMGs are weapons with lineofsight = 1, renderType = 4 and color = 2, while cannons are the default weapon type (any weapon that does not otherwise describe a valid weapon).

This does not fix anything specifically related to aircraft, but if an aircraft had a cannon weapon as a turret, it was probably not aiming very well because the aircraft was above the target, and this will fix that.
User avatar
KDR_11k
Game Developer
Posts: 8293
Joined: 25 Jun 2006, 08:44

Post by KDR_11k »

I think arced (TrajectoryHeight=1) missiles have a similar problem (they ignore the y distance), do you feel like looking into that?
User avatar
Decimator
Posts: 1118
Joined: 24 Jul 2005, 04:15

Post by Decimator »

Arced missiles definitely have a problem with height differences. I think it may have been fixed in svn, so I would suggest trying a recent beta build.
User avatar
KDR_11k
Game Developer
Posts: 8293
Joined: 25 Jun 2006, 08:44

Post by KDR_11k »

Nope, it's not fixed in the SVN.
User avatar
jcnossen
Former Engine Dev
Posts: 2440
Joined: 05 Jun 2005, 19:13

Post by jcnossen »

Because this code will run every emg shot, I'm worried that the two sqrt functions will hurt performance. Maybe I'm just being silly here. Anyone want to alleviate/confirm my fears?
You could do sqrtf() for lower precision, but other than that i doubt its going to be such a problem. Vectors are being normalized all the time as well, much more often than the cannon aiming probably.
User avatar
ILMTitan
Spring Developer
Posts: 410
Joined: 13 Nov 2004, 08:35

Post by ILMTitan »

The Gundam Federation Type 61 tank, which I'm pretty sure has an arched missile weapon, has no problem hitting ground far above and far below itself in my version (Latest svn).
User avatar
Snipawolf
Posts: 4357
Joined: 12 Dec 2005, 01:49

Post by Snipawolf »

testing? With gundam!? Oh how happy Smoth will be...
User avatar
ILMTitan
Spring Developer
Posts: 410
Joined: 13 Nov 2004, 08:35

Post by ILMTitan »

I did all of my limited arc/unit rotation testing on Gundam. And it had the only arced missile unit I could think of, other than the AA arm rocket artillery bot that does not have enough accuracy to test accuracy on.
User avatar
smoth
Posts: 22309
Joined: 13 Jan 2005, 00:46

Post by smoth »

ILM, while you are look at that, would you mind looking at the "accuracy" tag in the weapon TDF?

The shots from indirect(trajectory height weapons) do not drift like other weapons can. So they always hit the same spot no matter what accuracy I give them.
User avatar
ILMTitan
Spring Developer
Posts: 410
Joined: 13 Nov 2004, 08:35

Post by ILMTitan »

Sounds like a new project. Will do. Is that just trajectory height missiles? Cannon weapons do not have a trajectory height, and I know the shellshocker (AA L1 artillery) has some inaccuracy.
User avatar
smoth
Posts: 22309
Joined: 13 Jan 2005, 00:46

Post by smoth »

Just the trajectory height weapons. To see what I mean.. give the zaku2 and accuracy of 2000 and watch how it's shots go all over the place and the guntank with the same accuracies all land in the same spot.
User avatar
KDR_11k
Game Developer
Posts: 8293
Joined: 25 Jun 2006, 08:44

Post by KDR_11k »

I've patched that, remember? The guntank's shots go all over the place in the SVN. The missile launcher weapon was just missing the code that adds the inaccuracy to the weapon vector.

The T61 aims properly but the Guntank doesn't.
User avatar
smoth
Posts: 22309
Joined: 13 Jan 2005, 00:46

Post by smoth »

OH WOW! no, kdr, I had forgotten. Sorry about that.
Post Reply

Return to “Engine”