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>
#1 Members - Reputation: 127
Posted 07 July 2014 - 09:21 AM
#2 Members - Reputation: 127
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
#3 Members - Reputation: 127
Posted 07 July 2014 - 09:51 AM
Tried the thing in the quote and it doesn't work.
#4 Crossbones+ - Reputation: 6783
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.
#5 Members - Reputation: 2923
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.
#6 Prime Members - Reputation: 3320
Posted 07 July 2014 - 05:36 PM
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).
#7 Members - Reputation: 127
Posted 10 July 2014 - 03:29 AM
It's really that simple? Thanks a lot for clarifying Kryzon!
#8 Members - Reputation: 127
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;
}
}
#9 Crossbones+ - Reputation: 6152
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 ?
#10 Members - Reputation: 139
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.
#11 Prime Members - Reputation: 3320
Posted 11 July 2014 - 02:06 AM
#12 Crossbones+ - Reputation: 11142
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.
-=- Full "Retro Mortis" Series Article Index -=-
Retro Mortis - RTS (Part 1) - It was found in a Desert...
Retro Mortis - RTS (Part 2) - Then a Blizzard came...
#13 Crossbones+ - Reputation: 11142
Posted 11 July 2014 - 04:24 PM
-=- Full "Retro Mortis" Series Article Index -=-
Retro Mortis - RTS (Part 1) - It was found in a Desert...
Retro Mortis - RTS (Part 2) - Then a Blizzard came...
#14 Members - Reputation: 127
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.