Fandom

BF:Heroes Wiki

Comments0

Code about weapon deviation hypothesis

Pier4r May 1, 2013 User blog:Pier4r

Run it in node.js .

/*
 Talk: http://battlefieldheroes.wikia.com/wiki/Talk:Weapons (01.05.2013)
*/

var deviation = 0.9;
  //over 10 meters, the max (or average? dunno! By test in game seems a max)
  //deviation is 0.9 meters
var numberOfRoundsFired = 1000*1000;
var discretizationStep = 100;

var fs = require('fs');

pickLinear();

/*
 testing the following code (part of it)
float randomx = new Random().nextFloat();
randomx *= new Random().nextFloat()*2-1;
float randomy = new Random().nextFloat();
randomy *= new Random().nextFloat()*2-1;
relativeVelocityVector.x += randomx*deviation;
relativeVelocityVector.y += randomy*deviation;
relativeVelocityVector.z = 1;
relativeVelocityVector *= velocity;
weapon.velocity = transformRelativeToAbsoluteSpace(relativeVelocityVector);
---
note that the minimum value of randomx and randomy
is almost-1, while the max is almost 1...

The following code multiply al random numbers for a power of ten to
create a matrix of discrete values.
*/
function ideaOfBFHWiki(){
    //squaretarget: the matrix where the bullet "land"
    var squareTarget = new Array(2*discretizationStep+1); //from -10^x to +10^x with zero
    for (var i=0; i<squareTarget.length; i++){
        squareTarget[i]= new Array(squareTarget.length);
        
        //init
        for (var j=0; j<squareTarget.length ; j++){
            squareTarget[i][j]=0;
        }
    }
    
    var randomx, randomy;
    for (i=0; i<numberOfRoundsFired; i++){
        randomx = Math.random()*(Math.random()*2-1);
        //randomx = Math.random()*Math.random()*2;
          //just use positive values
        randomy = Math.random()*(Math.random()*2-1);
        
        randomx = Math.floor(randomx*discretizationStep)
                  +discretizationStep; //offset else the value can be negative
        randomy = Math.floor(randomy*discretizationStep)+discretizationStep;        
        
        squareTarget[randomy][randomx]++;
        if (i % 1000 == 0){
            console.log (i);
        }
    }
    
    printBFHwikiIdea(squareTarget);
}

/*
 testing
var randomRadius : float = Math.random() * Math.random(); // or use Math.Power(Math.random(), 2);
var randomPosition : float = Math.random()*2*Math.PI*randomRadius;
relativeVelocityVector.x = Math.Sinus(randomPosition) * deviation;
relativeVelocityVector.y = Math.Cosinus(randomPosition) * deviation;

--
Note that, in a circle of max radius 1, x and y position can be -1 to 1
 */
function ideaOfBFHWiki2(){
    //squaretarget: the matrix where the bullet "land"
    var squareTarget = new Array(2*discretizationStep+1); //from -10^x to +10^x with zero
    for (var i=0; i<squareTarget.length; i++){
        squareTarget[i]= new Array(squareTarget.length);
        
        //init
        for (var j=0; j<squareTarget.length ; j++){
            squareTarget[i][j]=0;
        }
    }
    
    var randomRadius, randomCirPosition,
        randomx, randomy;
      //given a radius, compute the circonference and pick a random
      //position
    for (i=0; i<numberOfRoundsFired; i++){
        randomRadius = Math.random()*Math.random();
        randomCirPosition = Math.random()*2*Math.PI;
        
        randomx = Math.cos(randomCirPosition)*randomRadius;
        randomy = Math.sin(randomCirPosition)*randomRadius;
        
        //array position
        randomx = Math.floor(randomx*discretizationStep)
                  +discretizationStep; //offset else the value can be negative
        randomy = Math.floor(randomy*discretizationStep)+discretizationStep;
        
        squareTarget[randomy][randomx]++;
        if (i % 1000 == 0){
            console.log (i);
        }
    }
    
    printBFHwikiIdea(squareTarget);
}


/*
 pseudocode 
var randomRadius : float = Math.random();
randomRadius = -0.5 * randomRadius * randomRadius + randomRadius;
var randomPosition : float = Math.random()*2*Math.PI;
relativeVelocityVector.x = Math.Sinus(randomPosition) * deviation * randomRadius;
relativeVelocityVector.y = Math.Cosinus(randomPosition) * deviation * randomRadius;
*/
function ideaOfBFHWiki_piramidalLike(){
    //squaretarget: the matrix where the bullet "land"
    var squareTarget = new Array(2*discretizationStep+1); //from -10^x to +10^x with zero
    for (var i=0; i<squareTarget.length; i++){
        squareTarget[i]= new Array(squareTarget.length);
        
        //init
        for (var j=0; j<squareTarget.length ; j++){
            squareTarget[i][j]=0;
        }
    }
    
    var randomRadius, randomCirPosition,
        randomx, randomy;
      //given a radius, compute the circonference and pick a random
      //position
    for (i=0; i<numberOfRoundsFired; i++){
        randomRadius = Math.random();
        randomRadius = -0.5*randomRadius*randomRadius + randomRadius;
        randomCirPosition = Math.random()*2*Math.PI;
        
        randomx = Math.cos(randomCirPosition)*randomRadius;
        randomy = Math.sin(randomCirPosition)*randomRadius;
        
        //array position
        randomx = Math.floor(randomx*discretizationStep)
                  +discretizationStep; //offset else the value can be negative
        randomy = Math.floor(randomy*discretizationStep)+discretizationStep;
        
        squareTarget[randomy][randomx]++;
        if (i % 1000 == 0){
            console.log (i);
        }
    }
    
    printBFHwikiIdea(squareTarget);
}

/*
1/(x^2+1) comes close to a gaußian distribution and its integral is arcusTanges(x).
So this could get you a really nice curve (rest of the code is same like the flat curve): 
var randomRadius : float = Math.ArcTan(Math.random());
*/
function ideaOfBFHWiki_arcTan(){
    //squaretarget: the matrix where the bullet "land"
    var squareTarget = new Array(2*discretizationStep+1); //from -10^x to +10^x with zero
    for (var i=0; i<squareTarget.length; i++){
        squareTarget[i]= new Array(squareTarget.length);
        
        //init
        for (var j=0; j<squareTarget.length ; j++){
            squareTarget[i][j]=0;
        }
    }
    
    var randomRadius, randomCirPosition,
        randomx, randomy;
      //given a radius, compute the circonference and pick a random
      //position
    for (i=0; i<numberOfRoundsFired; i++){
        randomRadius = Math.atan(Math.random());
        randomCirPosition = Math.random()*2*Math.PI;
        
        randomx = Math.cos(randomCirPosition)*randomRadius;
        randomy = Math.sin(randomCirPosition)*randomRadius;
        
        //array position
        randomx = Math.floor(randomx*discretizationStep)
                  +discretizationStep; //offset else the value can be negative
        randomy = Math.floor(randomy*discretizationStep)+discretizationStep;
        
        squareTarget[randomy][randomx]++;
        if (i % 1000 == 0){
            console.log (i);
        }
    }
    
    printBFHwikiIdea(squareTarget);
}

function printBFHwikiIdea(squareTarget){
    var stringRes = "";
    for (var i=0; i<squareTarget.length; i++){
        for (var j=0; j<squareTarget.length ; j++){
            stringRes += squareTarget[i][j] + "\t";
        }
        stringRes += "\n";
    }
    fs.appendFileSync("squareTargetResult.txt", stringRes);
}

/*
 The idea is: do a triangle density function and after that rotate it to get a cone.
 
 The traingle density function is built over a uniform distribution. The shape is like
 an "house front drawn by a kid".
 
 The triangular density have the following shape (numbers are not fixed, it is an example):
 -Suppose that the base have a length of 201 unit, each unit have a minimal
  probability of 1/1000. Then 201/1000 of probabilities are already given.
  So the triangle has an area of 799/1000, with an height of 799/100500.
  That is a bit ugly so round it to 159/20000....
  ..i'm too lazy. The idea is not bad, it's me.
*/
function coneDistribution(){
    //squaretarget: the matrix where the bullet "land"
    var squareTarget = new Array(2*discretizationStep+1); //from -10^x to +10^x with zero
    for (var i=0; i<squareTarget.length; i++){
        squareTarget[i]= new Array(squareTarget.length);
        
        //init
        for (var j=0; j<squareTarget.length ; j++){
            squareTarget[i][j]=0;
        }
    }
    
    //nothing
}

/*
 Taking the pattern from BFHwiki user, just use a sort of transfer function.
 (a quick montecarlo method)
 So picking a random number and using it in a parabola to get the values
 with the wanted distribution.
*/
function pickFromParabola(){
    //squaretarget: the matrix where the bullet "land"
    var squareTarget = new Array(2*discretizationStep+1); //from -10^x to +10^x with zero
    for (var i=0; i<squareTarget.length; i++){
        squareTarget[i]= new Array(squareTarget.length);
        
        //init
        for (var j=0; j<squareTarget.length ; j++){
            squareTarget[i][j]=0;
        }
    }
    
    var randomRadius, randomCirPosition,
        randomx, randomy;
      //given a radius, compute the circonference and pick a random
      //position
    for (i=0; i<numberOfRoundsFired; i++){
        randomRadius = Math.random();
        randomRadius = Math.pow((randomRadius - 1),2);
        randomCirPosition = Math.random()*2*Math.PI;
        
        randomx = Math.cos(randomCirPosition)*randomRadius;
        randomy = Math.sin(randomCirPosition)*randomRadius;
        
        //array position
        randomx = Math.floor(randomx*discretizationStep)
                  +discretizationStep; //offset else the value can be negative
        randomy = Math.floor(randomy*discretizationStep)+discretizationStep;
        
        squareTarget[randomy][randomx]++;
        if (i % 1000 == 0){
            console.log (i);
        }
    }
    
    printBFHwikiIdea(squareTarget);
}

function pickLinear(){
    
    //squaretarget: the matrix where the bullet "land"
    var squareTarget = new Array(2*discretizationStep+1); //from -10^x to +10^x with zero
    for (var i=0; i<squareTarget.length; i++){
        squareTarget[i]= new Array(squareTarget.length);
        
        //init
        for (var j=0; j<squareTarget.length ; j++){
            squareTarget[i][j]=0;
        }
    }
    
    var randomRadius, randomCirPosition,
        randomx, randomy;
      //given a radius, compute the circonference and pick a random
      //position
    for (i=0; i<numberOfRoundsFired; i++){
        randomRadius = Math.random();
        //randomRadius = Math.pow((randomRadius - 1),2);
        randomCirPosition = Math.random()*2*Math.PI;
        
        randomx = Math.cos(randomCirPosition)*randomRadius;
        randomy = Math.sin(randomCirPosition)*randomRadius;
        
        //array position
        //randomx = Math.floor(randomx*discretizationStep)
        //          +discretizationStep; //offset else the value can be negative
        //randomy = Math.floor(randomy*discretizationStep)+discretizationStep;
        randomx = Math.round(randomx*discretizationStep)+discretizationStep;
        randomy = Math.round(randomy*discretizationStep)+discretizationStep;
        
        squareTarget[randomy][randomx]++;
        if (i % 1000 == 0){
            console.log (i);
        }
    }
    
    printBFHwikiIdea(squareTarget);
}







 

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.