Need help implementing shooting

This topic is 1650 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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>

Share on other sites

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

Share on other sites

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

Share on other sites

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.

Share on other sites

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

Share on other sites

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

Share on other sites

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

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;

}

}

Share on other sites

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

Share on other sites

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.

• What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 9
• 13
• 9
• 9
• 15
• Forum Statistics

• Total Topics
634076
• Total Posts
3015353
×

Important Information

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!