Public Group

# Bullet Trajectory - Questions

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

## Recommended Posts

I've got a 2D physics related question, and I was wondering if anyone could help.

I'm working on an iPhone game using the accelerometer. In my game, I fire a bullet in a parabolic arc. This I have achieved using some basic physics. However, via the use of the accelerometer, I would like the player to be able to subtly change the X position of the bullet in mid-air.

Evey update, I am calculating the position of my bullet as follows:

m_pointPosition.x = m_pointStartPoint.x + m_pointInitialVelocity.x *  cos(m_flAngle) * time;m_pointPosition.y = m_pointStartPoint.y + m_pointInitialVelocity.y *  sin(m_flAngle) * time - GRAVITY * time * time / 2;

However, when it comes to modifying the position of the bullet via the accelerometer, I'm having no luck. I've tired altering the current velocity of the bullet whilst it is in the air and applying that to my bullet position calculation, but no joy. I've also tried modifying the position itself, but again that has no effect because I am always recalculating the position in my update loop.

I'm thinking that I might need to apply a horizontal acceleration to the position every update, and modify that acceleration based on the accelerometer value.

Is that the right thing to do, or is there something I am missing?

##### Share on other sites
First, you need to replace your math. What you have is too static for what you want to get out of it.
You need to remove your initial conditions (v*cos(angle) and gravity), and make them variables that you keep track of over time.

The motion you want is something more like:
Vt = Vt-1 + A*t + Ainput;Pt = Pt-1 + Vt*t

You would SET V to "m_pointInitialVelocity.x *cos(m_flAngle)" only once, then never see that value again.
You would then set A to gravity once.
Then, every frame, Ainput is that frame's accelerometer value (scaled and clamped to reasonable values). Vt is the velocity you keep track of every frame, and Pt is the position you keep track of every frame.
Once you get that working, there are more stable "integration" methods, like velocity-verlet or RK4 that you may want to use.

##### Share on other sites
Are these changes required for just the X component of the bullet's position?

##### Share on other sites
No, they would be changes to the entire vector in all components x,y (and in 3d z).

You would apply the formula to all the components in the exact same way. Just the inital values would just be different for each component.
A.x would start 0, A.y would be -9.8 (or whatever your gravity is).

##### Share on other sites

Bearing in mind that I am working out the X and Y components of my vector separately, what would I need to do to get the required effect.

Based on the changes you suggested, I've done the following:

// Set to Gravity just once.m_flA = GRAVITY;// When I trigger the bullet to be fired, I do the following:m_flV = m_pointInitialVelocity.x * cos(m_flAngle);// Inside my Update loop, I am calculating position and velocity as follows.// This is my attempt to calculate Vt = Vt-1 + A*t + Ainput;// I've not implemented Ainput yet, as I want to see if I can get the correct // Parabola curve to work without worrying about user input first.// X Velocity component.m_pointVelocity.x	= m_flV + m_flA * timeSinceFiring; // X Position component.// This is my attempt to calculate Pt = Pt-1 + Vt*tm_pointPosition.x	+= m_pointVelocity.x * timeSinceFiring;// Y Component.m_pointPosition.y = m_pointStartPoint.y + m_pointInitialVelocity.y * cos(m_flAngle) * m_timeSinceFiring - GRAVITY * m_timeSinceFiring * m_timeSinceFiring / 2;

Needless to say this isn't working, as I'm confused as to exactly how to proceed. (Please accept my sincerest apologies for this).

I should add that when working out motion before, instead of adding delta time to my values, I was using the time since the bullet was fired. However, this did allow me to achieve the effect I wanted and fired the bullet in a parabola based on the angle I had set it.

Perhaps you would be so kind as to point me in the right direction again?

##### Share on other sites
The best way I found to handle movement with gravity is the following:

You hold, for each dimension (x, y, z), the position, velocity and acceleration. Then every frame, you add acceleration to velocity and velocity to position. This must be FPS independent, so we must also multiply with SPF or time_per_frame:

// constantm_pointAcceleration.y = -10.0;// in your game loopm_pointVelocity.x += m_pointAcceleration.x * time_per_frame;m_pointVelocity.y += m_pointAcceleration.y * time_per_frame;m_pointPosition.x += m_pointVelocity.x * time_per_frame;m_pointPosition.y += m_pointVelocity.y * time_per_frame;// when bullet is fired, you do the followingm_pointVelocity.x += m_pointBulletVelocity.x * cos(m_flAngle);m_pointVelocity.y += m_pointBulletVelocity.y * sin(m_flAngle);

Then you need to check for collision. Also make sure that when the object is at rest (ie. it lays on the ground) there must be a force (acceleration) in the opposite direction of gravity.

##### Share on other sites
It sounds like you are still thinking in terms of a closed form solution to your motion. This only works if the trajectory is fixed at launch time, but obviously in your case that's what you are trying to change. What you want to do, at least to start with, is simply do the following each frame:

position += velocity * dt
velocity += acceleration * dt

where position, velocity and acceleration are all 2D vectors. dt is the delta time, which is the time since the last update, not the total time or time since firing the projectile.

You can vary the acceleration as you see fit, based on the accelerometer values. This is an example of Euler Integration, if you google this you'll find a lot more information.

##### Share on other sites
Thanks you all kindly for your help, I really appreciate it.

##### Share on other sites
Funny, I worked on this same subject yesterday.

For me, the problem was that upon rest the acceleration kept subtracting 10 from the velocity. And I kept putting the position back to 0. When I wanted to launch, I had to surpass the debt in velocity first. But at the look you set the velocity to be positive (instead of adding) so that shouldn't be a problem.

Oh btw, your m_pointBulletVelocity is not a vector YET. It's a scalar. Then your angle dictates that the bullets should be fired in the x-direction (90 degree in cos gives a 1), but since m_pointBulletVelocity is a vector with no x-velocity the bullet has no speed.

EDIT: oh you removed your post

##### Share on other sites
Quote:
 Original post by DecriusFunny, I worked on this same subject yesterday.For me, the problem was that upon rest the acceleration kept subtracting 10 from the velocity. And I kept putting the position back to 0. When I wanted to launch, I had to surpass the debt in velocity first. But at the look you set the velocity to be positive (instead of adding) so that shouldn't be a problem.Oh btw, your m_pointBulletVelocity is not a vector YET. It's a scalar. Then your angle dictates that the bullets should be fired in the x-direction (90 degree in cos gives a 1), but since m_pointBulletVelocity is a vector with no x-velocity the bullet has no speed.EDIT: oh you removed your post

Thanks for the reply all the same ;).

I made a simple error, hence deleting my post.

But thank you so much, you've helped me solve my problem, so I'm in your debt!

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 26
• 9
• 11
• 9
• 9
• ### Forum Statistics

• Total Topics
633715
• Total Posts
3013494
×