Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9095635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T23:39:17+00:00 2026-06-16T23:39:17+00:00

In the accepted answer on this question: Java game – how to stop player

  • 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T23:39:18+00:00Added an answer on June 16, 2026 at 11:39 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using the accepted answer from this question . The relevant code is
I was reading over this question and wondered if the accepted answer might also
Possible Duplicate: synchronized block vs synchronized method? From accepted answer to this question: In
Based the accepted answer to this question I've setup a NetBeans/tomcat environment. In testing
I'm using the accepted answer to this question to help me build an HTML
This is basically a duplicate of this question , but the accepted answer was
I was trying to answer this question. As suggested by the accepted answer, the
Im using the accepted answer in this question . As I have the same
This is a question of curiosity about accepted coding practices. I'm (primarily) a Java
The accepted answer in this question: Calculate business days is buggy. Just check echo

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.