How to handle multiple shots with different directions in libGDX? - java

I am programming a game with libGDX. The protagonist can shoot at some flying enemies.
Heres my problem:
I don't know how I can handle multiple shots at the same time. Here is my code:
private Image backgroundImage = new Image(backgroundTexture);
private Stage levelStage = new Stage(), menuStage = new Stage();
private Table menuTable = new Table();
private Skin menuSkin = Manager.menuSkin;
private OrthographicCamera camera;
private SpriteBatch batch;
private Rectangle farmer, ufo;
private Array<Rectangle> lasers;
private boolean ufoMovementLeft = true;
private boolean leftArrow = false;
private boolean rightArrow = false;
private float laserMovement[] = new float[100];
private int ufoLife = 15;
#Override
public void show() {
levelStage.addActor(backgroundImage);
Gdx.input.setInputProcessor(levelStage);
camera = new OrthographicCamera();
camera.setToOrtho(false, 800, 480);
batch = new SpriteBatch();
farmer = new Rectangle();
farmer.x = 480 / 2 - 56 / 2; farmer.y = 80;
farmer.width = 55; farmer.height = 140;
ufo = new Rectangle();
ufo.x = 500 / 2; ufo.y = 600;
ufo.width = 160; ufo.height = 70;
lasers = new Array<Rectangle>();
}
public void spawnLaser() {
Rectangle laser = new Rectangle();
if(Gdx.input.justTouched()) {
lasers.add(laser);
Vector3 touchPos = new Vector3();
touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
laserMovement[lasers.size] = touchPos.x;
camera.unproject(touchPos);
laser.x = farmer.x + 50; laser.y = 110;
laser.width = 25; laser.height = 20;
}
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
levelStage.act();
levelStage.draw();
batch.setProjectionMatrix(levelStage.getCamera().combined);
batch.begin();
movement();
if(leftArrow) {if(farmer.x >= 50) farmer.x -= 2;
batch.draw(farmerLeftTexture, farmer.x, farmer.y);}
else if(rightArrow) {if(farmer.x <= 370) farmer.x += 2;
batch.draw(farmerRightTexture, farmer.x, farmer.y);}
else batch.draw(farmerBackTexture, farmer.x, farmer.y);
if(ufoMovementLeft) {ufo.x -= 1;
if(ufo.x < 50) ufoMovementLeft = false; }
else {ufo.x += 1; if(ufo.x > 300) ufoMovementLeft = true ; }
batch.draw(ufoTexture, ufo.x, ufo.y);
if(Gdx.input.justTouched()) spawnLaser();
Iterator<Rectangle> iter = lasers.iterator();
while(iter.hasNext()) {
Rectangle laser = iter.next();
laser.y += 300 * Gdx.graphics.getDeltaTime();
laser.x += (laserMovement[lasers.size] - farmer.x + farmer.x * 0.25) * 2 * Gdx.graphics.getDeltaTime();
if(laser.overlaps(ufo)){iter.remove(); ufoLife -= 1;}
if(laser.y + 64 > 550) iter.remove();
}
for(Rectangle laser: lasers) {
batch.draw(laserTexture, laser.x, laser.y);
}
The problem at this point is that the shots are handled "as if they were one". If the player shoots in a different direction than the other shots before, every bullet that is on the screen at the moment changes its direction.
I just have no idea how to solve this problem, so every help is appreciated :).
Cheers, Joshflux
EDIT: I edited the code, and here are some screenshots:
The first one shows to shots fired to the left side, but in the second one you can see that after i fired two shots to the right side, the first two shots also start to move to the right.

Related

Java libGDX different sizes of animation frames

