[nexuiz-commits] r6198 - in trunk/data/qcsrc: common server

DONOTREPLY at icculus.org DONOTREPLY at icculus.org
Tue Mar 17 05:45:47 EDT 2009


Author: div0
Date: 2009-03-17 05:45:46 -0400 (Tue, 17 Mar 2009)
New Revision: 6198

Modified:
   trunk/data/qcsrc/common/util.qc
   trunk/data/qcsrc/common/util.qh
   trunk/data/qcsrc/server/cl_weaponsystem.qc
Log:
add a function solve_quadratic, and make newton projectiles already use it
more uses will come later


Modified: trunk/data/qcsrc/common/util.qc
===================================================================
--- trunk/data/qcsrc/common/util.qc	2009-03-17 08:04:33 UTC (rev 6197)
+++ trunk/data/qcsrc/common/util.qc	2009-03-17 09:45:46 UTC (rev 6198)
@@ -1559,3 +1559,56 @@
 	_shufflewords_str = string_null;
 	return str;
 }
+
+vector solve_quadratic(float a, float b, float c) // ax^2 + bx + c = 0
+{
+	vector v;
+	float D;
+	v = '0 0 0';
+	if(a == 0)
+	{
+		if(b != 0)
+		{
+			v_x = v_y = -c / b;
+			v_z = 1;
+		}
+		else
+		{
+			if(c == 0)
+			{
+				// actually, every number solves the equation!
+				v_z = 1;
+			}
+		}
+	}
+	else
+	{
+		D = b*b - 4*a*c;
+		if(D >= 0)
+		{
+			D = sqrt(D);
+			if(a > 0) // put the smaller solution first
+			{
+				v_x = ((-b)-D) / (2*a);
+				v_y = ((-b)+D) / (2*a);
+			}
+			else
+			{
+				v_x = (-b+D) / (2*a);
+				v_y = (-b-D) / (2*a);
+			}
+			v_z = 1;
+		}
+		else
+		{
+			// complex solutions!
+			v_x = -b / (2*a);
+			if(a > 0)
+				v_y =  D / (2*a);
+			else
+				v_y = -D / (2*a);
+			v_z = 0;
+		}
+	}
+	return v;
+}

Modified: trunk/data/qcsrc/common/util.qh
===================================================================
--- trunk/data/qcsrc/common/util.qh	2009-03-17 08:04:33 UTC (rev 6197)
+++ trunk/data/qcsrc/common/util.qh	2009-03-17 09:45:46 UTC (rev 6198)
@@ -160,3 +160,9 @@
 string shufflewords(string str);
 
 string substring_range(string s, float b, float e);
+
+vector solve_quadratic(float a, float b, float c);
+// solution 1 -> x
+// solution 2 -> y
+// z = 1 if a real solution exists, 0 if not
+// if no real solution exists, x contains the real part and y the imaginary part of the complex solutions x+iy and x-iy

Modified: trunk/data/qcsrc/server/cl_weaponsystem.qc
===================================================================
--- trunk/data/qcsrc/server/cl_weaponsystem.qc	2009-03-17 08:04:33 UTC (rev 6197)
+++ trunk/data/qcsrc/server/cl_weaponsystem.qc	2009-03-17 09:45:46 UTC (rev 6198)
@@ -1108,18 +1108,17 @@
 		// fire at smallest possible mspeed that works?
 		// |(mdirection * pvelocity) * pvelocity - pvelocity| = mspeed
 
-		float D;
-		float p;
-		float q;
-		p = mdirection * pvelocity;
-		q = pvelocity * pvelocity - mspeed * mspeed;
-		D = p * p - q;
-		if(D < 0)
+		vector solution;
+		solution = solve_quadratic(1, -2 * (mdirection * pvelocity), pvelocity * pvelocity - mspeed * mspeed);
+		if(solution_z)
+			outspeed = solution_y; // the larger one
+		else
 		{
+			//outspeed = 0; // slowest possible shot
+			outspeed = solution_x; // the real part (that is, the average!)
 			//dprint("impossible shot, adjusting\n");
-			D = 0;
 		}
-		outspeed = p + sqrt(D);
+
 		outspeed = bound(mspeed * 0.7, outspeed, mspeed * 5.0);
 		outvelocity = mdirection * outspeed;
 	}



More information about the nexuiz-commits mailing list