|
-
Nov 13th, 2010, 10:50 PM
#1
Thread Starter
Frenzied Member
Angle of projectile
I am having trouble with the height component of a simple projectile equation...
Here is what I have done already:
Distance = GetDistance3D(Bullet(x).BulletStartPosition.x, 0, Bullet(x).BulletStartPosition.z, Bullet(x).BulletPosition.x, 0, Bullet(x).BulletPosition.z)
Bullet(x).BulletPosition.x = Bullet(x).BulletPosition.x + Cos(Bullet(x).BulletAngleY)
Bullet(x).BulletPosition.z = Bullet(x).BulletPosition.z + Sin(Bullet(x).BulletAngleY)
This works fine, the bullet moves at the correct angle. I am having a problem with the height of the bullet as it moves through the air. I think it should be something like this:
Bullet(x).BulletPosition.y = Tan(Bullet(x).BulletAngleX) * Distance
However, this doesn't work. I am not worried about gravity pulling the bullet down, i just want to shoot a bullet in the desired direction.
Please help
thanks
-
Nov 14th, 2010, 12:14 AM
#2
Re: Angle of projectile
You haven't specified your coordinate system, so it's tough to help you. BulletAngleY seems to be the angle of your velocity vector projected into the x-z plane. This suggests your bullet is fired from a turret which can rotate around the y axis, with rotation angle BulletAngleY. I imagine the turret can point up or down and makes an angle BulletAngleX with the x-z plane. (I'm ignoring your expression for the y coordinate.) This coordinate system is just the usual spherical coordinates, though your axes are different from standard. Try
Code:
...BulletPosition.x += Cos(...BulletAngleY) * Sin(...BulletAngleX)
...BulletPosition.z += Sin(...BulletAngleY) * Sin(...BulletAngleX)
...BulletPosition.y += Cos(...BulletAngleX)
or minor variations thereof. If it doesn't work, please explain what your angles mean.
Edit: I should mention the above is supposed to be run on each frame. The += notation is as usual, i.e. A += B means A = A + B. One could also use your distance function, but it's needlessly complex to do so.
Last edited by jemidiah; Nov 14th, 2010 at 12:55 AM.
The time you enjoy wasting is not wasted time.
Bertrand Russell
<- Remember to rate posts you find helpful.
-
Nov 14th, 2010, 01:41 AM
#3
Thread Starter
Frenzied Member
Re: Angle of projectile
thank you heaps... works perfectly
-
Nov 14th, 2010, 01:52 AM
#4
Re: Angle of projectile
Cool, glad to help. Adding in gravity isn't difficult at all, by the way. You just have to decrease the velocity in the y-direction by a constant factor each frame. More specifically, you would do...
Code:
...BulletPosition.y += Cos(...BulletAngleX) - FramesSinceBulletWasFired * g
where g is an arbitrary constant tuned to your liking. Similarly, if you want the velocity to change, just multiply each term on the right hand side of the equations in my first post by a constant.
An alternative approach to all of this is to parametrize the bullet's trajectory analytically, which would give you the usual parabolic motion of an object in freefall in a uniform gravitional field in Newtonian physics. This approach becomes unworkable for more complicated situations, and the above approximates this ideal solution very well for small time steps. The distance function above seems to be trying this analytic approach.
The time you enjoy wasting is not wasted time.
Bertrand Russell
<- Remember to rate posts you find helpful.
-
Dec 4th, 2010, 12:12 AM
#5
Thread Starter
Frenzied Member
Re: Angle of projectile
OK, another question on this topic. I am using your math to move a tank around. Here is my code:
Dim Speed As TV3D65.TV_3DVECTOR
Dim SphereVec As TV3D65.TV_3DVECTOR
SphereVec.x = 0.1
SphereVec.y = 0.1
SphereVec.z = 0.1
Speed.x = SphereVec.x * System.Math.Cos(sngAngleY) * System.Math.Sin(sngAngleX)
Speed.y = SphereVec.y * System.Math.Cos(sngAngleX)
Speed.z = SphereVec.z * System.Math.Sin(sngAngleY) * System.Math.Sin(sngAngleX)
TankPosition.x = TankPosition.x + Speed.x * TV.TimeElapsed * TankCurrentSpeed
TankPosition.y = TankPosition.y - Speed.y * TV.TimeElapsed * TankCurrentSpeed
TankPosition.z = TankPosition.z + Speed.z * TV.TimeElapsed * TankCurrentSpeed
I am wanting to know how to make the tank strafe from either left or right.
I think it is with this line of code:
Speed.x = SphereVec.x * System.Math.Cos(sngAngleY) * System.Math.Sin(sngAngleX)
-
Dec 4th, 2010, 03:32 AM
#6
Re: Angle of projectile
I'll assume Speed is a vector giving the direction in which the tank is pointed. You want to strafe left or right in the xz plane, I believe. So, you can project Speed into the xz plane and find a vector perpendicular to it. For a vector <x, z>, a vector perpendicular to it is <z, -x> (their dot product is 0). That is, do the following:
Code:
Strafe.x = Speed.z
Strafe.z = -Speed.x
Strafe = Strafe.Normalize()*StrafeSpeed
where .Normalize() just normalizes the given vector and StrafeSpeed is a constant you supply. A negative strafe speed will strafe in the opposite direction. Add Strafe like you did Speed at each frame.
Note that strafing isn't defined with this setup if Speed's .x and .z components are both 0. An alternative method that overcomes this shortcoming is to keep track of a normal vector for the tank (a vector coming out of the hatch) and take the strafing direction to be the cross product of the forward vector for the tank and the normal vector. But, this doesn't seem worthwhile for you.
The time you enjoy wasting is not wasted time.
Bertrand Russell
<- Remember to rate posts you find helpful.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|