Computer Science Canada

360 degree movement

Author:  Insectoid [ Tue Dec 08, 2009 1:37 pm ]
Post subject:  360 degree movement

Who'da thought programming had anything to do with math? /sarcasm

So, I decided it was time to do something with all the trigonometry school is shoving down my throat, so I re-invented the wheel and developed my own 360-degree movement simulator (which I assume is the same as the way everyone else does it). Tired of using x and y velocity to create a very difficult to control movement simulation, I used sine and cosine functions on a combined velocity based on a directional degree. Anyway, the fruits of my labour (if ten minutes can be called labour);

Turing:

var x : real := maxx / 2
var y : real := maxy / 2
var degree : real := 0
var keys : array char of boolean
var velocity : real := 0
var moved : boolean
View.Set ("offscreenonly")
loop
    moved := false
    Draw.FillArc (round (x), round (y), 10, 10, round (degree+22.5), round (degree -22.5), blue)
    View.Update
    Input.KeyDown (keys)
    if keys (KEY_RIGHT_ARROW) then
        degree -= 1
    elsif keys (KEY_LEFT_ARROW) then
        degree += 1
    end if
    if keys (KEY_UP_ARROW) then
    velocity += .001
    moved := true
end if
if moved = false and velocity >= 0 then
    velocity -= velocity/100
end if
if x < 0 then
    x := maxx
end if
if x > maxx then
    x := 0
end if
if y < 0 then
    y := maxy
end if
if y > maxy then
    y := 0
end if
y += velocity * sind (round (degree))
x += velocity * cosd (round (degree))
delay (1)
cls
end loop

Author:  Zasalamel [ Sun Feb 14, 2010 4:48 pm ]
Post subject:  RE:360 degree movement

insectoid, i am a little bit new to the 360 degree movement, is it possible for you to explain this to me? this seems a little confusing Razz thanks

Author:  Turing_Gamer [ Sun Feb 14, 2010 4:53 pm ]
Post subject:  Re: 360 degree movement

Why don't you check the Turing Help (F10 -> Search 'Pic.Rotate')...
It has a rotating house.

Author:  TerranceN [ Sun Feb 14, 2010 6:34 pm ]
Post subject:  Re: RE:360 degree movement

Zasalamel @ Sun Feb 14, 2010 4:48 pm wrote:
insectoid, i am a little bit new to the 360 degree movement, is it possible for you to explain this to me? this seems a little confusing Razz thanks


I'll try to explain it.

The two most important lines are
Turing:
y += velocity * sind (round (degree))
x += velocity * cosd (round (degree))


This is what decides where to move to next. It finds the speed (which is incorrectly labeled velocity here) multiplied by the direction and adds that to the position.

The direction is just two values, a distance on the x and y axis, whose length is ALWAYS 1. This is why when you multiply the direction by a speed, the reulting values will always move speed distance away. In turing, the direction can be found with cosine(angle) on the x-axis and sine(angle) on the y-axis.

I attached a program that shows the different direction values you get at different angles.

Hope that helps.

@Turing_Gamer: That is not the same thing, as you need to know the math behind it in order to move something in that direction for example (AFAIK).

Author:  Zasalamel [ Mon Feb 15, 2010 11:23 am ]
Post subject:  RE:360 degree movement

Thank you, I was not sure haha. Is there a significant difference between using cosine or sine on the x and y axis? or :
cosd(x) = sind(x) ?

Author:  andrew. [ Mon Feb 15, 2010 11:49 am ]
Post subject:  RE:360 degree movement

Well cosx =/= sinx. He's using basic trig to find the x and y components of velocity. Basically, there's a right angle triangle with one of the other angles as x. We know the hypotenuse (it is the constant speed) and we know what x is. From this, we can use sin and cos to find the other 2 sides (the x and y sides). We do this by using basic trig (SOHCAHTOA); sinx = O/H and cosx = A/H. We need to find O and A so we solve for it. O = H*sinx and A = H*cosx. Well, since O is the same as the yspeed, A is the same as the x speed, and H is the same as the resulting speed, then the equations become:

