HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Normalizing forces for basketball game

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
basketballforcesgamefornormalizing

Problem

Recently I found the need to implement a normalization formula in a couple of places in my basketball game, so I created some static methods inside of a class to do so.

public class Normalize {

    //explanation of normalization formula:
    /*
    Suppose you have a range or scale from A to B and you want to convert 
    it to a scale of 1 to 10, where A maps to 1 and B maps to 10.  
    Furthermore, we want to do this with a linear function, so that for 
    example the point midway between A and B maps to halfway between 1 and 
    10, or 5.5.

    Then the following (linear) equation can be applied to any number 
    x on the A-B scale:
    */  
    //y = 1 + (x-A)*(10-1)/(B-A)

    public static Vector2 normalizedVector(Vector2 velocity, float min, float max, float normalizedMin, float normalizedMax) {
        float inputX = velocity.x;
        float inputY = velocity.y;
        float normalizedX = normalizedMin + (inputX-min) * (normalizedMax - normalizedMin) / (max-min);
        float normalizedY = normalizedMin + (inputY-min) * (normalizedMax - normalizedMin) / (max-min);
        return new Vector2(normalizedX, normalizedY);
    }

    public static float normalizedStrength (Vector2 velocity, float min, float max, float normalizedMin, float normalizedMax) {
        float inputX = velocity.x;
        float inputY = velocity.y;
        float inputTotal = inputX + inputY;
        return normalizedMin + (inputTotal-min) * (normalizedMax - normalizedMin) / (max-min);
    }
}


I use this code in a couple of places in the game. First, I'm using it to reduce the strength of the impact force of a ball when it hits the hoop, in order to make sure that it does not move too far as a result of the collision:

```
public void ballCollided(float angle, Vector2 velocity) {

//the angle is not currently used in the calculation

//the 0 and 100 are hard coded here
//the value represents the power of the ball velocity
//that i got from shooting balls

Solution

I have just some small tips with regards to this simple code.

Avoid duplication like this:

float normalizedX = normalizedMin + (inputX-min) * (normalizedMax - normalizedMin) / (max-min);
float normalizedY = normalizedMin + (inputY-min) * (normalizedMax - normalizedMin) / (max-min);


I re-read 3 times these long statements to make sure that the expression at the right are identical. If you put that in a local variable, then:

  • it will be easier to read, since it will be obvious that the expressions are the same



  • it will be easier to read without scrolling to the right



  • it will be easier to modify, in case you need to, as you can change in one place



Look, now the piece of code fits within the page here too:

float coef = (normalizedMax - normalizedMin) / (max - min);
float normalizedX = normalizedMin + (inputX-min) * coef;
float normalizedY = normalizedMin + (inputY-min) * coef;

Code Snippets

float normalizedX = normalizedMin + (inputX-min) * (normalizedMax - normalizedMin) / (max-min);
float normalizedY = normalizedMin + (inputY-min) * (normalizedMax - normalizedMin) / (max-min);
float coef = (normalizedMax - normalizedMin) / (max - min);
float normalizedX = normalizedMin + (inputX-min) * coef;
float normalizedY = normalizedMin + (inputY-min) * coef;

Context

StackExchange Code Review Q#114085, answer score: 5

Revisions (0)

No revisions yet.