# Throwing an object while running

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

## Recommended Posts

Hey Guys, Working on a little shooter game and want to tweak the bullets to behave a little more realistically. Basically, I would like them to take into account the player's (the player is the one shooting the bullets) current velocity. Like if you are riding in a car at 30 km/hr and throw a ball from the window straight forward at 2 km/hr, the ball would have an inital velocity of 32 km/hr (or something, I'm just basing this on logic; math isn't my thing). Right now, when the player shoots, I simply create a bullet object at the player's origin, and calculate the vector from that point to the reticule. I normalize that forward vec and every frame move the projectile along it by as constant speed variable (no need for acceleration, friction, etc here). Problem arises when the player shoots straight forward and is moving very fast: he ends up moving almost as fast as the bullet, which looks super cheesey. Same goes for when he shots in the opposite direction that he is moving. Here's what I have access to: Player's position and velocity (both defined in 3D vectors) Bullets position (3D Vector), speed (Scaler), and forward vector (Normalized 3D Vector) First thing I tried was adding the player's velocity to the bullets forward vector, but that lead to incorrect results. Next I tried adding the length of the player's velocity vector to the bullets base speed (scalar). This kind of worked, speeding the bullet up as the player moves, but it's clear that that is only half the battle. It appears I need to take both things into account; the player's velocity and direction, but I can't figure out how to bridge that gap. Any help would be amazing! Thanks! Matt [Edited by - matthughson on November 24, 2005 11:12:59 AM]

##### Share on other sites
You simply add the player's velocity vector to the bullet's velocity vector.

However, you have a strange game where players can outrun bullets. I thought only Superman could do that. If you want to make it more realistic, you need to make the bullets much faster or the players much slower. In RL, a person can run 5 - 10 m/s and a bullet can travel from 180 - 1500 m/s, so it won't make a noticable difference if you include the velocity of the player. Even the fastest runner would change the velocity of the bullet by less than 10 m/s.

##### Share on other sites
"If you want to make it more realistic..."

I only said I want the bullets to behave more realistic, not the player [wink] It's not really a strange game. It's a 2.5D shooter. These games rarely have bullets that move faster than the player; it'd just be too hard if they did.

Hmm, so it seems adding the vectors is correct... it just doesn't look right. Maybe because I normalize the vector after adding them together (needed to calculate the amount to rotate them to face forward, and because I use a scalar to move along that vector).

Oh well. I'll have to figure out another way to get the effect I want.

Thanks!
Matt

##### Share on other sites
I think i see where your problem comes from... for each object in your game (player, bullet, etc..) you are storing a direction vector and a speed right? or is that just for your bullet?

here is what i suggest, you can combine direction vector and speed together to form a velocity vector.

e.g.

direction vector = (0.707, 0.707) // this corresponds to 45 degree angle
speed = 10

you can convert that to a velocity vector like so:

velocity vector = direction vector*speed = (7.07, 7.07)

this now contains speed AND direction, less data to store since you're only storing 2 numbers instead of 3. If you want to get the speed just get the length of the vector (sqrt(x*x+y*y)), and normalise to get direction.

Now lets say you have a velocity vector for your player and bullet, you can just add the velocity vectors together..

e.g.

player velocity = (10, 2)
bullet velocity = (20, 0) // before adjusting for player velocity

now bullet velocity will be:

bullet velocity = (30, 2)

##### Share on other sites
Ahhh perfect! Thanks space!

Matt

##### Share on other sites
"These games rarely have bullets that move faster than the player; it'd just be too hard if they did."

What on Earth?! Why didn't someone tell me that when I was getting my arse kicked in Quake Arena. Everybody else must have been outrunning my bullets. ;)

##### Share on other sites
Quote:
 Original post by matthughsonPlayer's position and velocity (both defined in 3D vectors)Bullets position (Normalized 3D Vector), speed (Scaler), and forward vector (3D Vector)

The player's position is a vector, but the bullet's position is a normalized vector? That's not right. A normalized vector is for a direction. So, the bullet's position should be just a regular vector and the forward vector of the bullet should be normalized.

##### Share on other sites
Quote:
 Original post by Anonymous Poster"These games rarely have bullets that move faster than the player; it'd just be too hard if they did."What on Earth?! Why didn't someone tell me that when I was getting my arse kicked in Quake Arena. Everybody else must have been outrunning my bullets. ;)

heh... well by this type of game I'm refering to games like Contra, Ikaruga, etc (2D/2.5D shooters, not a FPS)

Quote:
Original post by Endar
Quote:
 Original post by matthughsonPlayer's position and velocity (both defined in 3D vectors)Bullets position (Normalized 3D Vector), speed (Scaler), and forward vector (3D Vector)

The player's position is a vector, but the bullet's position is a normalized vector? That's not right. A normalized vector is for a direction. So, the bullet's position should be just a regular vector and the forward vector of the bullet should be normalized.

Yeah that was a typo.

Matt

##### Share on other sites
If you only want to add the velocity of the players forward velocity, you can find the projection of the players velocity in the forward direction with a dot product. The forward vector must be unit length.

forwardSpeed = Dot(playerVel, playerForward)

then just add that to bullet speed

bulletVelocity = (bulletSpeed + forwardSpeed) * playerForward

##### Share on other sites
Well, Newton says for every action there is an equal and opposite reaction... So if you shoot a bullet out of a gun, you're velocity should decelerate the ammount the bullets velocity is, right?

• ### What is your GameDev Story?

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

• 14
• 23
• 12
• 12
• 11
• ### Forum Statistics

• Total Topics
634782
• Total Posts
3019266
×