xspeed = speed*cosd(x)
yspeed = speed*sind(x)

I hope that helps and I hope I got it right.

Author:  Insectoid [ Mon Feb 15, 2010 12:00 pm ]
Post subject:  RE:360 degree movement

You learn all this in grade 10 math, by the way.

Author:  TerranceN [ Mon Feb 15, 2010 12:08 pm ]
Post subject:  RE:360 degree movement

Also Zasalamel, you should look at the program I posted. At the bottom of the screen it says what the cosine and sine of the current angle are and it is color coded to show you where they fit in to finding the components of an angle.

@andrew.
Ya you got it right, but all of that assumes the angle is in standard position (0degrees = right, angle increases ccw). You could also use sine for x and cosine for y, but then 0degrees would be up, and it would increase cw).

Author:  Zasalamel [ Mon Feb 15, 2010 12:27 pm ]
Post subject:  RE:360 degree movement

i did but it still confuses me with the graphic applications of it Razz

Author:  andrew. [ Mon Feb 15, 2010 1:57 pm ]
Post subject:  Re: RE:360 degree movement

TerranceN @ Mon Feb 15, 2010 12:08 pm wrote:
@andrew.
Ya you got it right, but all of that assumes the angle is in standard position (0degrees = right, angle increases ccw). You could also use sine for x and cosine for y, but then 0degrees would be up, and it would increase cw).
Yeah true. I've always thought of the triangle angles starting from the right, that's why I did it like that.

Edit: I finally got a chance to run the code. LOL, there is no limit on how fast you can go. It just keeps accelerating. You should set a terminal velocity on it.

Author:  Zasalamel [ Mon Feb 15, 2010 2:13 pm ]
Post subject:  RE:360 degree movement

Also i am working on a small game with 360 degree shooting. I am at a complete blank as to how i can get the line of fire to follow the mouse. Can anyone explain to me how to do this? So basically the keyword for me is how can i get the bullet to shoot towards the mouse?

Author:  TerranceN [ Mon Feb 15, 2010 2:39 pm ]
Post subject:  Re: 360 degree movement

If you take the arctangent of the slope you get the angle. The problem is that you have to make sure the neither y nor x of the slope is 0, or else you have to handle them manualy. Also when you find arctangent it can only give you an angle from -90 to 90 so you have to check if the mouse x is less than the player x, and if so, subtract 180 from the angle.

I will quickly write a common function most languages have built in, called atan2. This takes the y and x of the slope and outputs an angle, accounting for all the exceptions. When we wre done it will be used like this:

Turing:
angle := atan2(mouseY - playerY, mouseX - playerX)


First thing, write the shell of the function

Turing:
fcn atan2(y : real, x : real) : real
    % We will add code here
end atan2


Next we have to handle y or x being 0.

Turing:
if (y = 0 and x > 0) then
    result 0.0
elsif (y = 0 and x < 0) then
    result 180.0
elsif (x = 0 and y > 0) then
    result 90.0
elsif (x = 0 and y < 0) then
    result 270.0
else
    result 0.0
end if


The next problem with our function is that it can only return -90 to 90, so we need to check if x is less than 0 (meaning that the mouse is on the left side of that player, and the angle will be -90 to -270) and if it is we can subtract 180 to give us angles on the other side. Finally we use the mod command to make the angle between 0 and 360.

Turing:
if (x < 0) then
    result (arctand(y / x) - 180) mod 360
end if


But what if x >= 0? We need to put a generic return at the bottom, so if there are no exceptions, it will still return an angle.

Turing:
result (arctand(y/x)) mod 360


Then our function is complete!

Hope that helps.

Author:  Zasalamel [ Mon Feb 15, 2010 2:51 pm ]
Post subject:  RE:360 degree movement

What i am trying to do is have a bullet/item shoot (in a strait line) towards the mouse.

Author:  TerranceN [ Mon Feb 15, 2010 3:19 pm ]
Post subject:  RE:360 degree movement

