Monster Racer Rush
Select between 5 monster racers, upgrade your monster skill and win the competition!
4.23 / 5.00 3,881 ViewsBuild and Base
Build most powerful forces, unleash hordes of monster and control your soldiers!
3.93 / 5.00 4,634 ViewsI'm having a problem which is seriously stuninting the growth of a few things I'm currently making.
Here it is,
I want an object (which will most likely be an enemy) to rotate after the player but at a set rate, so it doesn't just snap to the players direction. When making a game involving aircraft, I can see this turning into a game of chicken, that you can't escape from.
I do A Level Physics and A level Math, so in explanations I wont need the equations or math dumbing down.
In my test (which involved an arrow in the centre of the screen rotating after the mouse) I manage to obtain the non-snapping effect from 0-360 degrees, however, the problem that's been on my back is that when the mouse moves across the bottom of the screen onto the other side of the arrow it stops following through the shortest direction and rotates all the way aroud the other direction... Either that or it does the same, but across the top of the screen instead.
I've tried loads of different methods. Any help would be greatly appreciated.
-LAB
Funny isn't it... Curiosity killed the cat, but throwing it out of an airbourne plane doesn't.
Terminal velocity FTW!
just do that based on relative angle.
make sure the rotationValue is 0-360 (normally -180 to 180)
then relativeAngle = angleToObject - rotationValue
if relativeAngle > 180
relativeAngle -= 180
if relative Angle < -180
relativeAngle += 180
those if statements will make sure it uses the shortest possible turn
then relativeAngle should tell you how far it needs to turn
positive is clockwise
negative counterclockwise
use a variable to store the direction which the enemy WANTS to face, and as long as it's not facing that direction, turn towards it at a certain rate.
I just had to figure out the same thing
private var radians:Number = 180 / Math.PI;
enemy.rotation = -(Math.atan2(mouseX - enemy.x, mouseY - enemy.y)) * radians;
just replace the mouseX and mouseY with the players X,Y
this should do just the thing your looking for i hope
It's a post!
Thank you. I'll be sure to try out all of your methods, then I'll get back to you all on which one worked best for me.
=]
-LAB
Funny isn't it... Curiosity killed the cat, but throwing it out of an airbourne plane doesn't.
Terminal velocity FTW!
At 3/19/10 12:49 AM, TheDreamStar wrote: I just had to figure out the same thing
private var radians:Number = 180 / Math.PI;
enemy.rotation = -(Math.atan2(mouseX - enemy.x, mouseY - enemy.y)) * radians;
just replace the mouseX and mouseY with the players X,Y
this should do just the thing your looking for i hope
Unfortunately, this method just snaps the enemy to the player, which I could do anyway (even though this is a much easier method). The 180 rotation one, I'm not quite sure how to do that. I've tried it and the enemy just kind of had a rotational attack. Then the other method, that's the one I've been trying out, with little to no success.
Thanks for the help anyway. =[
-LAB
Funny isn't it... Curiosity killed the cat, but throwing it out of an airbourne plane doesn't.
Terminal velocity FTW!
I've had the same exact problem a few days ago, one of the nice guys at FlashGameLicense.com was able to give me a solution that worked.
rotationDifference = ( targetRotation - movieClip.rotation ) % 360;
if ( rotationDifference > 180 ) { rotationDifference -= 360; }
else if ( rotationDifference < -180 ) { rotationDifference += 360; }
movieClip.rotation += rotationDifference * 0.05;
It's self-explanatory, but there's a whole thread over there about different methods and explanations of rotations and such. You need an account to view it though.
Here's my function:
function difInDir(newRot:Number, oldRot:Number):Number
{
var rotDiff:Number;
if(newRot - oldRot > 180)
{
rotDiff = (newRot - oldRot)-360;
}else
if(newRot - oldRot < -180)
{
rotDiff = (newRot - oldRot)+360;
}else rotDiff = newRot - oldRot;
return(rotDiff);
}
If the new direction is to the left, it returns a negative number, and if it's to the right, a positive.
At 3/21/10 07:45 AM, Lifes-a-Bitch wrote:
Unfortunately, this method just snaps the enemy to the player, which I could do anyway (even though this is a much easier method). The 180 rotation one, I'm not quite sure how to do that. I've tried it and the enemy just kind of had a rotational attack. Then the other method, that's the one I've been trying out, with little to no success.
Thanks for the help anyway. =[
-LAB
sorry about that man but what you could do is you can check the enemy's current angle with the angle it should be and based of that you can slowly increment the angle until you get to the objective thats what i would do to solve this conundrum
It's a post!
At 3/21/10 02:07 PM, Drakenflight wrote: Here's my function:
function difInDir(newRot:Number, oldRot:Number):Number
{
var rotDiff:Number;
if(newRot - oldRot > 180)
{
rotDiff = (newRot - oldRot)-360;
}else
if(newRot - oldRot < -180)
{
rotDiff = (newRot - oldRot)+360;
}else rotDiff = newRot - oldRot;
return(rotDiff);
}
If the new direction is to the left, it returns a negative number, and if it's to the right, a positive.
I don't mean to sound rude... But neither of you have mentioned what method of trig you use to determine the new angle. acos, asin, atain or atan2? On top of that, how you made it relative to the equations. I've just tried both the methods out, but without knowing your method of finding the new angle, I'm going to carry-on finding it difficult.
-LAB
Funny isn't it... Curiosity killed the cat, but throwing it out of an airbourne plane doesn't.
Terminal velocity FTW!
At 3/28/10 11:13 AM, Lifes-a-Bitch wrote: I don't mean to sound rude... But neither of you have mentioned what method of trig you use to determine the new angle. acos, asin, atain or atan2? On top of that, how you made it relative to the equations. I've just tried both the methods out, but without knowing your method of finding the new angle, I'm going to carry-on finding it difficult.
-LAB
Use atan2 to get the angle, then convert it to degrees. Then apply the function.
At 3/28/10 11:20 AM, 4urentertainment wrote:
Use atan2 to get the angle, then convert it to degrees. Then apply the function.
Dandy. Works. However, things never seem to go straightforward for me. When the (for testing purposes) arrow on stage faces the mouse it keeps skipping to the left and right of the mouse rather than stopping dead on it. Is this an error in my scripting or my method or do people usually get this?
My code is:
var Speed:Number = 6
onEnterFrame = function(){
var YM:Number = _ymouse
var XM:Number = _xmouse
var Xdif:Number = ARR._x - XM
var Ydif:Number = ARR._y - YM
var newRot:Number = Math.atan2(Ydif, Xdif)*(180/Math.PI)
var oldRot:Number = ARR._rotation
if(newRot-oldRot > 180){
rotDif = (newRot-oldRot)-360
}else if(newRot-oldRot < -180){
rotDif = (newRot-oldRot)+360
}else{
rotDif = (newRot-oldRot)
}
if(rotDif > 0){
ARR._rotation -= Speed
}else if(rotDif < 0){
ARR._rotation += Speed
}
}
-LAB
Funny isn't it... Curiosity killed the cat, but throwing it out of an airbourne plane doesn't.
Terminal velocity FTW!
Nevermid, managed to get it done via adding in an if function to detect if it's rotation is with in the mouse's rotational position + or - the turning speed.
onEnterFrame = function(){
Speed = 6
var YM:Number = _ymouse
var XM:Number = _xmouse
var Xdif:Number = XM - ARR._x
var Ydif:Number = YM - ARR._y
var newRot:Number = Math.atan2(Ydif, Xdif)*(180/Math.PI)
var oldRot:Number = ARR._rotation
var VrotDif:Number = newRot - oldRot
if(newRot-oldRot > 180){
rotDif = (newRot-oldRot)-360
}else if(newRot-oldRot < -180){
rotDif = (newRot-oldRot)+360
}else{
rotDif = (newRot-oldRot)
}
if((Ydif < 0 && (rotDif < -180-Speed || rotDif > -180+Speed)) || (Ydif > 0 && (rotDif < 180-Speed || rotDif > 180+Speed))){
if(rotDif > 0){
ARR._rotation -= Speed
}else if(rotDif < 0){
ARR._rotation += Speed
}
}
} Funny isn't it... Curiosity killed the cat, but throwing it out of an airbourne plane doesn't.
Terminal velocity FTW!