birdwing
April 14th, 2010, 02:42 AM
Yeah. I know this has probably been asked before, but I couldn't find it using the forum's search function so I am gonna ask.
I am working on building a Tower Defense game. Right now I have enemies that move along a path. The ability to add towers that shoot at the targets, a monetary system, lives, and levels. I am programming this in AS3 and before anyone points it out thinking its a mistake. I define _root as a variable in each of my classes (old habits die hard, so I find a way to use them :D) you just wont see this because there is no point in posting the entire class for my question.
The problem I have come across is that my code for how the tower shoots at the target is inaccurate. The tower shoots at the targets current location, which means that the target is gone by the time the bullet gets there.
The algorithm I am using is this: I am using the PHP block because the code block is lame. But this IS action script.. rest in piece [as] tag
//this is the code that calculates how far the turret should rotate, and it creates the bullet
//enTarget is the target the turret is shooting at
this.rotation = Math.atan2((enTarget.y-y), enTarget.x-x)/Math.PI*180 - 90;;
if(loaded){ //if the turret is able to shoot
loaded = false; //then make it unable to do it for a bit
var newBullet:Bullet = new Bullet(); //create a bullet
//set the bullet's coordinates
newBullet.x = this.x;
newBullet.y = this.y;
newBullet.maxSpeed = bulletSpeed;
//set the bullet's target and damage
newBullet.target = enTarget;
newBullet.damage = damage;
_root.addChild(newBullet); //add it to the stage
}
//the following code calculates the angle the bullet needs to travel
//it sets the xSpeed and ySpeed separately
//target is the target its being shot at
var yDist:Number = (target.y - this.y); //how far the bullet is from the enemy (x)
var xDist:Number = (target.x - this.x); //how far it is from the enemy (y)
var angle:Number = Math.atan2(yDist,xDist); //the angle that it must move
ySpeed = Math.sin(angle) * maxSpeed; //movent is multiplied by maxSpeed
xSpeed = Math.cos(angle) * maxSpeed;
the problem as already stated.. is that this causes the turret to shoot where the target currently is. I can't figure out how to get it to anticipate the targets movements. There is a variable defined for the speed of the bullet because I know I will need that.
I have been scouring the internet for an answer, but the only thing i have found so far was this:
Note: he is talking with point A as the turret and point B as the target
posted on stackoverflow in answer to a similar question
First rotate the axes so that AB is vertical (by doing a rotation)
Now, split the velocity vector of B into the x and y components (say Bx and By). You can use this to calculate the x and y components of the vector you need to shoot at.
B --> Bx
|
|
V
By
Vy
^
|
|
A ---> Vx
You need Vx = Bx and Sqrt(Vx*Vx + Vy*Vy) = Velocity of Ammo.
This should give you the vector you need in the new system. Transform back to old system and you are done (by doing a rotation in the other direction).
I'm new to physics and trigonometry. In fact I am teaching myself... so I understand almost nothing of what he said.
So my question is:
Can someone help me understand what this guy meant? Or help me figure out the proper equation to use to shoot a target by anticipating its movement?
Thanks :beer: :trout:
I am working on building a Tower Defense game. Right now I have enemies that move along a path. The ability to add towers that shoot at the targets, a monetary system, lives, and levels. I am programming this in AS3 and before anyone points it out thinking its a mistake. I define _root as a variable in each of my classes (old habits die hard, so I find a way to use them :D) you just wont see this because there is no point in posting the entire class for my question.
The problem I have come across is that my code for how the tower shoots at the target is inaccurate. The tower shoots at the targets current location, which means that the target is gone by the time the bullet gets there.
The algorithm I am using is this: I am using the PHP block because the code block is lame. But this IS action script.. rest in piece [as] tag
//this is the code that calculates how far the turret should rotate, and it creates the bullet
//enTarget is the target the turret is shooting at
this.rotation = Math.atan2((enTarget.y-y), enTarget.x-x)/Math.PI*180 - 90;;
if(loaded){ //if the turret is able to shoot
loaded = false; //then make it unable to do it for a bit
var newBullet:Bullet = new Bullet(); //create a bullet
//set the bullet's coordinates
newBullet.x = this.x;
newBullet.y = this.y;
newBullet.maxSpeed = bulletSpeed;
//set the bullet's target and damage
newBullet.target = enTarget;
newBullet.damage = damage;
_root.addChild(newBullet); //add it to the stage
}
//the following code calculates the angle the bullet needs to travel
//it sets the xSpeed and ySpeed separately
//target is the target its being shot at
var yDist:Number = (target.y - this.y); //how far the bullet is from the enemy (x)
var xDist:Number = (target.x - this.x); //how far it is from the enemy (y)
var angle:Number = Math.atan2(yDist,xDist); //the angle that it must move
ySpeed = Math.sin(angle) * maxSpeed; //movent is multiplied by maxSpeed
xSpeed = Math.cos(angle) * maxSpeed;
the problem as already stated.. is that this causes the turret to shoot where the target currently is. I can't figure out how to get it to anticipate the targets movements. There is a variable defined for the speed of the bullet because I know I will need that.
I have been scouring the internet for an answer, but the only thing i have found so far was this:
Note: he is talking with point A as the turret and point B as the target
posted on stackoverflow in answer to a similar question
First rotate the axes so that AB is vertical (by doing a rotation)
Now, split the velocity vector of B into the x and y components (say Bx and By). You can use this to calculate the x and y components of the vector you need to shoot at.
B --> Bx
|
|
V
By
Vy
^
|
|
A ---> Vx
You need Vx = Bx and Sqrt(Vx*Vx + Vy*Vy) = Velocity of Ammo.
This should give you the vector you need in the new system. Transform back to old system and you are done (by doing a rotation in the other direction).
I'm new to physics and trigonometry. In fact I am teaching myself... so I understand almost nothing of what he said.
So my question is:
Can someone help me understand what this guy meant? Or help me figure out the proper equation to use to shoot a target by anticipating its movement?
Thanks :beer: :trout: