Back to project page SpaceGame.
The source code is released under:
Copyright (c) 2012 ??ukasz Patalas Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the So...
If you think the Android project SpaceGame listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.lpatalas.spacegame; //w w w . j a va 2 s.co m import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.Sprite; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.math.Rectangle; import com.badlogic.gdx.math.Vector2; /** * User: Lukasz * Date: 11.08.12 */ class Player { private final Vector2 initialPosition; private final Vector2 position = new Vector2(); private Vector2 targetPosition; private final Texture texture; private final Vector2 velocity = new Vector2(0, 0); public Player(Texture texture, Vector2 initialPosition) { this.texture = texture; this.initialPosition = initialPosition; } public Rectangle getBoundingRectangle() { float width = texture.getWidth(); float height = texture.getHeight() / 2; return new Rectangle(position.x - width / 2, position.y - height / 2, width, height); } public Vector2 getPosition() { return position.cpy(); } public void reset() { position.set(initialPosition); targetPosition = null; velocity.set(0, 0); } public void moveTo(Vector2 position) { targetPosition = position.cpy(); } public void render(SpriteBatch spriteBatch) { spriteBatch.draw(texture, position.x - texture.getWidth() / 2, position.y - texture.getHeight() / 2); } public void update(float dt) { if (targetPosition != null) moveTowardsTarget(dt); } private void moveTowardsTarget(float dt) { final float mass = 1.0f; final float k = 200.0f; final float b = 200.0f; Vector2 toTarget = getVectorToTarget(); Vector2 damping = velocity.cpy().mul(b); Vector2 force = toTarget.mul(k).sub(damping); Vector2 acc = force.mul(dt * mass); velocity.add(acc.mul(dt)); float x = position.x + velocity.x * dt; float y = position.y + velocity.y * dt; position.set(x, y); } private Vector2 getVectorToTarget() { Vector2 toTarget = targetPosition.cpy(); toTarget.sub(getPosition()); return toTarget; } }