Jump to content
  • Advertisement
Sign in to follow this  
klrr

Need help implementing shooting

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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

Share this post


Link to post
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 this post


Link to post
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.

 

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;
        
    }
    
}

Share this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!