Equal Distance Projectile Spread
by John Eckhardt · in Torque Game Engine · 07/20/2008 (7:05 pm) · 4 replies
I want to have a weapon shoot a spread of ammo, but NOT random. The projectiles should aim along the horizontal axis relative to the direction the gun is facing.
That means, I don't want the shells to go along the world's X-Axis, but along the player's eye transform (or gun transform) axis.
To make it easy, let's say 5 projectiles spread in a 30 degree angle. 1 in the center, 2 at +/- 15 degrees and 2 at the edges.
I do not know math well enough to figure this one out.
That means, I don't want the shells to go along the world's X-Axis, but along the player's eye transform (or gun transform) axis.
To make it easy, let's say 5 projectiles spread in a 30 degree angle. 1 in the center, 2 at +/- 15 degrees and 2 at the edges.
I do not know math well enough to figure this one out.
#2
07/21/2008 (7:12 pm)
I'm not sure where to add the offset in. It does not do what is expected when I add in the offset to the MuzzleVector. While it does fire in a spread, the spread aims south. Not exactly the same direction every time, but when you face north, it fires south, which is rather damaging. What am I missing?for(%i = 0; %i < %obj.spickleSpreadShot; %i++) {
// Get the muzzle vector. This is the dead straight aiming point of the gun
%vector = %obj.getMuzzleVector(%slot);
%vector = VectorAdd(%vector, %bulletOffset[%i]);
// Get our players velocity. We must ensure that the players velocity is added
// onto the projectile
%objectVelocity = %obj.getVelocity();
// Determine scaled projectile vector. This is still in a straight line as
%vector1 = VectorScale(%vector, %projectile.muzzleVelocity);
%vector2 = VectorScale(%objectVelocity, %projectile.velInheritFactor);
%velocity = VectorAdd(%vector1,%vector2);
// Create our projectile
%p = new (%this.projectileType)()
{
initialVelocity = %velocity; ...
};
MissionCleanup.add(%p);
if (%obj.getInventory(%this.ammo) < 1) break;
%obj.decInventory(%this.ammo,1);
}
#3
I hope I have done my math right! You may need to play around with the mDegToRad(90) bit, however.
07/21/2008 (7:48 pm)
Ahh, what I did was not give you an appropriate starting point. You need to start at the muzzle vector then calculate the offsets:%muzzleVector = %obj.getMuzzleVector(%slot); %muzzleAngle = mDegToRad(90) - mATan(getWord(%muzzleVector, 1), getWord(%muzzleVector, 0); while (%muzzleAngle < 0) %muzzleAngle += mDegToRad(360); while (%muzzleAngle > 360) %muzzleAngle -= mDegToRad(360); %initAngle = %muzzleAngle - %bulletSpread * ((%bulletCount - 1) / 2);
I hope I have done my math right! You may need to play around with the mDegToRad(90) bit, however.
#4
So if I aim straight, it appears to work. But if I aim at a 60 degree angle from the horizontal, it fires at 30 degrees. I've tried messing around with the mDegToRad(90) a bit, but it just seems to offset the direction of the spread by degrees left or right.
07/22/2008 (6:00 pm)
Awesome, thanks so much for the help. Now it fires in the general direction of the gun. However, the Z axis is the only problem. The arrows fire at the height of halfway between horizontal (player Z=0) and the angle that the gun is pointing at. So if I aim straight, it appears to work. But if I aim at a 60 degree angle from the horizontal, it fires at 30 degrees. I've tried messing around with the mDegToRad(90) a bit, but it just seems to offset the direction of the spread by degrees left or right.
Associate Phillip O'Shea
Violent Tulip
%bulletCount = 5; %bulletAngle = mDegToRad(30); %bulletSpread = %bulletAngle / (%bulletCount - 1); %initAngle = -%bulletSpread * ((%bulletCount - 1) / 2); for (%i = 0; %i < %bulletCount; %i++) %bulletOffet[%i] = mSin(%initAngle + %i * %bulletSpread) SPC mCos(%initAngle + %i * %bulletSpread) SPC 0;You'd need to add the offset to the eyeTransform, but the above should give you what you're looking for.