How to deal with different sizes of frames? If I setBounds(0, 0, 100, 100) in constructor then some frames are smaller but if I update it then frames are moved to left and up. How to make that every frame will be in the same place?
mayby you can see it
http://imgur.com/a/AN8Gc
public class Player extends Sprite{
//floats
private float animationTimer;
//box2d variables
public World world;
public Body body;
//enums
public enum State{STANDING, MOVING, SHOOTING, RELOAD, MALEE_ATTACK}
public State currentState;
public State previousState;
//booleans
boolean shoot;
boolean reload;
boolean maleeAttack;
private TextureRegion region;
public PlayScreen playScreen;
public Player(PlayScreen playScreen){
this.playScreen = playScreen;
this.world = playScreen.getWorld();
definePlayer();
animationTimer = 0;
region = Assets.instance.playerAssets.idleAniamtion.getKeyFrame(animationTimer);
setRegion(region);
setBounds(0, 0, getRegionWidth() / Constants.PPM, getRegionHeight() / Constants.PPM);
setPosition(0, 0);
currentState = State.STANDING;
previousState = State.STANDING;
shoot = false;
reload = false;
maleeAttack = false;
}
public void definePlayer(){
BodyDef bodyDef = new BodyDef();
bodyDef.position.set(100 / Constants.PPM, 100 / Constants.PPM);
bodyDef.type = BodyDef.BodyType.DynamicBody;
body = world.createBody(bodyDef);
FixtureDef fixtureDef = new FixtureDef();
CircleShape shape = new CircleShape();
shape.setRadius(50 / Constants.PPM);
fixtureDef.shape = shape;
body.createFixture(fixtureDef).setUserData(this);
body.setLinearDamping(Constants.LINEAR_DAMPING);
}
public void update(float delta){
region = Assets.instance.playerAssets.idleAniamtion.getKeyFrame(animationTimer);
setRegion(getFrame(delta));
moving();
//rotate();
}
public void moving(){
setPosition(body.getPosition().x - getWidth() / 2, body.getPosition(). y - getHeight() / 2);
//setBounds(0, 0, getRegionWidth() / Constants.PPM, getRegionHeight() / Constants.PPM); update bounds
if (input.isKeyPressed(Input.Keys.W) && body.getLinearVelocity().y < 5){
body.applyLinearImpulse(new Vector2(0, 1), body.getWorldCenter(), true);
}
if (input.isKeyPressed(Input.Keys.S) && body.getLinearVelocity().y > -5){
body.applyLinearImpulse(new Vector2(0, -1), body.getWorldCenter(), true);
}
if (input.isKeyPressed(Input.Keys.D) && body.getLinearVelocity().x < 5){
body.applyLinearImpulse(new Vector2(1, 0), body.getWorldCenter(), true);
}
if (input.isKeyPressed(Input.Keys.A) && body.getLinearVelocity().x > -5){
body.applyLinearImpulse(new Vector2(-1, 0), body.getWorldCenter(), true);
}
if (Gdx.input.isKeyPressed(Input.Keys.R)){
reload = true;
}
if (Gdx.input.isKeyPressed(Input.Keys.F)){
maleeAttack = true;
}
if (Gdx.input.isButtonPressed(Input.Buttons.LEFT)){
shoot = true;
}
}
public TextureRegion getFrame(float delta){
TextureRegion region;
currentState = getState();
switch (currentState){
case MOVING:
region = Assets.instance.playerAssets.moveAnimation.getKeyFrame(animationTimer);
break;
case SHOOTING:
maleeAttack = false;
region = Assets.instance.playerAssets.shootAniamtion.getKeyFrame(animationTimer);
if (Assets.instance.playerAssets.shootAniamtion.isAnimationFinished(animationTimer)){
shoot = false;
}
break;
case RELOAD:
region = Assets.instance.playerAssets.reloadAnimation.getKeyFrame(animationTimer);
if (Assets.instance.playerAssets.reloadAnimation.isAnimationFinished(animationTimer)){
reload = false;
}
break;
case MALEE_ATTACK:
region = Assets.instance.playerAssets.maleeAttackAnimation.getKeyFrame(animationTimer);
if (Assets.instance.playerAssets.maleeAttackAnimation.isAnimationFinished(animationTimer)) {
maleeAttack = false;
}
break;
default:
region = Assets.instance.playerAssets.idleAniamtion.getKeyFrame(animationTimer);
break;
}
animationTimer = currentState == previousState ? animationTimer + delta : 0;
previousState = currentState;
return region;
}
public State getState(){
if ((body.getLinearVelocity().x > 1 || body.getLinearVelocity().x < -1 || body.getLinearVelocity().y > 1 || body.getLinearVelocity().y < - 1) && !reload && !shoot && !maleeAttack){
return State.MOVING;
}
if (shoot && !reload){
return State.SHOOTING;
}
if (reload && !maleeAttack){
return State.RELOAD;
}
if (maleeAttack){
return State.MALEE_ATTACK;
}
else {
return State.STANDING;
}
}
public Vector2 getMousePosition(){
Vector2 mousePosition;
mousePosition = playScreen.getViewport().unproject(new Vector2(Gdx.input.getX(), Gdx.input.getY()));
return mousePosition;
}
public float getMouseAngle(){
float angle = (float) Math.atan2(getMousePosition().y - body.getPosition().y, getMousePosition().x - body.getPosition().x);
return angle;
}
public void rotate(){
setOrigin(getWidth() / 2, getHeight() / 2 );
setRotation((float) (getMouseAngle() * (180/Math.PI)));
body.setTransform(body.getPosition(), getMouseAngle());
}
}
To avoid the resizing of the texture just create a variable size used to set the bounds. The bound needs to set every frame.
public class Player extends Sprite {
private Body body;
private Vector2 size;
public Player(){
this.size = new Vector2( getRegionWidth() / Constants.PPM, getRegionHeight() / Constants.PPM );
}
public void update( float delta ){
Vector2 position = body.getPosition();
setRegion( getFrame( delta ) );
setRotation( MathUtils.radDeg * body.getAngle() );
setBounds( position.x, position.y, size.x, size.y );
setOriginCenter();
}
public void rotate(){
this.body.setTransform( body.getPosition(), getMouseAngle() );
}
}
You need to use the additional data information a TextureAtlas provides as an AtlasRegion.
One complete animation should have the same (width/height) size for each keyframe. Pick a size where the biggest keyfarme fits into. Also be aware of positioning correctly. Have the same pivot point for each keyframe.
To avoid wasting space when packing your TextureAtlas enable "trim" function. Should be supported by any TexturePacker Tool out there.
The texture data file (for libGDX) then has entries like this:
walk_animation
rotate: false
xy: 794, 235
size: 86, 109
orig: 160, 170
offset: 37, 22
index: 5
Use the AtlasRegion to draw things at the correct size and position:
float width = 400; // pick your size here
float height = 300;
float offsetPctX = atlasRegion.offsetX / atlasRegion.originalWidth;
float offsetPctY = atlasRegion.offsetY / atlasRegion.originalHeight;
float scaleWidth = (float) atlasRegion.packedWidth / atlasRegion.originalWidth;
float scaleHeight = (float) atlasRegion.packedHeight / atlasRegion.originalHeight;
float drawWidth = width * scaleWidth;
float drawHeight = height * scaleHeight;
float regionOffsetX = offsetPctX * width;
float regionOffsetY = offsetPctY * height;
float drawScaleX = 1; // adjust to your needs
float drawScaleY = 1;
float drawOriginX = 0; // adjust to tour needs
float drawOriginY = 0;
float drawRotation = false;
float x = 100 + offsetX + regionOffsetX; // adjust to your needs
float y = 100 + offsetY + regionOffsetY;
spriteBatch.draw(atlasRegion, x, y, drawOriginX, drawOriginY, drawWidth, drawHeight, drawScaleX, drawScaleY, drawRotation);

How to make sprite bounce of the sides of the screen

I'm using libgdx in java android studio. i have just started. i'm working on android phone. i not using any cameras. all i want is a sprite bounce of all four sides of the screen without tapping. i tried many codes i thought would work but nope. i hope u guys can help me. I'm expecting an answer soon as possible. Thanks
this is what i have:
SpriteBatch batch;
Texture background;
Sprite backgroundsprite;
Sprite ballsprite;
Texture line;
Texture ballimg;
BitmapFont credits;
BitmapFont input;
BitmapFont play;
float dt;
String string = "";
float ballx;
float bally;
float speedx;
float speedy;
Rectangle screenrect;
Rectangle ballrect;
float screenLeft ;
float screenBottom ;
float screenTop ;
float screenRight ;
#Override
public void create() {
batch = new SpriteBatch();
speedx = 5f * dt;
speedy = 5f * dt;
createsprite();
createbackground();
createtext();
ballx = ballsprite.getX();
bally = ballsprite.getY();
}
#Override
public void render() {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
dt = Gdx.graphics.getDeltaTime();
ballsprite.setPosition(ballx + speedx,ballsprite.getY());
ballsprite.translateX(speedx);
float left = ballrect.getX();
float bottom = ballrect.getY();
float top = bottom + ballrect.getHeight();
float right = left + ballrect.getWidth();
if(left < screenLeft) {
string = "left";
speedx = 5f*dt;
}
if(right > screenRight)
{
string = "right";
speedx = -5f*dt;
}
if(bottom < screenBottom)
{
string = "bottom";
}
if(top > screenTop)
{
string = "top";
}
batch.begin();
backgroundsprite.draw(batch);
ballsprite.draw(batch);
rendertext();
batch.end();
}
public void createbackground() {
background = new Texture("images/BackgroundGodwin.jpg");
backgroundsprite = new Sprite(background);
backgroundsprite.setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
screenrect = new Rectangle(0,0,Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
screenLeft = screenrect.getX();
screenBottom = screenrect.getY();
screenTop = screenBottom + screenrect.getHeight();
screenRight = screenLeft + screenrect.getWidth();
}
public void createsprite() {
ballimg = new Texture("images/SpriteGodwin.png");
ballsprite = new Sprite(ballimg);
ballsprite.setScale(0.65f);
ballsprite.setPosition(Gdx.graphics.getWidth()/3,Gdx.graphics.getHeight()/2);
ballrect = new Rectangle(ballsprite.getBoundingRectangle());
}
#Override
public void dispose() {
batch.dispose();
ballimg.dispose();
background.dispose();
credits.dispose();
play.dispose();
input.dispose();
line.dispose();
}
public void createtext(){
play = new BitmapFont(Gdx.files.internal("fonts/realfont.fnt"));
play.setColor(com.badlogic.gdx.graphics.Color.GOLD);
credits = new BitmapFont(Gdx.files.internal("fonts/realfont.fnt"));
credits.setColor(com.badlogic.gdx.graphics.Color.GOLD);
input = new BitmapFont(Gdx.files.internal("fonts/realfont.fnt"));
input.setColor(com.badlogic.gdx.graphics.Color.OLIVE);
}
public void rendertext() {
credits.draw(batch, "Maded", Gdx.graphics.getWidth() / 7 - 50, Gdx.graphics.getHeight() / 9);
play.draw(batch, "Touch the Screen to play!!", Gdx.graphics.getWidth() / 2 - 175, Gdx.graphics.getHeight() - 80);
input.draw(batch, string, Gdx.graphics.getWidth() / 2 - 160, Gdx.graphics.getHeight() - 120);
}
}
I made a very simple version of what you want:
public class BouncyGame extends ApplicationAdapter {
SpriteBatch batch;
Texture ball;
float speedX = 3f;
float speedY = 3f;
int x;
int y;
#Override
public void create () {
batch = new SpriteBatch();
ball = new Texture("ball.png");
x = Gdx.graphics.getWidth()/2;
y = Gdx.graphics.getHeight()/2;
}
#Override
public void render () {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
//When the ball's x position is on either side of the screen.
//The width of the sprite is taken into account.
if (x > Gdx.graphics.getWidth() - ball.getWidth()/2 || x < 0 + ball.getWidth()/2) {
//Here we flip the speed, so it bonces the other way.
speedX = -speedX;
}
//Same as above, but with on the y-axis.
if (y > Gdx.graphics.getHeight() - ball.getHeight()/2 || y < 0 + ball.getHeight()/2) {
speedY = -speedY;
}
//Move the ball according to the speed.
x += speedX;
y += speedY;
batch.begin();
//Draw the ball so the center is at x and y. Normally it would be drawn from the lower left corner.
batch.draw(ball, x - ball.getWidth()/2, y - ball.getHeight()/2);
batch.end();
}
}
It will result in the following:
http://gfycat.com/TatteredCarefreeHapuku
There are numerous ways to improve this code, you could for example use vectors, and I wouldn't recommend using it in your final product, but it might help you figure out how to do something like this for your own project.

