Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 360 degree movement
Index -> Programming, Turing -> Turing Submissions
Goto page 1, 2, 3  Next
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Insectoid




PostPosted: 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
Sponsor
Sponsor
Sponsor
sponsor
Zasalamel




PostPosted: 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
Turing_Gamer




PostPosted: 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




PostPosted: 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).



UnitCircles.zip
 Description:

Download
 Filename:  UnitCircles.zip
 Filesize:  334.6 KB
 Downloaded:  111 Time(s)

Zasalamel




PostPosted: 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.




PostPosted: 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




PostPosted: 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




PostPosted: 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
Sponsor
sponsor
Zasalamel




PostPosted: 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
andrew.




PostPosted: 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




PostPosted: 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




PostPosted: 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.
Zasalamel




PostPosted: 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




PostPosted: 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
Zasalamel




PostPosted: 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)
Display posts from previous:   
   Index -> Programming, Turing -> Turing Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 3  [ 33 Posts ]
Goto page 1, 2, 3  Next
Jump to:   


Style:  
Search: