RoninStretch
10-07-2007, 05:13 PM
I have a tank which moves about just dandy.
The problem is rotating it's turret to the mouse position.
This code does work to a point. However when i move the mouse past certain angles the turret starts turning in the oposite direction, away from the mouse and then just keeps spinning.
I just can't seem to track down why this is happening for the life of me.
Here's the code for my player class.
It's linked to a MC of my tank in the library which in turn has a nested MC called 'turret'
so 'this' refers to the tank instance and 'this.turret' to the turret itself.
and this code is running on an ENTER_FRAME event.
// Get Key Presses
if (Key.isDown(Keyboard.LEFT)) {
//left
rotSpeed -= rotMoveSpeed;
}
else if (Key.isDown(Keyboard.RIGHT)) {
// right
rotSpeed += rotMoveSpeed;
}
if (Key.isDown(Keyboard.UP)) {
// up
speed += moveSpeed;
}
else if (Key.isDown(Keyboard.DOWN)) {
//down
speed -= moveSpeed;
}
// Do Actual Movement.
// update x,y depending on current rotation and speed
var newAngle = this.rotation + 90;
this.x += Math.sin (newAngle * Math.PI / 180) * speed;
this.y += Math.cos (newAngle * Math.PI / 180) * -speed;
// apply rotation
this.rotation += rotSpeed;
this.turret.rotation -= rotSpeed;
// limit the reverse speed
if (speed < -2) {
speed = -2;
}
// friction (also limits top speed)
speed *= 0.85;
rotSpeed *= 0.85;
// Mouse and Turret movement.
// get the rotation angle for the current mouse position.
var distX = stage.mouseX - this.x;
var distY = stage.mouseY - this.y;
var angle = Math.atan2(distY,distX)/(Math.PI/180);
angle += 90;
// We need an angle relevant to the tank so subract it's rotation from the new angle
angle -= this.rotation;
// rotate toward our new position.
if(this.turret.rotation < (angle-1)){
this.turret.rotation += turretSpeed;
}
else if(this.turret.rotation > (angle+1)){
this.turret.rotation -= turretSpeed;
}
Any advice on this stuff is welcome as Im new to trig nevermind AS.
I've included the while project below. Theres not much in there besides what's been posted except senocular's fab Key.isDown class. But you can run the swf quick to see the problem if you need to.
Thanks for reading.
The problem is rotating it's turret to the mouse position.
This code does work to a point. However when i move the mouse past certain angles the turret starts turning in the oposite direction, away from the mouse and then just keeps spinning.
I just can't seem to track down why this is happening for the life of me.
Here's the code for my player class.
It's linked to a MC of my tank in the library which in turn has a nested MC called 'turret'
so 'this' refers to the tank instance and 'this.turret' to the turret itself.
and this code is running on an ENTER_FRAME event.
// Get Key Presses
if (Key.isDown(Keyboard.LEFT)) {
//left
rotSpeed -= rotMoveSpeed;
}
else if (Key.isDown(Keyboard.RIGHT)) {
// right
rotSpeed += rotMoveSpeed;
}
if (Key.isDown(Keyboard.UP)) {
// up
speed += moveSpeed;
}
else if (Key.isDown(Keyboard.DOWN)) {
//down
speed -= moveSpeed;
}
// Do Actual Movement.
// update x,y depending on current rotation and speed
var newAngle = this.rotation + 90;
this.x += Math.sin (newAngle * Math.PI / 180) * speed;
this.y += Math.cos (newAngle * Math.PI / 180) * -speed;
// apply rotation
this.rotation += rotSpeed;
this.turret.rotation -= rotSpeed;
// limit the reverse speed
if (speed < -2) {
speed = -2;
}
// friction (also limits top speed)
speed *= 0.85;
rotSpeed *= 0.85;
// Mouse and Turret movement.
// get the rotation angle for the current mouse position.
var distX = stage.mouseX - this.x;
var distY = stage.mouseY - this.y;
var angle = Math.atan2(distY,distX)/(Math.PI/180);
angle += 90;
// We need an angle relevant to the tank so subract it's rotation from the new angle
angle -= this.rotation;
// rotate toward our new position.
if(this.turret.rotation < (angle-1)){
this.turret.rotation += turretSpeed;
}
else if(this.turret.rotation > (angle+1)){
this.turret.rotation -= turretSpeed;
}
Any advice on this stuff is welcome as Im new to trig nevermind AS.
I've included the while project below. Theres not much in there besides what's been posted except senocular's fab Key.isDown class. But you can run the swf quick to see the problem if you need to.
Thanks for reading.