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();
}
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;
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?