00:00
00:00
Newgrounds Background Image Theme

cosoden just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

Bullet rotate on collision help?

1,338 Views | 7 Replies
New Topic Respond to this Topic

Bullet rotate on collision help? 2012-10-20 23:46:08


hey again :p
okay so i have a Bullet that bounces off walls and what not when its shot. works great but the graphic for the bullet is shaped well .. as a bullet. So how can i make it rotate the correct way when it bounces off the walls?(if your confused what im asking look at the pic)
Bullet Class

package 
{
	import flash.display.Sprite;
	import flash.events.Event;

	public class Bullet extends Sprite
	{
		private var xspeed:int;
		private var yspeed:int;
		private var bounce:int;

		public function Bullet()
		{
			//constructor code
			xspeed = 12;
			yspeed = 12;
			bounce = 0;
			addEventListener(Event.ENTER_FRAME, update);
		}

		function update(e:Event)
		{

			var b = this;
			x=x+Math.cos(rotation/180*Math.PI)*xspeed;
			y=y+Math.sin(rotation/180*Math.PI)*yspeed;

			if (b.x <= b.width / 2)
			{
				b.x = b.width / 2;
				xspeed *=  -1;
				bounce +=  1;
			}
			else if (b.x >= stage.stageWidth - b.width/2)
			{
				b.x = stage.stageWidth - b.width / 2;
				xspeed *=  -1;
				bounce +=  1;
			}
			if (b.y <= b.height / 2)
			{
				b.y = b.height / 2;
				yspeed *=  -1;
				bounce +=  1;
			}
			else if (b.y >= stage.stageHeight- b.height/2)
			{
				b.y = stage.stageHeight - b.height / 2;
				yspeed *=  -1;
				bounce +=  1;
			}
			if (bounce >= 2)
			{
				removeEventListener(Event.ENTER_FRAME, update);
				parent.removeChild(this);
				bounce -= 2;
			}
		}
	}
}

Bullet rotate on collision help?

Response to Bullet rotate on collision help? 2012-10-20 23:55:59


unless it's a rubber bullet, it's not going to hold its shape upon impact.

the most likely scenario of a ricochet is that either it will evaporate into shrapnel upon impact or it will bounce once (at the angle being shot) and become a deformed ball of steel with as much force as a pellet gun.

a tank's "bullets" generally explode upon impact.

save yourself some trouble and integrate some realism.


Programming stuffs (tutorials and extras)

PM me (instead of MintPaw) if you're confuzzled.

thank Skaren for the sig :P

BBS Signature

Response to Bullet rotate on collision help? 2012-10-21 00:04:41


save yourself some trouble and integrate some realism.

not really looking for realism, its kinda an arcade style tank game

Response to Bullet rotate on collision help? 2012-10-21 00:48:28


The rotation can be calculated using Math.atan2(vy, vx) where vy and vx are the components of your velocity. So, in your code it would look like:

if (b.y < b.height / 2)
{
	b.y = b.height / 2;
	yspeed *=  -1;
	bounce +=  1;
	rotation = Math.atan2(Math.sin(rotation/180*Math.PI)*yspeed, Math.cos(rotation / 180 * Math.PI) * xspeed) * 180 / Math.PI;
}

I would suggest precalculating and storing the velocity and only calculate it again when rotation changes.


BBS Signature

Response to Bullet rotate on collision help? 2012-10-21 00:56:14


Well..... simple approach. Make your bullet a round circle.

complex approach:

I'm going to show you the math behind the example you showed in the picture, then youll have to use the same idea for the left, right, and bottom walls.

and going to work in degrees:

so first you have your initial rotation of the bullet (in picture its 60 degrees).
when the bullet hits the top wall, the y-values turn from positive to negative values. so basically your rotation angle is just going to flip over the x-axis. so if your initial angle is 90, then when it hits the top wall it will flip over x-axis and become 270 or -90. (insight to if it hits the bottom wall) if your initial angle is 300 or -60 then when it hits bottom wall it will become 120.

same thing for side walls but this time it will flip over y-axis.

inside your if statements for when it hits each wall. change the rotation of the bullet based on the info provided.

here is a picture to help you hopefully:

Bullet rotate on collision help?


BBS Signature

Response to Bullet rotate on collision help? 2012-10-21 00:59:33


oooopss. ignore those 40 degree angles. they should be 60 degrees. I was gonna use a different angle and forgot to update picture.


BBS Signature

Response to Bullet rotate on collision help? 2012-10-21 01:17:37


if, however, you want to keep going like this (it's the core mechanic, for example) then it's time for a basic physics lesson:

Newton's third law states that for every action there is an equal and opposing reaction.

An object in motion likes to stay in motion, and an object as rest likes to stay at rest. That and the third law combined create the "bounce" (ricochet) that is seen.

The thing about firearms is that you basically have a crapton of kinetic energy stored in a single projectile. Obviously when the bullet is travelling through the air it faces some air resistance and gravitational pull, which slows the bullet down and pulls the bullet toward the ground more and more as time goes on. Some bullets take the shape design of arrows, which help to decrease air resistance.

Bullets are also lightweight and can be pushed from side to side easily (wind) - which is why a mile-long shot (the JFK assassination) requires the best snipers one can have, and wind gauges along the way.

I am absolutely horrible with math, so let's see how far off I can get.

Kinetic energy is measured in Joules, which is:
(mass*velocity^2)/2
mass would be kilograms and velocity would be meters.

a .45 caliber bullet will weight approximately 0.0162Kg and the muzzle velocity is about 290 meters per second. We will take this as our example bullet.

so, plugging in the numbers, you will get:
(0.0162 * 290 ^ 2) / 2 = 681.21 Joules of kinetic energy when a .45 cal bullet leaves the gun.

Generally speaking that kind of bullet is large, bulky, does not shatter easily, and does terribly against wind and air resistance.

air resistance is measured as such: (by NASA)
(Cd*A*Rho*V^2)/2
Cd is the coefficient of drag, A is the cross-section of the bullet, Rho is the air density, and V is velocity (in meters, I believe)
a cylindrical object will have 0.295 as Cd, and the average air density in the US at 0 degrees Celsius is 1.292 Kg/m3

we'll assume 3D space and say the length of the stage is 100 pixels.

we'll say that each pixel is a meter, which would leave your stage at 800x600 meters (easy enough to remember)
so, plugging that in:
1.292 * (800 * 600 * 100) = 62016000 Kg or air density for the entire space (it's quite a large area, remember)

so we're still missing A. Since the cross-section of a cylinder is a circle, we just need to find the area of a circle (basic geom: a = pi * radius ^ 2) - a .45 cal bullet has the diameter (twice the radius) of .45 inches (herpaderp), or about 11.5mm. Since we're dealing with meters, i'll leave it at 0.0115 meters.
so, plugging that in:
3.14 * (0.0115 / 2) ^ 2 = 0.00010381625 meters

so using all of this, we get:
(0.295 * 0.00010381625 * 62016000 * 290 ^ 2) / 2 = 79865111.91966 m/w.c.

^now that doesn't look right, does it? I'll wait for a math whiz to come by and correct me.

in the meantime, i've had enough math for today.

in the meantime, say that the bullet loses .10 joules of energy per meter. That should help your physics along a bit.
low let's assume that the bullet is made of rubber. Since rubber retains is joules quite well, we'll say that it only loses 45% of its total energy when it hits the wall dead-on (which is where you would lose the most energy)
We'll also say that for every degree added to the angle, it loses 0.5% fewer joules of energy. This means if you're shooting perpendicular to the wall, it'll lose no energy bouncing off the wall because it didn't bounce off the wall (even if it's touching the wall and you're calculating the math)

now we'll assume that the bullet won't lose it's shape when it hits the wall (how that works i'll have no idea)
you will need to take the front end of the bullet and turn it into a circle, then rotate that circle faster the more of an angle there is. Friction plays a role here.

let's say that there is always 100% friction upon impact as far as rotating the bullet is concerned. We'll lose an additional 0.01 joules of energy there. For every degree of angle the bullet hits the wall with, rotate the bullet an additional 0.75 degrees.

now what if the back end of the bullet hits the wall as soon as the bullet leaves?
The bullet loses an additional 0.01 joules and reverses the rotation, minus 10% rotation.


Programming stuffs (tutorials and extras)

PM me (instead of MintPaw) if you're confuzzled.

thank Skaren for the sig :P

BBS Signature

Response to Bullet rotate on collision help? 2012-10-21 18:13:38


thanks everyone i got it working :D