Android Open Source - killingspree Body






From Project

Back to project page killingspree.

License

The source code is released under:

MIT License

If you think the Android project killingspree listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.sillygames.killingSpree.managers.physics;
/* w  w w .  j a v a2  s. co m*/
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.sillygames.killingSpree.managers.WorldRenderer;
import com.sillygames.killingSpree.serverEntities.ServerEntity;
import com.sillygames.killingSpree.serverEntities.ServerPlayer;

public class Body {
    
    public enum BodyType { StaticBody, DynamicBody }
    public Rectangle rectangle;
    public BodyType bodyType;
    public ServerEntity entity;
    public float restitutionX;
    public float restitutionY;
    public boolean grounded;
    public boolean toDestroy;
    public enum CollisionCategory { ALL, ENEMY, STATIC, NONE, WEAPON };
    public CollisionCategory category;
    private final Vector2 velocity;
    private final Vector2 temp1;
    private final Vector2 temp2;
    private final Vector2 temp3;
    private final Vector2 temp4;
    private final Vector2 temp5;
    private World world;
    private float gravityScale;
    public float xDamping;
    
    public Body() {
        bodyType = BodyType.StaticBody;
        velocity = new Vector2(0, 0);
        temp1 = new Vector2();
        temp2 = new Vector2();
        temp3 = new Vector2();
        temp4 = new Vector2();
        temp5 = new Vector2();
        restitutionX = 0;
        restitutionY = 0;
        gravityScale = 1f;
        grounded = false;
        toDestroy = false;
        xDamping = 0;
        category = CollisionCategory.ALL;
    }
    
    public Body(Rectangle rectangle) {
        this();
        this.rectangle = rectangle;
    }
    
    public Body(float x, float y, float width, float height) {
        this();
        rectangle = new Rectangle(x, y, width, height);
    }

    public Body(float x, float y, float w, float h, BodyType type) {
        this(x, y, w, h);
        bodyType = type;
    }
    
    public Body(Rectangle rectangle, BodyType type) {
        this(rectangle);
        bodyType = type;
    }

    public void setWorld(World world) {
        this.world = world;
    }
    
    public Vector2 getPosition() {
        return rectangle.getCenter(temp1);
    }

    public Vector2 getLinearVelocity() {
        return temp3.set(velocity);
    }

    public void setLinearVelocity(float x, float y) {
        velocity.set(x, y);
    } 

    public void setLinearVelocity(Vector2 velocity) {
        this.velocity.set(velocity);
    }

    public void setTransform(Vector2 position, float angle) {
        rectangle.setCenter(position);
    }


    public void setGravityScale(float gravityScale) {
        this.gravityScale = gravityScale;
    }

    public void setUserData(ServerEntity entity) {
        this.entity = entity;
    }
    
    public ServerEntity getUserData() {
        return entity;
    }

