[as3] Trigonometry is confusing

Started by
19 comments, last by Mathimetric 11 years, 1 month ago

Hi, first time posting here. I'm having problems with some trigonometry calculations in as3. Here's the swf file, i can move the ship forwards and backwards but things get weird when i try to move it sideways.

Here's the code;


//this moves the ship forward
vx += Math.cos(rotation* Math.PI / 180) * 3;
vy += Math.sin(rotation* Math.PI / 180) * 3;

//and this should move the ship to its right
vx += Math.cos((rotation+90)* Math.PI / 180) * 3;
vy += Math.sin((rotation+90)* Math.PI / 180) * 3;

I have no idea what i'm doing with these stuff i just took the calculations from a tutorial. Why this isnt working?

Thanks in advance!

ps: Here's some additional info;


//ship's location is updated like this in every tick
x+=vx;
y+=vy;

//and i calculate the angle of the ship like this;
rotation = (Math.atan2(y-stageRef.mouseY,x-stageRef.mouseX)*180/Math.PI)+180;

Advertisement

Hi there smile.png

Your mathematics is perfectly legitimate here, everything is as it should be at a glance.


It might be fixed ( it's a bit "meh" ) by making sure you calculate the angle at each tick BEFORE doing the movement.

Hope it helps. biggrin.png

Daniel Kruyt

what's the problem, the swf appears to behave perfectly correctly.

your constantly pointing the ship at the mouse, so when you go to move right, you do move right, but then you rotate the ship to face the mouse, move, rotate....etc

if you want to be able to move linearly from a position left or right, then when u need to offset what you are looking at but the distance you've moved left/right.

edit: alternativly, only set the ship to face the mouse when the mouse moves, this would allow the ship to move left/right, but when you move your mouse, the ship snaps back to looking at the mouse.

Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

If you plan on making games it's well worth your time to look up some basic trig and see what those functions are doing and why.

I also don't see a problem here. Can you explain more precisely what it's not doing correctly?

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

Nature of Code book was super helpful for me here -

http://natureofcode.com/

He's got some really great vector math and physics stuff I found super useful.

I think you probably don't want to spiral outwards when moving right or left.

You can do that fairly easily with the setup you have now... have a forward/backward speed stored, and a clockwise/counterclockwise one too, that act independently.

Since you always face the centre point, do moving forwards and backwards as you do now, but when you have a clockwise/counterclockise component to your velocity, rotate around the focus point the correct amount of degrees to match the given speed. You can work out how many degrees to travel since you know moving 360 degrees would be a speed of 2 * PI * r where r is the radius of the circle you travel around (i.e. distance between the ship and the focus point).

Does that make sense?

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

Awesome site for trig here

http://www.mathsisfun.com/algebra/trigonometry.html

If this post or signature was helpful and/or constructive please give rep.

// C++ Video tutorials

http://www.youtube.com/watch?v=Wo60USYV9Ik

// Easy to learn 2D Game Library c++

SFML2.2 Download http://www.sfml-dev.org/download.php

SFML2.2 Tutorials http://www.sfml-dev.org/tutorials/2.2/

// Excellent 2d physics library Box2D

http://box2d.org/about/

// SFML 2 book

http://www.amazon.com/gp/product/1849696845/ref=as_li_ss_tl?ie=UTF8&camp=1789&creative=390957&creativeASIN=1849696845&linkCode=as2&tag=gamer2creator-20

what's the problem, the swf appears to behave perfectly correctly.

If you plan on making games it's well worth your time to look up some basic trig and see what those functions are doing and why.

I also don't see a problem here. Can you explain more precisely what it's not doing correctly?

I updated it yesterday but its still broken. I don't want the ship to move away from the mouse when i'm only pressing "a" or "d" buttons.

Nature of Code book was super helpful for me here -

http://natureofcode.com/

He's got some really great vector math and physics stuff I found super useful.

I'ts seems nice and cheap gonna check it out thanks!

Since you always face the centre point, do moving forwards and backwards as you do now, but when you have a clockwise/counterclockise component to your velocity, rotate around the focus point the correct amount of degrees to match the given speed. You can work out how many degrees to travel since you know moving 360 degrees would be a speed of 2 * PI * r where r is the radius of the circle you travel around (i.e. distance between the ship and the focus point).

Does that make sense?

Uhm.. I'm not very good at english and when its combined with trigonometry it makes no sense to me. I cant understand the last sentence. I can calculate the distance between two points but i dont know what should i do with it.

Edit: By the way sorry for the late response i was waiting to get a mail from gamedev.net informing me when i got some answers, it didnt happen.

Edit2: I uploaded the source here if it helps. Some parts are in turkish but i can translate the source if needed.

Rotate around in a circle centred on the focus point but keep the same distance away.

You can work out the angle you are at with respect to the centre of the circle using atan2.

Then set your new position to be the same distance away from the focus point but offset the angle by an amount based on the speed you are moving at and the radius (distance from the centre).

If you are 100 units away from the centre point (call this r), a complete circle would be 2 * PI * r = 200PI circumference. You want to move 10 units, so you move 10 / (200 * PI) = 1/(20 * PI) of a circle = 0.0159 of a full circle i.e. 0.0159 * 360 degrees = 5.72 degrees.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

You can work out the angle you are at with respect to the centre of the circle using atan2.

Dont i know the angle already? From the rotation variable?

This topic is closed to new replies.

Advertisement