• Create Account

## Need help implementing shooting

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

13 replies to this topic

### #1klrr  Members

127
Like
0Likes
Like

Posted 07 July 2014 - 09:21 AM

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>

### #2klrr  Members

127
Like
2Likes
Like

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

### #3klrr  Members

127
Like
-1Likes
Like

Posted 07 July 2014 - 09:51 AM

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

### #4Buckeye  GDNet+

10739
Like
3Likes
Like

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.

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

### #5HappyCoder  Members

4738
Like
0Likes
Like

Posted 07 July 2014 - 03:30 PM

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

### #6Kryzon  Prime Members

4437
Like
5Likes
Like

Posted 07 July 2014 - 05:36 PM

POPULAR

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).

### #7klrr  Members

127
Like
0Likes
Like

Posted 10 July 2014 - 03:29 AM

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

### #8klrr  Members

127
Like
0Likes
Like

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.

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;

}

}

### #9unbird  Members

8298
Like
0Likes
Like

Posted 10 July 2014 - 05:49 AM

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

### #10Thinias  Members

178
Like
0Likes
Like

Posted 11 July 2014 - 01:58 AM

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.

### #11Kryzon  Prime Members

4437
Like
0Likes
Like

Posted 11 July 2014 - 02:06 AM

Please also post the move() method, it may be where the problem lies.

### #12Orymus3  Members

18543
Like
0Likes
Like

Posted 11 July 2014 - 09:18 AM

This is a usual math lib method that is a life saver
That's how I handled my bullets.

-=- My Articles -=-
Getting Games Done - Method and tools on how to start a hobby project and get it Done!

The Art of Enemy Design in Zelda: A Link to the Past - Reverse-engineering functional enemy design from applied example.

Retro Mortis - "RTS" - Article Series (4 Parts) on the history of RTS development (4th part finally released!!!)

### #13Orymus3  Members

18543
Like
0Likes
Like

Posted 11 July 2014 - 04:24 PM

also should call your function checkAlive (typo) and should not contain magic numbers (worst case make them a global constant for now so you keep your eyea on them)

-=- My Articles -=-
Getting Games Done - Method and tools on how to start a hobby project and get it Done!

The Art of Enemy Design in Zelda: A Link to the Past - Reverse-engineering functional enemy design from applied example.

Retro Mortis - "RTS" - Article Series (4 Parts) on the history of RTS development (4th part finally released!!!)

### #14klrr  Members

127
Like
0Likes
Like

Posted 13 July 2014 - 06:54 AM

Thanks for all help, the problem was that I used the mouse position in the screen resolution, not as game world coordinates.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.