Java: difficulties making fade out animation run without being refreshed by processing

I have a simple animation play of a circle scale outwards and fade out whenever the applications detects that the hammer object has reached a certain angle. However, as it is currently implemented the animation will only play once. I have attempted using for loops to allow the animation to complete, but this causes the entire application to pause while the code is looped, and other attempts I have made have not worked, so any help at all would be appreciated.
import oscP5.*;
import netP5.*;
OscP5 oscP5;
NetAddress myRemoteLocation;
float bx;
float by;
int boxSizeX = 160;
int boxSizeY = 30;
boolean overBox = true;
boolean locked = false;
float xOffset = 0.0;
float yOffset = 0.0;
float angle = 4.70;
float cooldown;
int alphaVal = 255;
int colorchange = 0;
//for sound effect
float a = 0.0;
float s = 0.0;
BeatBall b1 = new BeatBall(210,425,60, 1);
BeatBall b2 = new BeatBall(570,395,60, 2);
Hammer h = new Hammer(135, -67, boxSizeY+25, boxSizeX-25, 1);
void setup()
{
ellipseMode(CENTER);
size(800, 600);
smooth();
frameRate(120);
bx = width/2.0;
by = height/2.0;
oscP5 = new OscP5(this,12001);
/* myRemoteLocation is a NetAddress. a NetAddress takes 2 parameters,
* an ip address and a port number. myRemoteLocation is used as parameter in
* oscP5.send() when sending osc packets to another computer, device,
* application. usage see below. for testing purposes the listening port
* and the port of the remote location address are the same, hence you will
* send messages back to this sketch.
*/
myRemoteLocation = new NetAddress("127.0.0.1",12000);
}
void draw()
{
background(0);
ellipseMode(CENTER);
float mx = constrain(angle, -2.6075916, -0.5207284);
///////////////////////////
//THIS IS THE ANIMATION //
//////////////////////////
if(locked)
{
a = a + 0.02;
s = cos(a)*10;
pushMatrix();
translate(210,425);
scale(s);
fill(colorchange, 30, 150, alphaVal);
ellipse(0, 0, 50, 50);
alphaVal = alphaVal - 3;
colorchange = colorchange + 3;
popMatrix();
}
pushMatrix();
translate(400, 425);
rotate(mx);
fill(222,223,255);
h.displayHammer();
rect(-25, -15, boxSizeX, boxSizeY);
popMatrix();
pushMatrix();
translate(595, 390);
rotate(radians(58));
fill(122,78,163);
rect(0, 0, 50, 50);
popMatrix();
b1.displayBall();
//b2.displaySquare();
cooldown = cooldown + 0.01;
println(cooldown);
if(mx == -2.6075916)
{
if(cooldown > 0.5)
{
locked = true;
soundEffect();
b1.collide();
cooldown = 0;
}
}
else if(mx == -0.5207284)
{
if(cooldown > 0.5)
{
b1.collide();
cooldown = 0;
}
}
}
void soundEffect()
{
}
void mousePressed()
{
xOffset = mouseX-bx;
yOffset = mouseY-by;
}
void mouseDragged()
{
angle = atan2(mouseY - 400, mouseX - 400);
println(angle);
}