    public void update(float delta) {
        
        float xOffset = 0;
        
        if (toDestroy) {
            return;
        }
        velocity.x -= (xDamping * delta * velocity.x);
        
        grounded = false;
        // Calculate velocity
        temp2.set(world.gravity);
        temp2.scl(gravityScale);
        temp2.scl(delta);
        velocity.add(temp2);
        temp2.set(velocity);
        temp2.scl(delta);

        if (rectangle.x + rectangle.width > WorldRenderer.VIEWPORT_WIDTH 
                && velocity.x > 0) {
            xOffset = -WorldRenderer.VIEWPORT_WIDTH;
        } else if (rectangle.x < 0 && velocity.x < 0) {
            xOffset = WorldRenderer.VIEWPORT_WIDTH;
        }
        rectangle.x += xOffset;
        // Update position
        rectangle.getPosition(temp1);
        temp1.add(temp2);
        rectangle.getPosition(temp2);
        rectangle.getPosition(temp4);
        temp2.sub(temp1);
        
        if (Math.abs(velocity.y) > 5) {
            rectangle.setPosition(rectangle.x, temp1.y);
            for (Body body: world.bodies) {
                if (toDestroy || body == this || body.toDestroy ||
                        (body.category != CollisionCategory.ALL &&
                        body.category == category) 
                        || body.category ==CollisionCategory.NONE ||
                        (category == CollisionCategory.NONE && 
                        body.bodyType !=BodyType.StaticBody))
                    continue;
                if (body.rectangle.overlaps(rectangle)) {
                    solveVerticalCollision(body, temp1);
                }
            }
        }
        rectangle.setPosition(temp1.x, rectangle.y);
        for (Body body: world.bodies) {
            if (toDestroy || body == this || body.toDestroy ||
                    (body.category != CollisionCategory.ALL &&
                    body.category == category)
                    || body.category ==CollisionCategory.NONE ||
                    (category == CollisionCategory.NONE && 
                    body.bodyType !=BodyType.StaticBody))
                continue;
            if (body == this || body.toDestroy)
                continue;
            if (body.rectangle.overlaps(rectangle)) {
                solveHorizontalCollision(body, temp1);
            }
        }
        
        if (Math.abs(velocity.y) <= 5) {
            rectangle.setPosition(rectangle.x, temp1.y);
            for (Body body: world.bodies) {
                if (toDestroy || body == this || body.toDestroy ||
                        (body.category != CollisionCategory.ALL &&
                        body.category == category) 
                        || body.category ==CollisionCategory.NONE ||
                        (category == CollisionCategory.NONE && 
                        body.bodyType !=BodyType.StaticBody))
                    continue;
                if (body.rectangle.overlaps(rectangle)) {
                    solveVerticalCollision(body, temp1);
                }
            }
        }        
        rectangle.x -= xOffset;
    }

    public void applyLinearImpulse(float x, float y) {
        temp1.set(velocity);
        velocity.set(temp1.x + x, temp1.y + y);
    }
    
    public void solveHorizontalCollision(Body body, Vector2 temp1) {
        if (velocity.x < 0) {
            float x = body.rectangle.x + body.rectangle.width + 0.01f;
            temp1.x = Math.abs(temp4.x - x) < Math.abs(temp4.x - temp2.x) ?
                    x : temp4.x;
            velocity.x *= -restitutionX;
            CollisionProcessor.touchLeft(this, body);
        } else if(velocity.x > 0){
            float x = body.rectangle.x - rectangle.width - 0.01f;
            temp1.x = Math.abs(temp4.x - x) < Math.abs(temp4.x - temp2.x) ? x : temp4.x;
            velocity.x *= -restitutionX;
            CollisionProcessor.touchRight(this, body);
        }
        rectangle.setPosition(temp1.x, rectangle.y);
    }
    
    public void solveVerticalCollision(Body body, Vector2 position) {
        temp5.set(position);
        if (velocity.y > 0){
            float y = body.rectangle.y - rectangle.height - 0.01f;
            position.y = Math.abs(temp4.y - y) < Math.abs(temp4.y - temp2.y) ?
                    y: temp4.y;
            velocity.y *= -restitutionY;
            CollisionProcessor.jumpedOn(this, body);
        } else if(velocity.y <= 0) {
            float y = body.rectangle.y + body.rectangle.height + 0.01f;
            position.y = Math.abs(temp4.y - y) < Math.abs(temp4.y - temp2.y) ?
                    y: temp4.y;
            velocity.y *= -restitutionY;
            if (body.bodyType == BodyType.StaticBody) {
                grounded = true;
            } else {
                grounded = false;
            }
            CollisionProcessor.jumpOn(this, body);
        }
        rectangle.setPosition(rectangle.x, position.y);
    }

}




Java Source Code List

