PDA

View Full Version : motion according to rotation


Swas
12-16-2004, 05:54 AM
im creating a mini game for my big game in which u must flick coins between each other (not gonna say details koz im too lazy) basically i have a full engine going the only thing i cant get right is the title, when each coin is click it is selected and you can rotate using the left a right arrow keys, when u press space a power bar builds up and when u release it, it "flicks" the coin according to that, but i cant figure out a way to get it to fire in the direction of the rotation, im guessing im gonna have to use trig? please help, ill be finished in when if someone helps

DarkReality
12-16-2004, 08:56 AM
English would be a start? Like... periods.

www.actionscript.org/tutorials has a good tutorial on trig and angle movement. Basically, it's trig. And a bit too much effort for me to want to explain right now, sorry. But that tutorial won't run away. Most likely.

Nexus
12-16-2004, 07:59 PM
Assuming you already have the angle of direction in degrees, you would do something like...

//Calculate directional force
this.vx = Math.sin(angle*(Math.PI/180))*power;
this.vy = -Math.cos(angle*(Math.PI/180))*power;
//Moves coin and applies friction (put in onEnterFrame)
this.vx *= 0.9;
this.vy *= 0.9;
this._x += this.vx;
this._y += this.vy;

The concept behind this isn't hard. I'm suprised there hasn't been a tutorial that makes it easy to understand sine and cosine. Most of the tutorials I see make it way more complicated than it really is.

Hope this helps you out.

Swas
12-23-2004, 05:42 AM
it does kinda theres just a couple of problems with it can u tell me whats wrong with my code, it fires in the direction, if its left it fires down, right up, up right, down left im confused onClipEvent (mouseMove) {
if (this.c1control == true) {
x = this._xmouse;
y = this._ymouse*-1;
angle = Math.atan(y/x)/(Math.PI/180);
if (x<0) {
angle += 180;
}
if (x>=0 && y<0) {
angle += 360;
}
_root.coinOne.rotate._rotation = angle*-1;
}
updateAfterEvent();
}
onClipEvent (enterFrame) {
if (this.c1control == true) {
if (Key.isDown(Key.SPACE)) {
this.vx = Math.sin(angle*(Math.PI/180))*_root.powerbar.scale;
this.vy = -Math.cos(angle*(Math.PI/180))*_root.powerbar.scale;
this.vx *= 0.9;
this.vy *= 0.9;
this._x += 0;
} else {
this._x += this.vx;
this._y += this.vy;
}
}
}



changing a few things around produces the desired effect apart from the weird directions it works in some directions but not others :S it confusing
it now moves in the way i want with the friction applied

onClipEvent (mouseMove) {
if (this.c1control == true) {
x = this._xmouse;
y = this._ymouse*-1;
angle = Math.atan(y/x)/(Math.PI/180);
if (x<0) {
angle += 180;
}
if (x>=0 && y<0) {
angle += 360;
}
_root.coinOne.rotate._rotation = angle*-1;
}
updateAfterEvent();
}
onClipEvent (enterFrame) {
if (this.c1control == true) {
if (Key.isDown(Key.SPACE)) {
this.vx = Math.sin(angle*(Math.PI/180))*_root.powerbar.scale;
this.vy = -Math.cos(angle*(Math.PI/180))*_root.powerbar.scale;
this.vx *= 0.9;
this.vy *= 0.9;

} else {
this.vx *= 0.9;
this.vy *= 0.9;
this._x += this.vx;
this._y += this.vy;
}
}
}

Nexus
12-23-2004, 05:18 PM
What function are you using to get the angle of direction?

If you're using Math.atan2() then I think you need to subtract 90 degrees from the original value. That's why you're getting right as up and such. Try adding 90 or subtracting 90 to the _rotation attribute along with what you are already adding.

Swas
12-24-2004, 05:37 AM
im using math.atan, whats the difference between atan2 and atan, its weird tho because in some directions it goes the right way


i had a go, if i send u it would u be able to have a lil look at it ?

denacioust
12-24-2004, 06:48 AM
Math.atan would be on a calculator just tangent its for finding the tan of an angle. Atan2 is used to find an angle using two lines

Nexus
12-24-2004, 05:17 PM
Math.atan2() is the better version. Math.atan() I believe only returns an angle ranging from 0 to 1/2PI. Math.atan2() returns 0 to PI. (thus achieves full-360 degree angle detection.

The Brown Cow
12-25-2004, 08:22 PM
Math.atan() takes one argument, Math.atan2() takes two.

As far as I know, there's no difference between...
Math.atan(y/x);
and
Math.atan2(y, x);

Strange. Everyone uses atan2 though. Nexus' theory is probably right.

Swas
12-28-2004, 05:50 AM
o, ok well anyway ive use tan, does any1 have any ideas on whats wrong and how i could fix it

Swas
01-02-2005, 08:10 AM
hey i figured out was i did wrong, not coding but very stupid stupid animating errors ?, i had rotated the movie clip on the stage without using action script, thats why it was going in weird directions, heh kinda funny that it caused me so much grief :p thanx anyway thought u might like to know