# GMS 2get difference between two velocity's?

##### Member
can someone tell me how to get the velocity difference between vecA and vecB?

Code:
``````var _bullet_spd = 2
var _movex = lengthdir_x(speed, direction)
var _movey = lengthdir_y(speed, direction)
var _facex  = lengthdir_x(speed+_bullet_spd, image_angle)
var _facey  = lengthdir_y(speed+_bullet_spd, image_angle)
var _spd     = ???
create_bullet(x, y, _spd, image_angle)``````

#### Nux

##### GMC Member
YYG Staff
What exactly are you trying to do? those variables seem useless if you're basing your vector off a magnitude (speed) and direction (angle), shouldn't you just be able to go straight to:
Code:
``create_bullet(x, y, _bullet_spd, image_angle);``
...without all the hassle?

##### Member
I'm trying to add the bullet speed to the ships facing velocity not the ships moving velocity

so my ship was moving with a speed of 1 at direction 90 (up) and I am facing direction 0(right)
I want to add it to my facing velocity which would be a speed of 0 with a direction of 0 (right)

Last edited:

#### flyingsaucerinvasion

##### Member
What. How does a facing have a velocity? Velocity implies movement.

Your descriptiong is confusing. Are you trying to add the ship's velocity to the bullet's initial velocity? If so just add the ship's h and v speeds to the bullet's initial speed.

#### xDGameStudios

##### Member
can someone tell me how to get the velocity difference between vecA and vecB?

Code:
``````var _bullet_spd = 2
var _movex = lengthdir_x(speed, direction)
var _movey = lengthdir_y(speed, direction)
var _facex  = lengthdir_x(speed+_bullet_spd, image_angle)
var _facey  = lengthdir_y(speed+_bullet_spd, image_angle)
var _spd     = ???
create_bullet(x, y, _spd, image_angle)``````
Lets first start to try to understand what you want.... What is the facing velocity?!
This might be what you want (but you are asking it wrong).

Code:
``````var _bullet_spd = 2
// Decompose the bullet speed
var _bullet_x_velocity = lengthdir_x(_bullet_spd, image_angle);
var _bullet_y_velocity = lengthdir_y(_bullet_spd, image_angle);

// Decompose the movement velocity
var _movement_x_velocity  = lengthdir_x(speed, direction)
var _movement_y_velocity  = lengthdir_y(speed, direction)

// Add them up (sum of vectors) and calculate magnitude
var _spd_x = _bullet_x_velocity + _movement_x_velocity;
var _spd_y = _bullet_y_velocity + _movement_y_velocity;
var _spd = sqrt(_spd_x*_spd_x + _spd_y*_spd_y);

create_bullet(x, y, _spd, image_angle)``````

#### Amon

##### Member
Maybe he means shooting bullets from the ship at its current rotational vector. So, if it was rotated 30 degrees he'd possibly want the bullets to also fire in that same direction.

#### NightFrost

##### Member
I'm pretty sure OP is talking about asteroids-style controls where ship's velocity vector is moving it in certain direction but the player may have rotated it to point into another direction. Although, I think that type of games tend to ignore the ship's velocity and shoot bullets out at standard speed.

##### Member
@NightFrost your are right I am making a asteroids game
my ship is moving in one direction and I want to find out the ships speed in the direction it is facing and add the bullet speed to it

#### icuurd12b42

##### TMC Founder
GMC Elder
still the way this is phrased make little sense... anyway here's the standard bullet create in space ship games
Code:
``````//create a bullet
with(instance_create(x,y,bullet)) //end step code
//with(instance_create(x+hspeed,y+vspeed,bullet)) //step code
{
//set the bullet motion direction to ship facing direction (image angle assumed as facing)
direction = other.image_angle;
//set orientation to same
image_angle = direction;
//set initial velocity
speed = 8;
//add the ship velocity vector to the bullet velocity vector
hspeed+=other.hspeed;
vspeed+=other.vspeed;
}``````
hspeed and vspeed are related to direction and speed, they are the velocity vector. change any of those it affects the others...

#### Strawbry_jam

##### Member
my ship is moving in one direction and I want to find out the ships speed in the direction it is facing and add the bullet speed to it
Code:
``````var _bullet_spd = 2
var _movex = lengthdir_x(speed, direction) // or hspeed
var _movey = lengthdir_y(speed, direction) // or vspeed
var _facex = lengthdir_x(1, image_angle)
var _facey = lengthdir_y(1, image_angle)
var _spd   = dot_product(_movex,_movey,_facex,_facey)
create_bullet(x, y, _spd + _bullet_spd, image_angle)``````

#### flyingsaucerinvasion

##### Member
Code:
``````var _bullet_spd = 2
var _movex = lengthdir_x(speed, direction) // or hspeed
var _movey = lengthdir_y(speed, direction) // or vspeed
var _facex = lengthdir_x(1, image_angle)
var _facey = lengthdir_y(1, image_angle)
var _spd   = dot_product(_movex,_movey,_facex,_facey)
create_bullet(x, y, _spd + _bullet_spd, image_angle)``````
What is going on there?

#### Strawbry_jam

##### Member
What is going on there?
The dot product between the velocity vector and facing direction vector gives the speed moving in the direction of the facing direction. Then you just add the bullet speed to that.

#### icuurd12b42

##### TMC Founder
GMC Elder
The dot product between the velocity vector and facing direction vector gives the speed moving in the direction of the facing direction. Then you just add the bullet speed to that.
so the bullet does not exactly launch with the ship's velocity then, just some aspect of it?

#### Strawbry_jam

##### Member
so the bullet does not exactly launch with the ship's velocity then, just some aspect of it?
Yeah so if you're facing the direction you are moving, it just adds movement speed to the bullet. Facing 90 degrees will add nothing. Facing the opposite direction gives negative speed.

#### icuurd12b42

##### TMC Founder
GMC Elder
OK, yeah I see, no side motion on the bullet (unlike my example) but with some influence on the speed. I'll try to remember that one

#### flyingsaucerinvasion

##### Member
@Strawbry_jam but I don't understand what that accomplishes.

#### icuurd12b42

##### TMC Founder
GMC Elder
@Strawbry_jam but I don't understand what that accomplishes.
It adds velicity to the bullet, but not in a realistic fashion which can be annoying, causing bullets to shoot sideways (like my code would). Shooting sideways is not always desired. and having the bullet not have ship velocity applied can also be weird.

this is the middle ground between the 2