View Full Version : Firing, homing, hitting...
fw2803
September 5th, 2007, 09:34 AM
Hi there
How do I make a missile chase after the player?
What codes should I add to the locomotion part of the missile?
//edit: Wow wow... Is homing missile a hot topic? It hits 1600...
Skribble
September 6th, 2007, 04:30 AM
What codes should I add to the locomotion part of the missile?
Cheat codez!
omnislant
September 6th, 2007, 12:37 PM
Hi there
How do I make a missile chase after the player?
What codes should I add to the locomotion part of the missile?
well i'm bored at work and messing around with it... just cause i need something to do...
kinda rusty on trig, and moderately new to AS, so i could be way off, but i think i'm getting pretty close to being done with it... when i get home i'll switch it from notepad into flash and get it running and tweaked and whatnot... would you prefer the fla in flash mx 2004, 8, or cs3??
Sirisian
September 6th, 2007, 02:12 PM
you wouldn't use trig... just use the dot product of the normal. That will tell you which side of the missile a point is at.
here I have the code to change the angle in my old tank game:
www.sirisian.templarian.com/flash/flashtankdriver/source.zip
omnislant
September 6th, 2007, 05:46 PM
you wouldn't use trig... just use the dot product of the normal. That will tell you which side of the missile a point is at.
here I have the code to change the angle in my old tank game:
www.sirisian.templarian.com/flash/flashtankdriver/source.zip (http://www.sirisian.templarian.com/flash/flashtankdriver/source.zip)
lol, there's like 5,000 files there, not sure what you're suggesting i change...
plus i'm pretty sure you'd have to use trig unless you're just going to magically have the missile change directions instead of alter it's trajectory to seek the target... but it's been years since i've heard dot product and normal i don't even remember what you're talking about...
i guess i'll wait to hear what you have to say before i continue with the trig
Sirisian
September 6th, 2007, 11:12 PM
var tv:Point = new Point(e.mouseX-pos.x, e.mouseY-pos.y);
tv.normalize(1);
if(tv.y*turretDirection.x - tv.x*turretDirection.y > 0){
turretDirection.x = Math.cos(Math.PI/180*turningRate) * turretDirection.x - Math.sin(Math.PI/180*turningRate) * turretDirection.y;
turretDirection.y = Math.cos(Math.PI/180*turningRate) * turretDirection.y + Math.sin(Math.PI/180*turningRate) * turretDirection.x;
if(tv.y*turretDirection.x - tv.x*turretDirection.y < 0){
turretDirection.x = tv.x;
turretDirection.y = tv.y;
}
}else{
turretDirection.x = Math.cos(-Math.PI/180*turningRate) * turretDirection.x - Math.sin(-Math.PI/180*turningRate) * turretDirection.y;
turretDirection.y = Math.cos(-Math.PI/180*turningRate) * turretDirection.y + Math.sin(-Math.PI/180*turningRate) * turretDirection.x;
if(tv.y*turretDirection.x - tv.x*turretDirection.y > 0){
turretDirection.x = tv.x;
turretDirection.y = tv.y;
}
}
The basic idea is that you keep the velocity normalized (or not whatever). Then take the missiles velocity so get the normal... (-y,x). Then perform the dot product with the target-missile :)
If it's greater than zero the vectors are pointing in the same direction (I think that's right... could be less than).
One second I'll make an example if I have time.
fw2803
September 7th, 2007, 06:40 AM
Thanks so much for all your replies. I'm still trying to understand your codes.
I see you mentioned vectors. I've just started learning vector in school. Actually, what does the code "normalize()" mean? Or is it a function?
omnislant
September 7th, 2007, 07:32 AM
sirisian: lol, you realize all that Math.cos(Math.PI/180) crap is trig, right??
and this code would only fire the bullet at the position the mouse was at when fired... you are aware that what fw's looking for is something where, once the bullet is fired, if you move the mouse (assuming the mouse is the target), the bullet would change it's path to the new location of the mouse, correct? just checking...
however the code you posted here would be a great start for the firing location and direction of the missile
fw2803
September 7th, 2007, 09:17 AM
Oh I see. I'm actually looking for both of these suggested codes. Then if I can make the missile travel to a desired location, I'll be able to make a homing missile by adding appropriate codes to the existing codes.
But omnislant, can you suggest a tutorial for the homing missile or do you know some of the theories?
omnislant
September 7th, 2007, 09:43 AM
Oh I see. I'm actually looking for both of these suggested codes. Then if I can make the missile travel to a desired location, I'll be able to make a homing missile by adding appropriate codes to the existing codes.
But omnislant, can you suggest a tutorial for the homing missile or do you know some of the theories?
yeah buddy i'm writing it now but it's taking longer than i thought i'll try and have it up tonight or maybe tomorrow morning... if i can get it working at all :P
fw2803
September 7th, 2007, 10:06 AM
Oh thanks! It'll definitely be perfect if I could look at some source codes and make some sense out of it.
Sirisian
September 7th, 2007, 07:44 PM
I had an hour before class:
have fun, fairly straight forward.
Here's it running:
Example (http://www.sirisian.templarian.com/flash/MissileRocket/missilerocket.html)
Source:
It's in AS3 so you might have to convert a little, but the concept is the same.
Main.as
package {
import flash.display.*;
import flash.events.*;
import flash.geom.*;
public class Main extends Sprite {
public var renderBuffer:BitmapData;
private var missiles:Array = new Array();
private var target:Point = new Point();
private var targetRotationSpeed:Number = Math.PI/180;
private var targetRadius:Number = 100;
public function Main() {
renderBuffer = new BitmapData(stage.stageWidth, stage.stageHeight, false, 0xFFFFFF);
target.x = stage.stageWidth/2+targetRadius;
target.y = stage.stageHeight/2;
stage.addEventListener(Event.ENTER_FRAME, EnterFrame);
stage.addEventListener(MouseEvent.MOUSE_DOWN, MouseDown);
stage.addChild(new Bitmap(renderBuffer));
}
public function MouseDown(e:MouseEvent):void {
var angle:Number = Math.random()*Math.PI*2;
missiles.push(new Missile(this, stage.stageWidth/2, stage.stageHeight/2, Math.cos(angle), Math.sin(angle), 5, target));
}
public function EnterFrame(e:Event):void {
Update();
Render();
}
public function Update():void {
for(var i:uint = 0; i < missiles.length; ++i){
missiles[i].Update();
}
var newTarget:Point = new Point();
target.x -= stage.stageWidth/2;
target.y -= stage.stageHeight/2;
newTarget.x = Math.cos(targetRotationSpeed) * target.x - Math.sin(targetRotationSpeed) * target.y;
newTarget.y = Math.sin(targetRotationSpeed) * target.x + Math.cos(targetRotationSpeed) * target.y;
target.x = newTarget.x;
target.y = newTarget.y;
target.x += stage.stageWidth/2;
target.y += stage.stageHeight/2;
}
public function Render():void {
renderBuffer.fillRect(renderBuffer.rect, 0xFFFFFF);
for(var i:uint = 0; i < missiles.length; ++i){
missiles[i].Render();
}
var s:Shape = new Shape();
var mat:Matrix = new Matrix();
s.graphics.beginFill(0x0000FF);
s.graphics.drawCircle(0,0,5);
s.graphics.endFill();
mat.translate(target.x, target.y);
renderBuffer.draw(s, mat);
}
}
}
Missile.as
package {
import flash.display.*;
import flash.geom.*;
public class Missile {
public var e:Main;
public var pos:Point = new Point();
public var vel:Point = new Point();
public var velMag:Number;
public var mat:Matrix = new Matrix();
public var s:Shape = new Shape();
public var target:Point;
public var rotationalVelocity:Number = 0;
public var rotationalVelocityMax:Number = Math.PI/180*5;
public var tangentialAcceleration:Number = Math.PI/180*1;
public function Missile(e_:Main, x_:Number, y_:Number, vx_:Number, vy_:Number, velMag_:Number, target_:Point) {
e = e_;
pos.x = x_;
pos.y = y_;
vel.x = vx_;
vel.y = vy_;
velMag = velMag_;
target = target_;
}
public function Update():void {
var targetVector:Point = new Point(target.x - pos.x, target.y - pos.y);
targetVector.normalize(1);
var rotateDirection:int = 0;
if(-vel.y * targetVector.x + vel.x * targetVector.y > 0){
rotationalVelocity += tangentialAcceleration;
CorrectRotationalVelocity();
rotateDirection = -1;
}else if(-vel.y * targetVector.x + vel.x * targetVector.y < 0){
rotationalVelocity -= tangentialAcceleration;
CorrectRotationalVelocity();
rotateDirection = 0;
}
Rotate(rotationalVelocity);
var angle:Number;
if(vel.x * targetVector.x + vel.y * targetVector.y > 0){
if(rotateDirection == -1 && -vel.y * targetVector.x + vel.x * targetVector.y < 0){
angle = Math.acos(vel.x * targetVector.x + vel.y * targetVector.y);
Rotate(-angle);
}
if(rotateDirection == 1 && -vel.y * targetVector.x + vel.x * targetVector.y > 0){
angle = Math.acos(vel.x * targetVector.x + vel.y * targetVector.y);
Rotate(angle);
}
}
pos.x += vel.x*velMag;
pos.y += vel.y*velMag;
}
public function CorrectRotationalVelocity():void {
if(Math.abs(rotationalVelocity) > rotationalVelocityMax) rotationalVelocity = rotationalVelocityMax * (rotationalVelocity/Math.abs(rotationalVelocity));
}
public function Rotate(angle_:Number):void{
var newVel:Point = new Point();
newVel.x = Math.cos(angle_) * vel.x - Math.sin(angle_) * vel.y;
newVel.y = Math.sin(angle_) * vel.x + Math.cos(angle_) * vel.y;
vel.x = newVel.x;
vel.y = newVel.y;
vel.normalize(1);
}
public function Render():void {
s.graphics.clear();
s.graphics.beginFill(0x0,0);
s.graphics.lineStyle(1,0x0);
s.graphics.moveTo(-vel.x*5, -vel.y*5);
s.graphics.lineTo(vel.x*5, vel.y*5);
s.graphics.endFill();
s.graphics.beginFill(0x0,0);
s.graphics.lineStyle(2,0xFF0000);
s.graphics.moveTo(-vel.x*5, -vel.y*5);
s.graphics.lineTo(-vel.x*8, -vel.y*8);
s.graphics.endFill();
mat.identity();
mat.translate(pos.x,pos.y);
e.renderBuffer.draw(s,mat);
}
}
}
SacrificialLamb
September 7th, 2007, 08:28 PM
That's some nice robust code I have a good couple hundred missile and it's still going like a champ, and it looks cool
omnislant
September 7th, 2007, 11:11 PM
wow well done sirisian, can't wait to dissect your code. runs very well
fw2803
September 8th, 2007, 05:13 AM
Thanks a lot! I'll be working on it for these few days.
fw2803
September 9th, 2007, 03:28 AM
Ah yes... I've just finished a simplified simplified simplified... simplified version of a homing missile.
I've got a movieclip called "pc", which is controllable by the player by the arrow keys. Four small red blocks act as the missiles. I've put this onto the missiles:
onClipEvent (enterFrame) {
_root.chasing();
}
And below is the codes of the function chasing():
function chasing() {
var speed:Number
var dx:Number
var dy:Number
var distance:Number
var numMiss:Number = 4
// var vx:Number
// var vy:Number
// var turning:Number
for (i=1;i<10;i++) {
dx=_root.pc._x - _root["missile"+i]._x;
dy=_root.pc._y - _root["missile"+i]._y;
distance=Math.sqrt((dx*dx)+(dy*dy));
dx/=distance;
dy/=distance;
speed = i/i
_root["missile"+i]._x += dx*speed
_root["missile"+i]._y += dy*speed
}
}
And it is a disaster. I soon find out that the speed of the "missiles" increases as i increases. I can't solve the problem. Got any ideas?
SacrificialLamb
September 9th, 2007, 05:33 AM
Why do you have "speed = i/i" any number divided by it self is one why not have "speed = 1" you also seem to be neglecting angles, this link might help in your understanding of applied trigonometry. I think the bottom example will be very helpful from changing the direction of the missile and it more resembles English than Sirisian code
http://flash-creations.com/notes/asclass_math.php
fw2803
September 9th, 2007, 05:43 AM
Thanks for your reply.
I am neglecting angles for the moment. I wonder if you are using Flash CS3 or Flash 8 or not. Anyway... I'll upload the file.
When you look at the codes, you'll find that when 3 missiles, represented by red squares, are present, the speed is 3. When there are 4, the speed becomes 4! What I want to do is to make it constant. If I put 10 missiles onto the stage, the speed would become 10! Let's say I want the speed to be 5.
the "speed = i/i" is my creation when I am attempting every single method to stay it constant.
SacrificialLamb
September 10th, 2007, 12:04 AM
you'll kick your self for this one. It's doing it coz all the missile calls a code that moves all the missile, so every missile there moves them all one. Tack the code off the missile and add
function onEnterFrame() {
_root.chasing();
} to the main time line
fw2803
September 10th, 2007, 04:27 AM
Thanks. I'll try that.
fw2803
September 13th, 2007, 06:04 AM
Thanks a lot! I've managed to make the missiles normal and also the rotation. However, I could observe that there is something wrong with the missiles...
Sometimes, the missiles go crazy and just cruise in a circle... Is my method of making the locmotor of the missile a problem?
My missiles' locomotor is divided into two parts. The first part detects the angle between the missiles and the player and the second part make the missiles go towards the player at that angle.
See the files that I've attached.
gilemon
September 17th, 2007, 02:04 AM
NICE Loops Sirisian!!!!
Darkblade
September 18th, 2007, 04:00 PM
If you still need it there is a good tut here: http://www.tutorialized.com/view/tutorial/Create-heat-seeking-missiles-with-ActionScript/26781
Although I'm sure you can add on to it to make it fit into whatever your trying to make ;)
Sirisian
September 18th, 2007, 05:20 PM
If you still need it there is a good tut here: http://www.tutorialized.com/view/tutorial/Create-heat-seeking-missiles-with-ActionScript/26781
Although I'm sure you can add on to it to make it fit into whatever your trying to make ;) that is probably the most inaccurate representation of a missile I have ever seen. :( It just sets it's velocity to (mousePos - missile).normalize(missileVelocityMagnitude).
fw2803
September 19th, 2007, 02:55 AM
I think I know what the problem is but I definitely don't know how to tackle it... It's the same problem as it is in my other thread "Rotation of turret"
Charleh
September 19th, 2007, 04:02 AM
Simple solution is to use sirsians code :P it's all there and it's easy enough to follow!
fw2803
September 19th, 2007, 04:26 AM
C'mon... I'm here to learn and edit, not to copy>paste nor to understand>copy>paste.
SacrificialLamb
September 19th, 2007, 06:21 AM
I know the problem and I have fixed it before but I can't for the life of me find the file right now and I need sleep (that's not helping). If I remember correctly it's a combination of the MC's _rotation being 0 to 360 and the Math.atan2 giving -180 to 180 and you not taking in to account that the _rotation of the missile properly, my recommendation is to make lots of diagrams
fw2803
September 19th, 2007, 12:18 PM
Thanks for your help. Do have a nice sleep and I'm looking forward to seeing how your fix works :D
Sirisian
September 19th, 2007, 08:51 PM
Hmm, I already said if you didn't understand the code to ask.
The dot product can be used to find if two vectors are pointing in the same direction. Grab some graph paper and try it out.
Draw two vector and give them some values. For instance, the vector (1,1) and (2,1). You can tell they point in the same direction by looking at them, but using the dot product you can make sense of mathematically how to find out.
http://en.wikipedia.org/wiki/Dot_product
Okay, so you have (x1,y1) and (x2,y2) so how would you use the dot product? Better yet, what does it tell you? Well it tells you the angle between two vectors.
x1*x2 + y1*y2
a . b (a dot b) is just x1*x2 + y1*y2. Even in the vectors unnormalized forms this is very useful information.
Going back to: (1,1) and (2,1) we see that 1 * 2 + 1 * 1 is 3. When the dot product is greater than 0 it means the vectors are pointing in the same direction. When they are perpendicular you get zero.
For a missile you are trying to find if the target is on the left or right hand side of the missile. Since in theory the missile is pointing in the direction of it's velocity we can take the normal of the velocity (-vel.y, vel.x). Now we have a left hand normal the magnitude of the velocity which is fine since for checking if the dot product is greater than zero doesn't require the vectors to be normalized.
We have one vector. So the other one must be the vector between the target and the missile. By performing the dot product of that we find that when:
vectorBetweenTargetAndMissile = (target.x-missile.x, target.y-missile.y)
-vel.y * vectorBetweenTargetAndMissile.x + vel.x * vectorBetweenTargetAndMissile.y
x1 * x2 + y1 * y2
the value is greater than zero the vectors are pointing in the same general direction. This means that the missile should turn left if the dot product is greater than 0 and it needs to turn right if it's less than zero.
As a side note you may realize I perform the dot product 2 times. This is take sure that if the target was on one side before moving the missile then it was on the other side the missile obviously over turned and the missile should lock on the targets true position. (which would mean the missiles velocity should be made equal to (target.x-missile.x, target.y-missile.y).normalize(missileVelocityMagnitude)
If anyone needs clarification or any further information then feel free to ask.
SacrificialLamb
September 20th, 2007, 04:49 AM
I can't find it but with the notes I could find I think it was some thing like this;
function onEnterFrame() {
var speed:Number = 4;
var dx:Number;
var dy:Number;
var distance:Number;
var numMiss:Number = 4;
var turning = 5;
// var vx:Number
// var vy:Number
// var turning:Number
for (i=1; i<10; i++) {
if (_root["missile"+i]) {
dx = _root.pc._x-_root["missile"+i]._x;
dy = _root.pc._y-_root["missile"+i]._y;
/*//used this for testing jsut makes it quiker
dx = _root._xmouse-_root["missile"+i]._x;
dy = _root._ymouse-_root["missile"+i]._y;
//*/
angle = Math.round(Math.atan2(dy, dx)*180/Math.PI);
ro = _root["missile"+i]._rotation;
//trace( angle-ro+" a:"+angle+" -r:"+ro)
if ((angle-ro<90 && angle-ro>-90) || angle-ro>270 || angle-ro<-270) {
_root["missile"+i]._rotation += turning;
} else if (angle-ro<180 || angle-ro>-180) {
_root["missile"+i]._rotation -= turning;
}
distance = Math.sqrt((dx*dx)+(dy*dy));
xx = Math.sin(_root["missile"+i]._rotation*Math.PI/180)*speed;
yy = Math.cos(_root["missile"+i]._rotation*Math.PI/180)*speed;
_root["missile"+i]._x += xx;
_root["missile"+i]._y -= yy;
}
}
}
But it really irritates me that I can't find the file, I have back ups of back ups so I know it here some where but there is just to much computer to search when all you have is an idea of what the code should look like
fw2803
September 20th, 2007, 06:07 AM
Thank you so much! You don't have the main file but that's perfectly okay. I could grasp the idea. So you mean that I should split the condition of my two if statements into even clearer parts so the computer can act more accurately. I got it now. Just with a little bit of logical work, I'll get it right. Thanks again to you.
By the way, are you still sleepy? :D
//edi: Ah~~~ Is your code written for objects with initla _rotation 0?
SacrificialLamb
September 20th, 2007, 02:31 PM
Yes I have not been sleeping much all week. the initla _rotation should not matter but the missile was facing up in the MC
fw2803
September 22nd, 2007, 12:29 AM
Right. I'll return if I've got any problems.
fw2803
September 28th, 2007, 07:28 AM
onClipEvent (load) {
var speed = 1
}
onClipEvent (enterFrame) {
dx = this._x - _root.pc._x
dy = this._y - _root.pc._y
radians = Math.atan2(dy,dx)
degrees = Math.round(radians*180/Math.PI)
// if (this._rotation < degrees) {
// this._rotation += speed
// }
// if (this._rotation > degrees) {
// this._rotation -= speed
// }
if ((this._rotation >= 0) and (this._rotation <= 90)) {
if (this._rotation < degrees) {
this._rotation += speed
}
if (this._rotation > degrees) {
this._rotation -= speed
}
}
if ((this._rotation <= 0) and (this._rotation >= -90)) {
if (this._rotation < degrees) {
this._rotation += speed
}
if (this._rotation > degrees) {
this._rotation -= speed
}
}
if ((this._rotation >= 90) and (this._rotation <= 180)) {
if ((degrees >= 90) and (degrees <= 180)) {
if (this._rotation < degrees) {
this._rotation += speed
}
if (this._rotation > degrees) {
this._rotation -= speed
}
}
if ((degrees <= -90) and (degrees >= -180)) {
this._rotation += speed
}
}
if ((this._rotation <= -90) and (this._rotation >= -180)) {
if ((degrees <= -90) and (this._rotation >= -180)) {
if (this._rotation < degrees) {
this._rotation += speed
}
if (this._rotation > degrees) {
this._rotation -= speed
}
}
if ((degrees >= 90) and (degrees <= 180)) {
this._rotation -= speed
}
}
}
Thank god that I've finished the piece of code. It's now turning normally. However, it's a pity that the code is incredibly long.
//edit: Ooops. I found out that there should be even more conditions to add for a perfect rotation...
//edit: Omg! Found a big bug. The missile cannot rotate back from (90<_rotation<180) to (0<_rotation<90)... Big problem...
fw2803
October 6th, 2007, 07:40 AM
Hm... Really need some help. Can anybody help programming this piece of thing? I still couldn't figure out a perfect solution.
StridBR
October 8th, 2007, 12:12 PM
try checking for the difference from desired rotation and actual rotation
if difference <180 && >0 - rotation ++
else if >-180 && <0 - rotation --
else if > 0 - rotation --
else if <0 - rotation ++
here the code i've been using for a mouse guided "turret"
private function onEnterFrame(e:Event):void{
if(playerControlled){
var angle = Math.atan2(mouseY - mpod.y, mouseX - mpod.x);
var drotationMpod = (angle*180/Math.PI);
var rotDist = drotationMpod -mpod.rotation;
if(Math.abs(rotDist) >mpodRotVelo){
if(rotDist <180 && rotDist >0){
mpod.rotation +=mpodRotVelo
}else if(rotDist >-180 && rotDist <0){
mpod.rotation -=mpodRotVelo
}else if(rotDist >0){
mpod.rotation -=mpodRotVelo
}else if(rotDist <0){
mpod.rotation +=mpodRotVelo
}
}
};
}
fw2803
October 8th, 2007, 12:18 PM
Thanks. But got to sleep now. I'm looking forward to seeing your script!
Charleh
October 8th, 2007, 12:21 PM
Just use the dot product like Sirisian said - it will involve a tiny couple of lines of code
We have one vector. So the other one must be the vector between the target and the missile. By performing the dot product of that we find that when:
vectorBetweenTargetAndMissile = (target.x - missile.x, target.y - missile.y)
- vel.y * vectorBetweenTargetAndMissile.x + vel.x * vectorBetweenTargetAndMissile.y
x1 * x2 + y1 * y2
the value is greater than zero the vectors are pointing in the same general direction. This means that the missile should turn left if the dot product is greater than 0 and it needs to turn right if it's less than zero.
fw2803
October 9th, 2007, 05:51 AM
I'm of course going to try that out. There are two methods. I'm just trying one out at the moment but I'll get the vector method too. In the first place, I must get myself a clearer vision about vectors...
Thanks for the advice and the codes. I'll return when I'm stuck again.
//edit: well... Got a little problem. What is the difference between .x and ._x? What does that underscore do to make them different?
Charleh
October 9th, 2007, 06:44 AM
Nothing, I think Sirisian is treating the code as if he has instantiated his own objects with an x/y position - movieclips use _x and _y as their variable names. Depends how you have your game set up
fw2803
October 9th, 2007, 09:56 AM
Alright. Actually, will any disruption be caused if I've got an instance name the same as one of the many Flash built-in functions, such as bullet? When I type _root.bullet, referring to the bullet on the stage, the word "bullet" turned blue.
Charleh
October 9th, 2007, 11:09 AM
No, because you specified the _root so it knows which path you are using. Remember that everything works predominantly in the current scope.
If you had this
var x:Number;
x = 5;
function Test() {
var x:Number;
x++;
trace(x);
}
It should output 1 (assuming it get's initialised to 0) because the x is within the local scope of the function so the compiler doesn't see the x outside of the function from inside the function. I don't know if this will work in Flash though (it does in C) you might get a 'duplicate definition in current scope' error but it SHOULD work in theory.
fw2803
October 10th, 2007, 06:15 AM
I think there won't be any errors... I've defined a the same variable twice. It takes the value of the last definition.
Charleh
October 10th, 2007, 09:54 AM
You shouldn't be able to declare a variable twice within the same scope without a 'duplicate definition in current scope' error.
fw2803
October 10th, 2007, 10:44 AM
I've something like this in the first frame:
//health variables
var health
//end health variables
//upgrades
//original values
var healthO = 100
//end original values
//multipliers
var healthM = 1
//end multipliers
//new values
var health = healthO * healthM
//end new values
//end upgrades
No error message. Actually, what do you mean by the current scope? The same piece of script?
Charleh
October 10th, 2007, 11:25 AM
That code should really throw an error!
You should really use
health = healthO * healthM
instead of
var health = healthO * healthM
Current scope means the 'area' in which the variable can 'see'
So for instance
var x:Number;
function something() {
var x:Number;
}
function somethingelse() {
var x:Number;
}
The first x is accessible from anywhere in the program.
The second and third x are only accessible from inside their respective functions - this is valid code, but the x's inside the functions override the x outside the function
The x outside the function is not visible from inside either of the functions any more
Finally, one x in one function cannot see the x in the other function because they have scope which is local to their respective functions
If you were to declare x more than once either outside the functions or inside the functions you would get an error. (Well you SHOULD)
ArmoredSandwich
October 10th, 2007, 11:27 AM
Ive been trying the same thing for a few days now. I changed the rotation to positive only (-10 = 350). Eliminates 1 problem, fixing the other one now. Ill post here when im done..
The dot product is a better idea i think, however i just cant seem to connect that (or rather the dutch translation of the dot product) to the code.
Charleh
October 10th, 2007, 11:39 AM
Right hang on I'll do the flash code then post it...coz everyone seems to be working on this and it's taken 6 weeks to get nowhere with 50 lines of code for most :)
Charleh
October 10th, 2007, 12:21 PM
Right here it is
It's commented - and for some reason I've done a couple of things which make the code a bit longer... I started off with a vector for velocity... then I realised an angle and speed would do - so there's some parts which could be simplified - but it works.
ArmoredSandwich
October 10th, 2007, 05:10 PM
Right here it is
It's commented - and for some reason I've done a couple of things which make the code a bit longer... I started off with a vector for velocity... then I realised an angle and speed would do - so there's some parts which could be simplified - but it works.
nice, however, I want a code a can understand and I do not understand the dot product, therefor i want it on another way.
I do hope ill understand the dot product now i got a second example..
ohh.. And ive been working on that piece of code for so long (for such a short piece), i want to finish it too :P AND (last thing) i want to know if my way will work ;)
fw2803
October 11th, 2007, 12:59 AM
I think that Flash does not have 350 degrees... When I trace a clip's _rotation value, I can only find that the values range from 0 to 180 and 0 to -180.
Charleh
October 11th, 2007, 04:33 AM
fw2803:
Degrees are relative to a base angle -
(-90) degrees is the same as (270) degrees.
Flash has 360 degrees -
-180 = 180
-90 = 270
90 = 90
0 = 360
in the same vein 0 = 720
0 also = 1440
and so on.
ArmoredSandwich
Er I put a comment in there about the cross product - it should actually say perpendicular dot product. The perpendicular dot product gives a signed angle between two vectors.
Imagine 2 vectors lying on a flat plane - the perpendicular is the one that points along the planes normal (directly away from the plane) - depending on the vector will decide which side of the plane this perpendicular is and can give you which direction you need to make the shortest rotation to a target angle (i.e. the perp-dot product will be positive or negative) - this is the value I store in w
The dot product gives the angle between two vectors and let's me know if the angle we turned the missile to is going to be too large and will 'overshoot' the target - in this case I just set the missile to fly straight at the target so it doesn't overshoot.
ArmoredSandwich
October 11th, 2007, 08:21 AM
got it working.. its a long code indeed but i bet i can halve it if i erase the useless stuff :P
http://www.wonima.demon.nl/other/flash_try_outs/missile_effect.html
source:
http://www.wonima.demon.nl/other/flash_try_outs/missile_effect.zip
and ill definitely try the dot product out sometime, its way more efficient. But for now, im just happy it works :)
edit: slow speed = slow moving speed, not lag
Charleh
October 11th, 2007, 08:30 AM
Ok dude! nps :D
ArmoredSandwich
October 11th, 2007, 08:46 AM
Ok dude! nps :D
nps??
Charleh
October 11th, 2007, 09:00 AM
No problems! D:
Ik ben een nederlander!
fw2803
October 11th, 2007, 09:26 AM
angle = Math.round(Math.atan2(dy, dx)*180/Math.PI);
ro = _root["missile"+i]._rotation;
//trace( angle-ro+" a:"+angle+" -r:"+ro)
if ((angle-ro<90 && angle-ro>-90) || angle-ro>270 || angle-ro<-270) {
_root["missile"+i]._rotation += turning;
} else if (angle-ro<180 || angle-ro>-180) {
_root["missile"+i]._rotation -= turning;
}
I'm actually wondering... Why is that? Why does it turn in the correct direction when coded like that?
ArmoredSandwich
October 11th, 2007, 09:52 AM
No problems! D:
Ik ben een nederlander!
cool!
is the dot product the "inwendig product" (inproduct) or is it inproduct AND uitproduct (uitwendig product);
Charleh
October 11th, 2007, 12:29 PM
Dot product is inwendig product and cross product is uitwendig product or kruisproduct - but this is perp-dot product which I'm not sure what it's called in Dutch but closest I can get is probably perpendiculair inwendig product
I'm not actually from Holland I just know a bit of dutch coz I visit there a lot :)
fw2803
October 12th, 2007, 01:15 AM
Honestly, how does that piece of code work? Can someone please explain?
ArmoredSandwich
October 12th, 2007, 03:06 AM
angle = preferred rotation
ro = current rotation
you calculate the distance and if its bigger or smaller then a certain number it should go to the left or right..
smaller then 90 but bigger then - 90 or bigger then 270 or smaller then -270 and the rotation should increase.
else
if its smaller then 180 or bigger then -180 and the rotation should decrease.
But i dunno. Thats just what i think and what i think is mostly wrong lol :P
What i did and do is draw pictures of it. It really helps to understand things.
Charleh
October 12th, 2007, 05:05 AM
Yeah - but working something out on paper takes longer than using proven mathematics - I find it's easier to understand the perp-dot product than to try and work out the rotation on your own!
ArmoredSandwich
October 12th, 2007, 05:14 AM
Yeah - but working something out on paper takes longer than using proven mathematics - I find it's easier to understand the perp-dot product than to try and work out the rotation on your own!
yes, thats true, but my point is that I find things easier to understand if have an example picture. For the dot product and for "finding out rotations".
fw2803
October 12th, 2007, 12:54 PM
Phew! Then I end the method by angle since I've got it. I'll move on to vector...
solarcloud7
April 24th, 2010, 04:20 AM
Hmm, I already said if you didn't understand the code to ask.
The dot product can be used to find if two vectors are pointing in the same direction. Grab some graph paper and try it out.
Draw two vector and give them some values. For instance, the vector (1,1) and (2,1). You can tell they point in the same direction by looking at them, but using the dot product you can make sense of mathematically how to find out.
http://en.wikipedia.org/wiki/Dot_product
Okay, so you have (x1,y1) and (x2,y2) so how would you use the dot product? Better yet, what does it tell you? Well it tells you the angle between two vectors.
x1*x2 + y1*y2
a . b (a dot b) is just x1*x2 + y1*y2. Even in the vectors unnormalized forms this is very useful information.
Going back to: (1,1) and (2,1) we see that 1 * 2 + 1 * 1 is 3. When the dot product is greater than 0 it means the vectors are pointing in the same direction. When they are perpendicular you get zero.
For a missile you are trying to find if the target is on the left or right hand side of the missile. Since in theory the missile is pointing in the direction of it's velocity we can take the normal of the velocity (-vel.y, vel.x). Now we have a left hand normal the magnitude of the velocity which is fine since for checking if the dot product is greater than zero doesn't require the vectors to be normalized.
We have one vector. So the other one must be the vector between the target and the missile. By performing the dot product of that we find that when:
vectorBetweenTargetAndMissile = (target.x-missile.x, target.y-missile.y)
-vel.y * vectorBetweenTargetAndMissile.x + vel.x * vectorBetweenTargetAndMissile.y
x1 * x2 + y1 * y2
the value is greater than zero the vectors are pointing in the same general direction. This means that the missile should turn left if the dot product is greater than 0 and it needs to turn right if it's less than zero.
As a side note you may realize I perform the dot product 2 times. This is take sure that if the target was on one side before moving the missile then it was on the other side the missile obviously over turned and the missile should lock on the targets true position. (which would mean the missiles velocity should be made equal to (target.x-missile.x, target.y-missile.y).normalize(missileVelocityMagnitude)
If anyone needs clarification or any further information then feel free to ask.
I am going to try to follow your guide and add another dimension to it. For a 3d homing missile. I am using papervision and I have to use Number3d instead of Point because it holds a Z value. Anyway, It looks like I will have to use the dot product 4 times. 2 for pitch and 2 for the yaw. Any advice for me on how this would change the math?!?!
this is a fun topic!
TheCanadian
April 24th, 2010, 05:18 AM
a.b does not give you the angle between a and b :|
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.