Posted: 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 :arraycharofboolean 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 endif if keys (KEY_UP_ARROW)then
velocity += .001
moved :=true endif
if moved =falseand velocity >= 0then
velocity -= velocity/100 endif
if x < 0then
x :=maxx endif
if x > maxxthen
x :=0 endif
if y < 0then
y :=maxy endif
if y > maxythen
y :=0 endif
y += velocity *sind(round(degree))
x += velocity *cosd(round(degree)) delay(1) cls endloop
Sponsor Sponsor
Zasalamel
Posted: 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 thanks
Turing_Gamer
Posted: 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.
TerranceN
Posted: 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 thanks
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).
Posted: 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) ?
andrew.
Posted: 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.
Insectoid
Posted: Mon Feb 15, 2010 12:00 pm Post subject: RE:360 degree movement
You learn all this in grade 10 math, by the way.
TerranceN
Posted: 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).
Sponsor Sponsor
Zasalamel
Posted: 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
andrew.
Posted: 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.
Zasalamel
Posted: 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?
TerranceN
Posted: 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:
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 =0and x > 0)then result0.0 elsif(y =0and x < 0)then result180.0 elsif(x =0and y > 0)then result90.0 elsif(x =0and y < 0)then result270.0 else result0.0 endif
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.
Posted: 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.
TerranceN
Posted: 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 =0and x > 0)then result0.0 elsif(y =0and x < 0)then result180.0 elsif(x =0and y > 0)then result90.0 elsif(x =0and y < 0)then result270.0 else result0.0 endif
should have been
Turing:
if(y =0and x > 0)then result0.0 elsif(y =0and x < 0)then result180.0 elsif(x =0and y > 0)then result90.0 elsif(x =0and y < 0)then result270.0 endif
Zasalamel
Posted: Mon Feb 15, 2010 3:36 pm Post subject: RE:360 degree movement
Thanks that may help i tried it the other way and all i seemed to get was 1 (with cosd) or 0 with sind (and 0 by itself)