PDA

View Full Version : Rotational AI w/ Missile


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.

ViolentAJ
09-04-2008, 06:17 AM
This is a bump.

I've noticed that it tracks the target fine unless the missile's position is above and to the right of the missile (meaning that the target is to the lower left of the missile; cartesian quadrant III with the missile being the origin).

rrh
09-04-2008, 04:59 PM
What's the purpose of this?
if(targetX < 0){
targetAngle += 180;
}
if(targetX <= 0 && targetY < 0){
targetAngle -= 180;
}
Note that if targetX<0 and targetY >=0, then it adds 180 and then subtracts it again. But if targetX<0 and targetY>0, then it just adds 180.

Now, when is targetX<0 and targetY>0? When the target is to the left and below the missile, right?

ViolentAJ
09-04-2008, 08:07 PM
if(targetX <= 0 && targetY < 0){
targetAngle -= 180;
}

I tried using && targetY > 0, but it screwed everything up.

ViolentAJ
09-04-2008, 10:11 PM
stop();

var rotationRate = 10;
var speed = 7;
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[i]._x)*(this._x - _root.liveEnemy[i]._x)+(this._y - _root.liveEnemy[i]._y)*(this._y - _root.liveEnemy[i]._y)) < this.distance){
this.distance = Math.sqrt((this._x - _root.liveEnemy[i]._x)*(this._x - _root.liveEnemy[i]._x)+(this._y - _root.liveEnemy[i]._y)*(this._y - _root.liveEnemy[i]._y));
this.minObject = _root.liveEnemy[i];
}
}
this.acquireTarget(this.minObject);
}

function pursueTarget(){
var lrot = this._rotation
var lrot2 = lrot;
var orot = 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);

trace("Degrees " + this.targetAngle);
trace("Radians " + targetRadians);

/*if(this.targetX < 0){
this.targetAngle += 180;
}
if(this.targetX <= 0 && this.targetY < 0){
this.targetAngle -= 180;
}*/
//Condition Needed for Quadrant III
if(this.targetY > 0 && this.targetX < 0){
this.targetAngle -= 360;
}
if(this._rotation == 0){
this._rotation++;
}

if(this.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[i]._x)*(this._x - _root.liveEnemy[i]._x)+(this._y - _root.liveEnemy[i]._y)*(this._y - _root.liveEnemy[i]._y)) <= this.distance){
this.acquireTarget(_root.liveEnemy[i]);
}
}

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();
}
}
}

This is my current code. I upped the speed of the missile, and it does seek great initially, but then it kind of sways back and forth over the target. I have commented out the code in question int he previous post.

This is the new SWF so that you can see what I'm talking about.

http://spamtheweb.com/ul/upload/040908/51087_ArianStarfighterEngineTest.php