ViolentAJ
09-03-2008, 07:54 PM
This is my code on my missile.
stop();
var rotationRate = 10;//10
var speed = 1;//10
var rot = (this._rotation/180)*Math.PI;
var xMov = Math.sin(this.rot);
var yMov = Math.cos(this.rot);
var timer = 0;
var targetLocked;
var distance = Math.sqrt((this._x - _root.liveEnemy[0]._x)*(this._x - _root.liveEnemy[0]._x)+(this._y - _root.liveEnemy[0]._y)*(this._y - _root.liveEnemy[0]._y));
var targetX;
var targetY;
var targetAngle;
var minObject;
//Movement
function turnCW(){
this._rotation += this.rotationRate;
}
function turnCCW(){
this._rotation -= this.rotationRate;
}
function acquireTarget(newEnemy){
this.targetLocked = newEnemy;
}
function chooseTarget(){
for(i = 0; i < _root.liveEnemy.length; i++){
if(Math.sqrt((this._x - _root.liveEnemy._x)*(this._x - _root.liveEnemy._x)+(this._y - _root.liveEnemy._y)*(this._y - _root.liveEnemy._y)) < this.distance){
this.distance = Math.sqrt((this._x - _root.liveEnemy._x)*(this._x - _root.liveEnemy._x)+(this._y - _root.liveEnemy._y)*(this._y - _root.liveEnemy._y));
this.minObject = _root.liveEnemy;
}
}
this.acquireTarget(this.minObject);
}
function pursueTarget(){
var lrot = this._rotation
var lrot2 = lrot;
var orot = this.targetRadians - this.rot;
var targetRadians = Math.atan2(targetY, targetX) + Math.PI/2
var turning = this.rotationRate * (Math.PI/180);
this.targetX = (this.targetLocked._x - this._x);
this.targetY = (this.targetLocked._y - this._y);
this.targetAngle = (targetRadians)*(180/Math.PI);
if(targetX < 0){
targetAngle += 180;
}
if(targetX <= 0 && targetY < 0){
targetAngle -= 180;
}
if (targetLocked._y > this._y){
targetRadians += Math.PI;
}
if(this.rot < 0){
this.rot += Math.PI * 2;
} else if(this.rot >= 360){
this.rot -= Math.PI * 2;
}
if(orot < 0){
orot += Math.PI * 2;
} else if(this.orot >= 360){
orot -= Math.PI * 2;
}
if(this.rot < ((targetRadians - (turning/2)) + turning )){
this.rot = targetRadians;
} else {
if(this.orot <= Math.PI){
this.rot += turning;
} else {
this.rot -= turning;
}
}
if(this._rotation > this.targetAngle){
this.turnCCW();
}
else if(this._rotation < this.targetAngle){
this.turnCW();
}
}
for(i = 0; i < _root.liveEnemy.length; i++){
if(Math.sqrt((this._x - _root.liveEnemy._x)*(this._x - _root.liveEnemy._x)+(this._y - _root.liveEnemy._y)*(this._y - _root.liveEnemy._y)) <= this.distance){
this.acquireTarget(_root.liveEnemy);
}
}
this.onEnterFrame = function(){
if(this._currentframe == 1){
// Update Trig
this.rot = (this._rotation/180)*Math.PI;
this.xMov = Math.sin(this.rot);
this.yMov = Math.cos(this.rot);
//Move in direction
this._x += this.xMov * this.speed;
this._y -= this.yMov * this.speed;
if(this.targetLocked.dead){
this.distance = Math.sqrt((this._x - _root.liveEnemy[0]._x)*(this._x - _root.liveEnemy[0]._x)+(this._y - _root.liveEnemy[0]._y)*(this._y - _root.liveEnemy[0]._y));
this.minObject = _root.liveEnemy[0];
this.chooseTarget();
} else {
this.pursueTarget();
}
this.timer++;
if(this.timer >= 180){
this.play();
}
}
}
It's still erractic.
This is the link to the swf.
http://spamtheweb.com/ul/upload/020908/80050_ArianStarfighterEngineTest.php
The C key shoots missiles. The Z key shoots plasma and the X key shoots the 20mm cannon. Only the the plasma and 20mm can "kill" "enemies" now. This is only a test of the missile rotation. The select target algorithm works well, it's just getitng it to rotate and move correctly. I code in my objects by the way.
stop();
var rotationRate = 10;//10
var speed = 1;//10
var rot = (this._rotation/180)*Math.PI;
var xMov = Math.sin(this.rot);
var yMov = Math.cos(this.rot);
var timer = 0;
var targetLocked;
var distance = Math.sqrt((this._x - _root.liveEnemy[0]._x)*(this._x - _root.liveEnemy[0]._x)+(this._y - _root.liveEnemy[0]._y)*(this._y - _root.liveEnemy[0]._y));
var targetX;
var targetY;
var targetAngle;
var minObject;
//Movement
function turnCW(){
this._rotation += this.rotationRate;
}
function turnCCW(){
this._rotation -= this.rotationRate;
}
function acquireTarget(newEnemy){
this.targetLocked = newEnemy;
}
function chooseTarget(){
for(i = 0; i < _root.liveEnemy.length; i++){
if(Math.sqrt((this._x - _root.liveEnemy._x)*(this._x - _root.liveEnemy._x)+(this._y - _root.liveEnemy._y)*(this._y - _root.liveEnemy._y)) < this.distance){
this.distance = Math.sqrt((this._x - _root.liveEnemy._x)*(this._x - _root.liveEnemy._x)+(this._y - _root.liveEnemy._y)*(this._y - _root.liveEnemy._y));
this.minObject = _root.liveEnemy;
}
}
this.acquireTarget(this.minObject);
}
function pursueTarget(){
var lrot = this._rotation
var lrot2 = lrot;
var orot = this.targetRadians - this.rot;
var targetRadians = Math.atan2(targetY, targetX) + Math.PI/2
var turning = this.rotationRate * (Math.PI/180);
this.targetX = (this.targetLocked._x - this._x);
this.targetY = (this.targetLocked._y - this._y);
this.targetAngle = (targetRadians)*(180/Math.PI);
if(targetX < 0){
targetAngle += 180;
}
if(targetX <= 0 && targetY < 0){
targetAngle -= 180;
}
if (targetLocked._y > this._y){
targetRadians += Math.PI;
}
if(this.rot < 0){
this.rot += Math.PI * 2;
} else if(this.rot >= 360){
this.rot -= Math.PI * 2;
}
if(orot < 0){
orot += Math.PI * 2;
} else if(this.orot >= 360){
orot -= Math.PI * 2;
}
if(this.rot < ((targetRadians - (turning/2)) + turning )){
this.rot = targetRadians;
} else {
if(this.orot <= Math.PI){
this.rot += turning;
} else {
this.rot -= turning;
}
}
if(this._rotation > this.targetAngle){
this.turnCCW();
}
else if(this._rotation < this.targetAngle){
this.turnCW();
}
}
for(i = 0; i < _root.liveEnemy.length; i++){
if(Math.sqrt((this._x - _root.liveEnemy._x)*(this._x - _root.liveEnemy._x)+(this._y - _root.liveEnemy._y)*(this._y - _root.liveEnemy._y)) <= this.distance){
this.acquireTarget(_root.liveEnemy);
}
}
this.onEnterFrame = function(){
if(this._currentframe == 1){
// Update Trig
this.rot = (this._rotation/180)*Math.PI;
this.xMov = Math.sin(this.rot);
this.yMov = Math.cos(this.rot);
//Move in direction
this._x += this.xMov * this.speed;
this._y -= this.yMov * this.speed;
if(this.targetLocked.dead){
this.distance = Math.sqrt((this._x - _root.liveEnemy[0]._x)*(this._x - _root.liveEnemy[0]._x)+(this._y - _root.liveEnemy[0]._y)*(this._y - _root.liveEnemy[0]._y));
this.minObject = _root.liveEnemy[0];
this.chooseTarget();
} else {
this.pursueTarget();
}
this.timer++;
if(this.timer >= 180){
this.play();
}
}
}
It's still erractic.
This is the link to the swf.
http://spamtheweb.com/ul/upload/020908/80050_ArianStarfighterEngineTest.php
The C key shoots missiles. The Z key shoots plasma and the X key shoots the 20mm cannon. Only the the plasma and 20mm can "kill" "enemies" now. This is only a test of the missile rotation. The select target algorithm works well, it's just getitng it to rotate and move correctly. I code in my objects by the way.