Sprites are drawing at the wrong, but right location

public void render() {
if (TimeUtils.nanoTime() - lastSpawn > 2100000000)
chooseLane();
}
private void spawnCar() {
Rectangle car2 = new Rectangle();
float Pos123 = MathUtils.random(0, 2);
if (Pos123 == 0) {
car2.x = 60;
}
if (Pos123 == 1) {
car2.x = 180;
}
if (Pos123 == 2) {
car2.x = 300;
}
car = new Rectangle();
car.width = 100;
car.height = 172;
car2.y = 1000;
car2.setWidth(100);
car2.setHeight(172);
carb.add(car2);
lastSpawn = TimeUtils.nanoTime();
}
The sprites are drawing at one of these three locations every time but when I do a System.out.println(Pos123) the Sprite doesn't correlate to the actually lane that it has spawned in. This is also throwing the collision detector out of whack because it's triggering in the wrong lanes

How to unbind a sprite in an updating vector?

How to unbind an updating vector to a sprite? I am using Libgdx framework. here is my code
public class VectorSample extends GDX_TEST implements InputProcessor {
Texture ball,bullet,guider;
OrthographicCamera camera;
Vector2 vector = new Vector2();
Vector2 vectoralt = new Vector2();
long lastDropTime;
SpriteBatch batch;
Rectangle rect = new Rectangle();
float angle = 0,anglealt = 0;
private ShapeRenderer renderer;
TextureRegion tr;
Sprite sprite,sprite2;
ShapeRenderer sr;
Ship ship;
BallShit shit;
Vector2 nvec;
protected Vector2 center = new Vector2();
Array<Vector2> bullets;
Array<Rectangle> bulletsRect;
Boolean fire = false;
Rectangle tmpRect = new Rectangle();
#Override
public void create() {
renderer = new ShapeRenderer();
ball = new Texture(Gdx.files.internal("data/player.png"));
bullet = new Texture("data/ball_black.png");
tr = new TextureRegion(ball);
camera = new OrthographicCamera();
camera.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
ship = new Ship(new Vector2(5, 50), 1, 1, 0, 5f);
sr = new ShapeRenderer();
batch = new SpriteBatch();
Gdx.input.setInputProcessor(this);
rect.width = ball.getWidth();
rect.height = ball.getHeight();
vector.add(200,200);
sprite = new Sprite(ball);
vectoralt.add(200 + sprite.getWidth(),200);
shit = new BallShit(new Vector2(10,0),50f,50f);
bullets = new Array<Vector2>();
getCenter();
bulletsRect = new Array<Rectangle>();
fireatwill2();
}
#Override
public void render() {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
camera.update();
batch.setProjectionMatrix(camera.combined);
batch.begin();
{
batch.draw(sprite,vector.x,vector.y,sprite.getWidth()/2,sprite.getHeight()/2,sprite.getWidth(),sprite.getHeight(),1,1,angle);
if(fire == true){
for(Rectangle raindrop: bulletsRect) {
batch.draw(bullet,raindrop.x,raindrop.y);
}
}
}
batch.end();
if(Gdx.input.isKeyPressed(Input.Keys.D)){
vector.add(1,0);
if(vector.x > Gdx.graphics.getWidth()){
Gdx.app.log("x","x");
}
}
if(Gdx.input.isKeyPressed(Input.Keys.A)){
vector.add(-1,0);
if(vector.x < 0){
Gdx.app.log("x","-x");
}
}
float w = camera.frustum.planePoints[1].x - camera.frustum.planePoints[0].x;
float distance = w - (vector.x + sprite.getWidth());
if(distance <=0){
vector.x = w - sprite.getWidth();
}
Gdx.app.log("camera x" , " " + distance);
if(distance >= Gdx.graphics.getWidth() - sprite.getWidth()) {
vector.x = 0;
}
if(Gdx.input.isButtonPressed(Input.Buttons.LEFT)){
fire = true;
if(TimeUtils.nanoTime() - lastDropTime > 100000000) fireatwill2();
}
Iterator<Rectangle> iter = bulletsRect.iterator();
while(iter.hasNext()) {
Rectangle raindrop = iter.next();
double angletry = getAngle() * MathUtils.degreesToRadians;
float speed = 5;
float scale_x = (float)Math.cos(angletry);
float scale_y = (float)Math.sin(angletry);
float velocity_x = (speed* scale_x);
float velocity_y = (speed* scale_y);
raindrop.x += velocity_x;
raindrop.y += velocity_y;
if(raindrop.y < 0) iter.remove();
if(raindrop.y > Gdx.graphics.getHeight() || raindrop.x > Gdx.graphics.getWidth()) iter.remove();
}
//getting the angle
float angle = findAngle(Gdx.input.getX(),
Gdx.graphics.getHeight() - Gdx.input.getY());
this.angle = angle % 360;
}
private float getAngle(){
float angle = findAngle(Gdx.input.getX(),
Gdx.graphics.getHeight() - Gdx.input.getY());
return angle;
}
private void fireatwill2() {
Rectangle raindrop = new Rectangle();
raindrop.x = vector.x + sprite.getWidth() / 2 - (bullet.getWidth() / 2);
raindrop.y = vector.y + sprite.getHeight() / 2 - (bullet.getHeight() / 2);
bulletsRect.add(raindrop);
lastDropTime = TimeUtils.nanoTime();
}
public Vector2 getCenter() {
center.x = vector.x + sprite.getWidth() / 2;
center.y = vector.y + sprite.getHeight() / 2;
return center.cpy();
}
#Override
public void pause() {
super.pause(); //To change body of overridden methods use File | Settings | File Templates.
}
#Override
public void resume() {
super.resume(); //To change body of overridden methods use File | Settings | File Templates.
}
public float findAngle(float x1, float y1) {
Vector2 center = getCenter();
float x0 = center.x;
float y0 = center.y;
float a = MathUtils.atan2(y1 - y0, x1 - x0);
return a * MathUtils.radiansToDegrees;
}
}
and here is the running demo and source is here
I cant explain it well but if you could run the demo u will get what i am saying.
Im really stuck in here.. Thanks.

Categories