• Creating a Sprite and setting its settings (attributes, position, rotation and movement)

    1 - Creating a Sprite

    To create a sprite just create an object of the Sprite class and use as parameter the directory of your image.

    								Sprite nave = new Sprite(“nave.png”);
    							

    2 - Setting settings

    The Sprite class has been modified, and new features such as mass, friction, restitution were added. All of them are set by calling their corresponding methods. setMass, setFriction, setRestitution. And to return the value, the following methods are called: getMass, and getFriction getRestitution.

    nave.setMass(1f);
    nave.setFriction(0.2f);
    nave.setRestitution(0f);
    Note: The values passed as a parameter must be of type float. Standard values are 0.5 each, in other words, if any of them is not set, the default value is 0.5.

    3 - Position and Rotation

    The methods used in the positioning of the sprite are setX, setY. And to return the value of their positions are used the methods getX and getY.

    nave.setX(200);
    nave.setY(100);
    Note: Although the Sprite class has access to the attributes x and y, while using the Physics class these attributes should not be accessed, or the sprite will not be set in the desired position.

    To rotate a sprite, simply set the value of its rotation (in radians) by calling the method SetRotation and getRotation to return its value.

    Ex:
    nave.setRotation(Math.PI/4);
      
    Note: This method (SetRotation) must be called before the methods setX and setY. Once the method SetRotation is called, setY and setX methods must be called even if it is to assign them to 0.

    4 - Creating the Body

    After all settings have been made, the method createBodyFromSprite (spr Sprite, boolean isStatic) must be called, because it's responsible for creating the body of the sprite by adding to it all of the the settings of the sprite. The boolean variable isStatic is responsible for creating a static body or non-static body. If static, the gravitational force will not act on the body. Below is an example from the initial stage (creating a window until the creation of the body of the sprite).

     Window win = new Window(800,600);
    Physics p = new Physics();
    p.createWorld(800,600);
    
    Sprite nave = new Sprite(“nave.png”);
    
    nave.setMass(1f);
    nave.setFriction(0.2f);
    nave.setRestitution(0f);
    
    nave.setRotation(Math.PI/4);
    
    nave.setX(200);
    nave.setY(100);
    
    
    p.createBodyFromSprite(nave,false).
    

    5 - Movement

    The movement of the sprite can be made using the keyboard or not. The movement of spirte using the keyboard is done by calling the method applyForceXFromKeyboard (for the X axis) and applyForceYFromKeyboard (for the Y axis). Both methods have four parameters to be passed on them, two keys and its behaviors.

    nave.applyForceXFromKeyboard((Keyboard.LEFT_KEY, Keyboard.RIGHT_KEY, 1,Keyboard.DETECT_EVERY_PRESS, 
    boolean boundsScreen);
    
    nave.applyForceYFromKeyboard(Keyboard.UP_KEY, Keyboard.DOWN_KEY, 90,Keyboard.DETECT_EVERY_PRESS, 
    boolean boundsScreen);	
    
    To move the sprite on the screen without using the keyboard, the methods applyForceX applyForceY shall be used. The parameters passed(parameters of type double) are just the amount of force to be applied in the sprite.
     nave.applyForceX(2);
     nave.applyForceY(0);
    

    UFF - Universidade Federal Fluminense - Institudo de Computação - Ciência da Computação