Ticket #2394: vector_prototype.diff

File vector_prototype.diff, 9.4 KB (added by sanderd17, 10 years ago)
  • binaries/data/mods/public/globalscripts/vector.js

     
    5858
    5959Vector2D.prototype.length = function()
    6060{
    61     return sqrt(this.lengthSquared());
     61    return Math.sqrt(this.lengthSquared());
    6262};
    6363
    6464Vector2D.prototype.normalize = function()
    6565{
    6666    var mag = this.length();
     67    if (!mag)
     68        return;
    6769   
    6870    this.x /= mag;
    6971    this.y /= mag;
     
    127129
    128130Vector3D.prototype.length = function()
    129131{
    130     return sqrt(this.lengthSquared());
     132    return Math.sqrt(this.lengthSquared());
    131133};
    132134
    133135Vector3D.prototype.normalize = function()
    134136{
    135137    var mag = this.length();
     138    if (!mag)
     139        return;
    136140   
    137141    this.x /= mag;
    138142    this.y /= mag;
  • binaries/data/mods/public/maps/random/rmgen/vector.js

     
    1 /////////////////////////////////////////////////////////////////////
    2 //  Vector2D
    3 //
    4 //  Class for representing and manipulating 2D vectors
    5 //
    6 /////////////////////////////////////////////////////////////////////
    7 
    8 // TODO: Type errors if v not instanceof Vector classes
    9 // 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     else
    18     {
    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 //  Vector3D
    74 //
    75 //  Class for representing and manipulating 3D vectors
    76 //
    77 /////////////////////////////////////////////////////////////////////
    78 
    79 function Vector3D(x, y, z)
    80 {
    81     if (arguments.length == 3)
    82     {
    83         this.set(x, y, y);
    84     }
    85     else
    86     {
    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

     
    449449            return;
    450450        var targetPosition = cmpTargetPosition.GetPosition();
    451451
    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);
    453453        var previousTargetPosition = Engine.QueryInterface(target, IID_Position).GetPreviousPosition();
    454454
    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);
    456456        // 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();
    458458
    459         var horizDistance = Math.VectorDistance(targetPosition, selfPosition);
     459        var horizDistance = targetPosition.sub(selfPosition).horizontalLength();
    460460
    461461        // This is an approximation of the time ot the target, it assumes that the target has a constant radial
    462462        // velocity, but since units move in straight lines this is not true.  The exact value would be more
     
    475475        var distanceModifiedSpread = spread * horizDistance/elevationAdaptedMaxRange;
    476476
    477477        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);
    480480
    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);
    482482
    483483        // Calculate when the missile will hit the target position
    484         var realHorizDistance = Math.VectorDistance(realTargetPosition, selfPosition);
     484        var realHorizDistance = selfPosition.sub(realTargetPosition).horizontalLength();
    485485        var timeToTarget = realHorizDistance / horizSpeed;
    486486
    487487        var missileDirection = {"x": (realTargetPosition.x - selfPosition.x) / realHorizDistance, "z": (realTargetPosition.z - selfPosition.z) / realHorizDistance};
     
    512512    var curPos = cmpTargetPosition.GetPosition();
    513513    var prevPos = cmpTargetPosition.GetPreviousPosition();
    514514    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);
    517517};
    518518
    519519// Tests whether it point is inside of ent's footprint
     
    532532
    533533    if (targetShape.type === 'circle')
    534534    {
    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));
    537537    }
    538538    else
    539539    {
  • source/scriptinterface/ScriptInterface.cpp

     
    10331033    return ok ? true : false;
    10341034}
    10351035
    1036 
    10371036bool ScriptInterface::Eval(const char* code)
    10381037{
    10391038    jsval rval;
  • source/simulation2/scripting/EngineScriptConversions.cpp

     
    163163
    164164template<> jsval ScriptInterface::ToJSVal<CFixedVector3D>(JSContext* cx, const CFixedVector3D& val)
    165165{
    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
    167185    if (!obj)
    168186        return JSVAL_VOID;
    169187
     
    197215
    198216template<> jsval ScriptInterface::ToJSVal<CFixedVector2D>(JSContext* cx, const CFixedVector2D& val)
    199217{
    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
    201237    if (!obj)
    202238        return JSVAL_VOID;
    203239