Need help implementing shooting

Started by
12 comments, last by klrr 9 years, 9 months ago

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>

Advertisement

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

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

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.

You don't forget how to play when you grow old; you grow old when you forget how to play.

Also, how does it not work. The way it is malfunctioning may be a clue was to what is wrong.

My current game project Platform RPG
It's not that you 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).

It's really that simple? Thanks a lot for clarifying Kryzon!

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;

}

}

Do you account for the size of the bullet ? Meaning, is it drawn so its center of mass (visually) is actually at this.position ?

From a directional perspective, the implementation presented here looks correct to me. I would check and see if the "dest" you passed in is actually where you thought it was.

This topic is closed to new replies.

Advertisement