com.sillygames.killingSpree.AndroidLauncher.java
com.sillygames.killingSpree.KillingSpree.java
com.sillygames.killingSpree.PlatformServices.java
com.sillygames.killingSpree.categories.EnemyCategory.java
com.sillygames.killingSpree.categories.ExplodingWeaponCategory.java
com.sillygames.killingSpree.categories.LivingCategory.java
com.sillygames.killingSpree.categories.NonExplodingWeaponCategory.java
com.sillygames.killingSpree.clientEntities.ClientArrow.java
com.sillygames.killingSpree.clientEntities.ClientBlob.java
com.sillygames.killingSpree.clientEntities.ClientBomb.java
com.sillygames.killingSpree.clientEntities.ClientBullet.java
com.sillygames.killingSpree.clientEntities.ClientEntity.java
com.sillygames.killingSpree.clientEntities.ClientFly.java
com.sillygames.killingSpree.clientEntities.ClientFrog.java
com.sillygames.killingSpree.clientEntities.ClientPlayer.java
com.sillygames.killingSpree.clientEntities.ClientTestPlayer.java
com.sillygames.killingSpree.controls.InputController.java
com.sillygames.killingSpree.controls.onScreenControls.java
com.sillygames.killingSpree.desktop.DesktopLauncher.java
com.sillygames.killingSpree.desktop.DesktopServices.java
com.sillygames.killingSpree.helpers.EntityUtils.java
com.sillygames.killingSpree.helpers.Event.java
com.sillygames.killingSpree.helpers.MyButton.java
com.sillygames.killingSpree.helpers.MyConnection.java
com.sillygames.killingSpree.helpers.Utils.java
com.sillygames.killingSpree.managers.LevelLoader.java
com.sillygames.killingSpree.managers.WorldBodyUtils.java
com.sillygames.killingSpree.managers.WorldManager.java
com.sillygames.killingSpree.managers.WorldRenderer.java
com.sillygames.killingSpree.managers.physics.Body.java
com.sillygames.killingSpree.managers.physics.CollisionProcessor.java
com.sillygames.killingSpree.managers.physics.Ray.java
com.sillygames.killingSpree.managers.physics.WorldDebugRenderer.java
com.sillygames.killingSpree.managers.physics.World.java
com.sillygames.killingSpree.networking.ControlsSender.java
com.sillygames.killingSpree.networking.NetworkRegisterer.java
com.sillygames.killingSpree.networking.StateProcessor.java
com.sillygames.killingSpree.networking.messages.AudioMessage.java
com.sillygames.killingSpree.networking.messages.ClientDetailsMessage.java
com.sillygames.killingSpree.networking.messages.ConnectMessage.java
com.sillygames.killingSpree.networking.messages.ControlsMessage.java
com.sillygames.killingSpree.networking.messages.EntityState.java
com.sillygames.killingSpree.networking.messages.GameStateMessage.java
com.sillygames.killingSpree.networking.messages.PlayerNamesMessage.java
com.sillygames.killingSpree.networking.messages.ServerStatusMessage.java
com.sillygames.killingSpree.pool.AssetLoader.java
com.sillygames.killingSpree.pool.EntityPool.java
com.sillygames.killingSpree.pool.MessageObjectPool.java
com.sillygames.killingSpree.pool.Pool.java
com.sillygames.killingSpree.pool.Poolable.java
com.sillygames.killingSpree.renderers.HUDRenderer.java
com.sillygames.killingSpree.screens.AbstractScreen.java
com.sillygames.killingSpree.screens.ClientDiscoveryScreen.java
com.sillygames.killingSpree.screens.GameScreen.java
com.sillygames.killingSpree.screens.LobbyScreen.java
com.sillygames.killingSpree.screens.MainMenuScreen.java
com.sillygames.killingSpree.screens.OptionsScreen.java
com.sillygames.killingSpree.screens.SplashScreen.java
com.sillygames.killingSpree.screens.settings.Constants.java
com.sillygames.killingSpree.serverEntities.ServerArrow.java
com.sillygames.killingSpree.serverEntities.ServerBlob.java
com.sillygames.killingSpree.serverEntities.ServerBomb.java
com.sillygames.killingSpree.serverEntities.ServerBullet.java
com.sillygames.killingSpree.serverEntities.ServerEntity.java
com.sillygames.killingSpree.serverEntities.ServerFly.java
com.sillygames.killingSpree.serverEntities.ServerFrog.java
com.sillygames.killingSpree.serverEntities.ServerPlayer.java
com.sillygames.killingSpree.sound.SFXPlayer.java