0

In the accepted answer on this question: Java game - how to stop player from walking over obstacles I came across this code:

xSpeed = Math.cos(direction) * speed;
ySpeed = Math.sin(direction) * speed;

What does this do? I read (and guessed of course, because of the variable names) that it translates a direction and speed to an x speed and y speed. But what are sin and cos for? What kind of direction does this accept? Because AlekHalfHeart gave no further explanation I take it this is a quite common snippet, but I have never seen it before.

Community
  • 1
  • 1
11684
  • 7,356
  • 12
  • 48
  • 71
  • 2
    With no disrepect intended, if you don't understand the basics of trigonometry, then you shouldn't yet be attempting to write a game that involves geometry... – Oliver Charlesworth Jan 06 '13 at 14:14
  • I do understand the basics of trigonometry, I just forgot what sine and cosine are and I only ever used them for calculating angles of triangles. Could you try to formulate an answer? I think I could understand that if you don't use very math specific terminology. @OliCharlesworth – 11684 Jan 06 '13 at 14:18
  • If you draw a diagram of what is happening, you will get a triangle! ;) – Oliver Charlesworth Jan 06 '13 at 14:18
  • Maybe read a little bit about calculating the Cartesian parts of a vector. Here's [an article with an image](http://zonalandeducation.com/mstm/physics/mechanics/forces/forceComponents/forceComponents.html) at "Force vector component mathematics" (the idea also applies to speed). – pimvdb Jan 06 '13 at 14:25
  • @OliCharlesworth I think I'm not as stupid as you think, I just use degrees and AlekHalfHeart used radians, so I didn't really expect my image to go up when I set direction to 180. – 11684 Jan 06 '13 at 14:26
  • And, I didn't expect the zero point of the circle to be on the right. I thought it was at the top. – 11684 Jan 06 '13 at 14:27
  • This is an easy problem if you understand 2D vectors. You need to know that velocity, like displacement, acceleration, and force, are all vector quantities. They have a magnitude and direction. – duffymo Jan 06 '13 at 15:21
  • 2
    "I do understand" and "I forgot what sine and cosine are" are mutually exclusive statements. – duffymo Jan 06 '13 at 15:38
  • @duffymo Perhaps 'I did understand' is better? But now, I do understand again: I answered my own question. – 11684 Jan 08 '13 at 10:11

2 Answers2

1

I think I figured it out thanks to Oli Charlesworth's comment 'If you draw a diagram of what is happening, you will get a triangle! ;)' and a fair amount of diagrams. I'll try to explain it, but I've never had math in English (I'm Dutch), so I don't know the appropriate mathematical terms for everything, but I'll try:

I first didn't understand where O. Charlesworth's triangle was. But after a few drawings I figured out that the line of the movement the 'player' makes forms a triangle with the x axis or the y axis.
After that I still didn't understand what sine and cosine did there, but after rereading and generalising my math book I figured out that the the relation between the x-axis and the hypothenuse (the movement of the player - hey, I knew the English word for it!) equaled the distance along the x-axis traveled by the 'player'. Fortunately (but not coincidentally, it's MATHS), this is exactly what the cosine returns. If you replace x-axis with y-axis and cosine with sine, you got my explanation of why ySpeed = ... works.

I fear my English explanation is not entirely correct, but that's because of my English skills, not my maths skills. I hope it helps someone.

PS I now realize my question was really vague. It's a miracle it isn't already closed.

11684
  • 7,356
  • 12
  • 48
  • 71
0

This is mathematical or physics background so take a look at this example Vector components. Cos and Sin are used to calculate X and Y components of vector or movement in in coordinate system. Direction is an angle and movement is vector so after translating it on X and Y components they are multiplied with speed, so finally you got X and Y component of speed. I am not professor of Mathematics so I hope that you understand :)

ttokic
  • 116
  • 1
  • 7