I want to make a top-down shooter but I am unsure how to write the shooting. I know how to get the angle but I don't know how to calculate how and what I should apply to the X and Y position to the bullet that spawns each frame, can anyone explain how you do this>

**2**

# Need help implementing shooting

###
#2
Members - Reputation: **123**

Posted 07 July 2014 - 09:27 AM

Nevermind, I found out:

"Don't use angles: Compute the vector from the shooter to the mouse and normalize it (i.e. divide it by its length). Then advance the position of the bullet by adding that vector multiplied by the bullet's speed." - Álvaro

###
#4
Crossbones+ - Reputation: **4013**

Posted 07 July 2014 - 10:07 AM

Tried the thing in the quote and it doesn't work.

Uh.. are you just making a statement, or is there some code you could post, maybe describing what "it" is? Alvaro's method is a common way to do that sort of thing, and the *method* works.

Please don't PM me with questions. Post them in the forums for *everyone's* benefit, and I can embarrass myself publicly.

###
#6
Prime Members - Reputation: **2694**

Posted 07 July 2014 - 05:36 PM

*shouldn't*use angles. It's just another way of getting the same result: a set of components for you to translate the bullets each frame.

You can calculate this with an angle or with a vector.

// Angle. Local deltaAngle:Float = Atan2( target.y - shooter.y, target.x - shooter.x ) Local velocityX:Float = Cos( deltaAngle ) * BULLET_SPEED Local velocityY:Float = Sin( deltaAngle ) * BULLET_SPEED

// Vector. Local deltaX:Float = target.x - shooter.x Local deltaY:Float = target.y - shooter.y Local length:Float = Sqr( deltaX * deltaX + deltaY * deltaY ) velocityX = ( deltaX / length ) * BULLET_SPEED velocityY = ( deltaY / length ) * BULLET_SPEED

These methods compute the same practical result, a velocity vector for the bullet. You only need to compute this when you create the bullet -- you preserve the same velocity vector while you move the bullet.

The difference between these methods is that the vector method is usually faster and more precise. It only uses one function call (the square root).

###
#8
Members - Reputation: **123**

Posted 10 July 2014 - 04:42 AM

Works kind of now. So it shoots bullets but they are shot in strange direction (not where I click). Here is code, java libgdx.

package com.deadsimple.zombiedown;

import com.badlogic.gdx.math.Vector2;

public class Bullet extends Sprite {

public Bullet(Vector2 start, Vector2 dest) {

super(start.x, start.y, 50, 50, 100);

float deltaX = dest.x - start.x;

float deltaY = dest.y - start.y;

float length = (float) Math.sqrt(deltaX * deltaX + deltaY * deltaY);

this.velocity.x = (deltaX / length) * this.SPEED;

this.velocity.y = (deltaY / length) * this.SPEED;

}

public void update(float dt) {

this.move();

chackAlive();

}

private void chackAlive() {

if (this.position.x > 480 || this.position.x < -50) this.alive = false;

if (this.position.y > 800 || this.position.y < -50) this.alive = false;

}

}