Ticket #2394: vector_prototype.diff
File vector_prototype.diff, 9.4 KB (added by , 10 years ago) |
---|
-
binaries/data/mods/public/globalscripts/vector.js
58 58 59 59 Vector2D.prototype.length = function() 60 60 { 61 return sqrt(this.lengthSquared());61 return Math.sqrt(this.lengthSquared()); 62 62 }; 63 63 64 64 Vector2D.prototype.normalize = function() 65 65 { 66 66 var mag = this.length(); 67 if (!mag) 68 return; 67 69 68 70 this.x /= mag; 69 71 this.y /= mag; … … 127 129 128 130 Vector3D.prototype.length = function() 129 131 { 130 return sqrt(this.lengthSquared());132 return Math.sqrt(this.lengthSquared()); 131 133 }; 132 134 133 135 Vector3D.prototype.normalize = function() 134 136 { 135 137 var mag = this.length(); 138 if (!mag) 139 return; 136 140 137 141 this.x /= mag; 138 142 this.y /= mag; -
binaries/data/mods/public/maps/random/rmgen/vector.js
1 /////////////////////////////////////////////////////////////////////2 // Vector2D3 //4 // Class for representing and manipulating 2D vectors5 //6 /////////////////////////////////////////////////////////////////////7 8 // TODO: Type errors if v not instanceof Vector classes9 // TODO: Possibly implement in C++10 11 function Vector2D(x, y)12 {13 if (arguments.length == 2)14 {15 this.set(x, y);16 }17 else18 {19 this.set(0, 0);20 }21 }22 23 Vector2D.prototype.set = function(x, y)24 {25 this.x = x;26 this.y = y;27 };28 29 Vector2D.prototype.add = function(v)30 {31 return new Vector2D(this.x + v.x, this.y + v.y);32 };33 34 Vector2D.prototype.sub = function(v)35 {36 return new Vector2D(this.x - v.x, this.y - v.y);37 };38 39 Vector2D.prototype.mult = function(f)40 {41 return new Vector2D(this.x * f, this.y * f);42 };43 44 Vector2D.prototype.div = function(f)45 {46 return new Vector2D(this.x / f, this.y / f);47 };48 49 Vector2D.prototype.dot = function(v)50 {51 return this.x * v.x + this.y * v.y;52 };53 54 Vector2D.prototype.lengthSquared = function()55 {56 return this.dot(this);57 };58 59 Vector2D.prototype.length = function()60 {61 return sqrt(this.lengthSquared());62 };63 64 Vector2D.prototype.normalize = function()65 {66 var mag = this.length();67 68 this.x /= mag;69 this.y /= mag;70 };71 72 /////////////////////////////////////////////////////////////////////73 // Vector3D74 //75 // Class for representing and manipulating 3D vectors76 //77 /////////////////////////////////////////////////////////////////////78 79 function Vector3D(x, y, z)80 {81 if (arguments.length == 3)82 {83 this.set(x, y, y);84 }85 else86 {87 this.set(0, 0, 0);88 }89 }90 91 Vector3D.prototype.set = function(x, y, z)92 {93 this.x = x;94 this.y = y;95 this.z = z;96 };97 98 Vector3D.prototype.add = function(v)99 {100 return new Vector3D(this.x + v.x, this.y + v.y, this.z + v.z);101 };102 103 Vector3D.prototype.sub = function(v)104 {105 return new Vector3D(this.x - v.x, this.y - v.y, this.z - v.z);106 };107 108 Vector3D.prototype.mult = function(f)109 {110 return new Vector3D(this.x * f, this.y * f, this.z * f);111 };112 113 Vector3D.prototype.div = function(f)114 {115 return new Vector3D(this.x / f, this.y / f, this.z / f);116 };117 118 Vector3D.prototype.dot = function(v)119 {120 return this.x * v.x + this.y * v.y + this.z * v.z;121 };122 123 Vector3D.prototype.lengthSquared = function()124 {125 return this.dot(this);126 };127 128 Vector3D.prototype.length = function()129 {130 return sqrt(this.lengthSquared());131 };132 133 Vector3D.prototype.normalize = function()134 {135 var mag = this.length();136 137 this.x /= mag;138 this.y /= mag;139 this.z /= mag;140 };141 -
binaries/data/mods/public/simulation/components/Attack.js
449 449 return; 450 450 var targetPosition = cmpTargetPosition.GetPosition(); 451 451 452 var relativePosition = {"x": targetPosition.x - selfPosition.x, "z": targetPosition.z - selfPosition.z}452 var relativePosition = new Vector3D(targetPosition.x - selfPosition.x, 0, targetPosition.z - selfPosition.z); 453 453 var previousTargetPosition = Engine.QueryInterface(target, IID_Position).GetPreviousPosition(); 454 454 455 var targetVelocity = {"x": (targetPosition.x - previousTargetPosition.x) / this.turnLength, "z": (targetPosition.z - previousTargetPosition.z) / this.turnLength}455 var targetVelocity = new Vector3D((targetPosition.x - previousTargetPosition.x) / this.turnLength, 0, (targetPosition.z - previousTargetPosition.z) / this.turnLength); 456 456 // the component of the targets velocity radially away from the archer 457 var radialSpeed = Math.VectorDot(relativePosition, targetVelocity) / Math.VectorLength(relativePosition);457 var radialSpeed = relativePosition.dot(targetVelocity) / relativePosition.length(); 458 458 459 var horizDistance = Math.VectorDistance(targetPosition, selfPosition);459 var horizDistance = targetPosition.sub(selfPosition).horizontalLength(); 460 460 461 461 // This is an approximation of the time ot the target, it assumes that the target has a constant radial 462 462 // velocity, but since units move in straight lines this is not true. The exact value would be more … … 475 475 var distanceModifiedSpread = spread * horizDistance/elevationAdaptedMaxRange; 476 476 477 477 var randNorm = this.GetNormalDistribution(); 478 var offsetX = randNorm[0] * distanceModifiedSpread * (1 + Math.VectorLength(targetVelocity) / 20);479 var offsetZ = randNorm[1] * distanceModifiedSpread * (1 + Math.VectorLength(targetVelocity) / 20);478 var offsetX = randNorm[0] * distanceModifiedSpread * (1 + targetVelocity.length() / 20); 479 var offsetZ = randNorm[1] * distanceModifiedSpread * (1 + targetVelocity.length() / 20); 480 480 481 var realTargetPosition = { "x": predictedPosition.x + offsetX, "y": targetPosition.y, "z": predictedPosition.z + offsetZ };481 var realTargetPosition = new Vector3D(predictedPosition.x + offsetX, targetPosition.y,predictedPosition.z + offsetZ); 482 482 483 483 // Calculate when the missile will hit the target position 484 var realHorizDistance = Math.VectorDistance(realTargetPosition, selfPosition);484 var realHorizDistance = selfPosition.sub(realTargetPosition).horizontalLength(); 485 485 var timeToTarget = realHorizDistance / horizSpeed; 486 486 487 487 var missileDirection = {"x": (realTargetPosition.x - selfPosition.x) / realHorizDistance, "z": (realTargetPosition.z - selfPosition.z) / realHorizDistance}; … … 512 512 var curPos = cmpTargetPosition.GetPosition(); 513 513 var prevPos = cmpTargetPosition.GetPreviousPosition(); 514 514 lateness /= 1000; 515 return {"x": (curPos.x * (this.turnLength - lateness) + prevPos.x * lateness) / this.turnLength, 516 "z": (curPos.z * (this.turnLength - lateness) + prevPos.z * lateness) / this.turnLength};515 516 return curPos.mult(this.turnLength - lateness).add(prevPos.mult(lateness)).div(this.turnLength); 517 517 }; 518 518 519 519 // Tests whether it point is inside of ent's footprint … … 532 532 533 533 if (targetShape.type === 'circle') 534 534 { 535 // Use VectorDistanceSquared and square targetShape.radius to avoid square roots.536 return (Math.VectorDistanceSquared(point, targetPosition) < (targetShape.radius * targetShape.radius));535 // Use lengthSquared and square targetShape.radius to avoid square roots. 536 return targetPosition.sub(point).lengthSquared() < (targetShape.radius * targetShape.radius)); 537 537 } 538 538 else 539 539 { -
source/scriptinterface/ScriptInterface.cpp
1033 1033 return ok ? true : false; 1034 1034 } 1035 1035 1036 1037 1036 bool ScriptInterface::Eval(const char* code) 1038 1037 { 1039 1038 jsval rval; -
source/simulation2/scripting/EngineScriptConversions.cpp
163 163 164 164 template<> jsval ScriptInterface::ToJSVal<CFixedVector3D>(JSContext* cx, const CFixedVector3D& val) 165 165 { 166 JSObject* obj = JS_NewObject(cx, NULL, NULL, NULL); 166 // apply the Vector3D prototype to the return value; 167 jsval vector; 168 JSObject* obj; 169 bool success = false; 170 if (JS_GetProperty(cx, JS_GetGlobalObject(cx), "Vector3D", &vector)) 171 { 172 jsval proto; 173 if (JS_GetProperty(cx, JSVAL_TO_OBJECT(vector), "prototype", &proto)) 174 { 175 success = true; 176 obj = JS_NewObject(cx, NULL, JSVAL_TO_OBJECT(proto), NULL); 177 } 178 } 179 if (!success) 180 { 181 LOGWARNING(L"Vector3D global script not loaded"); 182 obj = JS_NewObject(cx, NULL, NULL, NULL); 183 } 184 167 185 if (!obj) 168 186 return JSVAL_VOID; 169 187 … … 197 215 198 216 template<> jsval ScriptInterface::ToJSVal<CFixedVector2D>(JSContext* cx, const CFixedVector2D& val) 199 217 { 200 JSObject* obj = JS_NewObject(cx, NULL, NULL, NULL); 218 // apply the Vector2D prototype to the return value 219 jsval vector; 220 JSObject* obj; 221 bool success = false; 222 if (JS_GetProperty(cx, JS_GetGlobalObject(cx), "Vector2D", &vector)) 223 { 224 jsval proto; 225 if (JS_GetProperty(cx, JSVAL_TO_OBJECT(vector), "prototype", &proto)) 226 { 227 success = true; 228 obj = JS_NewObject(cx, NULL, JSVAL_TO_OBJECT(proto), NULL); 229 } 230 } 231 if (!success) 232 { 233 LOGWARNING(L"Vector3D global script not loaded"); 234 obj = JS_NewObject(cx, NULL, NULL, NULL); 235 } 236 201 237 if (!obj) 202 238 return JSVAL_VOID; 203 239