From reading these posts you should be able to find the direction to the mouse, and you should know how to make something move in a direction, so I don't see what else I can really tell you without doing it for you.

You need a position and angle for the player, and a position and angle for your bullet. If the player clicked the mouse, move the bullet to the player and set the bullet's angle to the player's angle. Then, every frame you find the angle from the player to the mouse (which will be the player's angle), and move the bullet in the direction of its own angle.

Hope that helps.

EDIT : Whoops my last post should not have had an else block

Turing:
if (y = 0 and x > 0) then
    result 0.0
elsif (y = 0 and x < 0) then
    result 180.0
elsif (x = 0 and y > 0) then
    result 90.0
elsif (x = 0 and y < 0) then
    result 270.0
else
    result 0.0
end if


should have been

Turing:
if (y = 0 and x > 0) then
    result 0.0
elsif (y = 0 and x < 0) then
    result 180.0
elsif (x = 0 and y > 0) then
    result 90.0
elsif (x = 0 and y < 0) then
    result 270.0
end if

Author:  Zasalamel [ Mon Feb 15, 2010 3:36 pm ]
Post subject:  RE:360 degree movement

Thanks that may help Razz i tried it the other way and all i seemed to get was 1 (with cosd) or 0 with sind (and 0 by itself)

Author:  Zasalamel [ Mon Feb 15, 2010 3:42 pm ]
Post subject:  RE:360 degree movement

thank you TerranceN it works now! I did not expect it to be so much work for that...Razz

Author:  zero-impact [ Mon Feb 15, 2010 11:38 pm ]
Post subject:  RE:360 degree movement

you might also want to look into vectors. They can simplify a lot of these calculations. As well as making them less computationally expensive. http://en.wikipedia.org/wiki/Euclidean_vector

Author:  SNIPERDUDE [ Mon Feb 15, 2010 11:46 pm ]
Post subject:  RE:360 degree movement

And in long-term can help you delve into 3D once you want to start.

Author:  USEC_OFFICER [ Tue Feb 16, 2010 12:36 pm ]
Post subject:  RE:360 degree movement

Or you can just dive into 3D without vectors, it is really just adding the z-axis, although learning vectors will always help.

Author:  SNIPERDUDE [ Tue Feb 16, 2010 1:45 pm ]
Post subject:  RE:360 degree movement

As long as they don't require any rotation.

Author:  Zasalamel [ Tue Feb 16, 2010 5:33 pm ]
Post subject:  RE:360 degree movement

I seem to hear a possible tutorial on vectors and advanced calculation in turing??? Someone?

Author:  TheGuardian001 [ Tue Feb 16, 2010 5:49 pm ]
Post subject:  Re: 360 degree movement

Vectors and calculus don't really have much material that can be applied specifically to a single language. It's a pretty wide subject (it occupies both it's own subject as well as a large chunk of grade 12 physics) that probably couldn't be done justice through a tutorial (although I suppose anybody's welcome to try.)

Author:  SNIPERDUDE [ Tue Feb 16, 2010 9:57 pm ]
Post subject:  RE:360 degree movement

Agreed, I think it would be better put in a section such as General Programming or the like.

Author:  TerranceN [ Tue Feb 16, 2010 10:18 pm ]
Post subject:  Re: RE:360 degree movement

Zasalamel @ Tue Feb 16, 2010 5:33 pm wrote:
I seem to hear a possible tutorial on vectors and advanced calculation in turing??? Someone?


You already WERE using vectors. A vector is just a direction multiplied by a magnitude (we used speed here). It is just a little less distinguishable in cartesian coordinates.

Also, the link zero-impact posted is really useful. Normally, wikipedia is way too advanced for me, but this article is (for the most part) very clear and easy to understand.

Author:  USEC_OFFICER [ Wed Feb 17, 2010 12:42 pm ]
Post subject:  RE:360 degree movement

That's vectors??? I though it was trig!

Author:  TheGuardian001 [ Wed Feb 17, 2010 2:59 pm ]
Post subject:  Re: 360 degree movement

Vectors and trig are very closely related. A vector is just a magnitude and a direction. In this case the speed acts as the magnitude, and the trig gives you the correct direction.

Author:  syntax_error [ Wed Feb 17, 2010 2:59 pm ]
Post subject:  Re: RE:360 degree movement

USEC_OFFICER @ Wed Feb 17, 2010 12:42 pm wrote:
That's vectors??? I though it was trig!


Really? O-M-G-!
I know I thought that too!
All those wait whats that word again.. theta's ...
Golly goss and sin and cos are there too!

Let me not even brother to try to read the article and just post the FIRST THING THAT COMES TO MIND!

JUST LIKE AN IRC, boy I'm sure TerranceN will now learn the error of his/her ways after all!

If you remotely look at the math you will see that trig functions are being used as a tool to perform transformations to the unit vectors within the matrices.

So yes, there are trig functions present and vectors and matrices, while we're at it there is also addition and subtraction as well!

Just because in integration by parts you have uv - integral of vdu doesnt mean you call it subtraction it still falls under integration.

EDIT: Damn TheGuardian001 beat me to it.

Author:  TerranceN [ Wed Feb 17, 2010 4:04 pm ]
Post subject:  Re: 360 degree movement

@USEC_OFFICER:
Like what TheGuardian001 said, the trig is just a means to an end. You should look at the polar coordinate system, vectors (at least for 2d programming purposes) are like polar coordinates. All we did was convert them to cartesian coordinates.

@syntax_error:
That was a little harsh Shocked; and I never knew Terrance was a gender neutral name.

Also I don't understand what you mean by this:
"Just because in integration by parts you have uv - integral of vdu doesnt mean you call it subtraction it still falls under integration."
Do you mean that adding vectors is still vectors, not adding?

Author:  Zasalamel [ Wed Feb 17, 2010 4:29 pm ]
Post subject:  Re: RE:360 degree movement

TerranceN @ Tue Feb 16, 2010 10:18 pm wrote:
Zasalamel @ Tue Feb 16, 2010 5:33 pm wrote:
I seem to hear a possible tutorial on vectors and advanced calculation in turing??? Someone?


You already WERE using vectors. A vector is just a direction multiplied by a magnitude (we used speed here). It is just a little less distinguishable in cartesian coordinates.

Also, the link zero-impact posted is really useful. Normally, wikipedia is way too advanced for me, but this article is (for the most part) very clear and easy to understand.


I knew that. i was implying that someone should write a tutorial involving this.

Author:  SNIPERDUDE [ Wed Feb 17, 2010 5:27 pm ]
Post subject:  RE:360 degree movement

There are some great tutorials online, but sometimes harder to find. Especially syntax-neutral.
I do think we need some more physics-based tutorials on here, it will also really help out when ISU time comes and people post similar physics-based questions.
Unfortunately I don't have the time to do any, but I'll see what I can do. First I'll take a look around the site to see what's already up. Perhaps we need some time of unified TOC such as the Turing Walkthrough, but focused on non-language topics.
Just some thoughts, I haven't spent too much time in the tutorials sections to be honest.

Author:  USEC_OFFICER [ Wed Feb 17, 2010 8:14 pm ]
Post subject:  RE:360 degree movement

Two things:

First: I'm lazy (big surprise there)

Second: Wikipedia doesn't always load on m school computers. (Guess if it did.)

Third: Sorry for adding an exta thing.

Fourth: Sorry for interupting.

Fifth: There should be a tutorial for vectors/trig. I would be surprised if someone didn't make one already.

Author:  SNIPERDUDE [ Wed Feb 17, 2010 11:17 pm ]
Post subject:  RE:360 degree movement

USEC_OFFICER wrote:
Two things:

Good math there gilligan.

Anyhow I do believe I have seen a tutorial for that somewhere here, a fairly decent one too if I remember right.

Author:  USEC_OFFICER [ Thu Feb 18, 2010 12:39 pm ]
Post subject:  RE:360 degree movement

The third thing should be sorry about adding an extra (Notice the r) thing.

Six: Sorry about not noticing that.


: