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 9048439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T12:09:25+00:00 2026-06-16T12:09:25+00:00

I am currently working on a game using javascript and processing.js and I am

  • 0

I am currently working on a game using javascript and processing.js and I am having trouble trying to figure out how to move stuff diagonally. In this game, there is an object in the center that shoots other objects around it. Now I have no problem moving the bullet only vertically or only horizontally, however I am having difficulty implementing a diagonal motion for the bullet algorithm.

In terms of attempts, I tried putting on my math thinking cap and used the y=mx+b formula for motion along a straight line, but this is what my code ends up looking like:

ellipse(shuriken.xPos, shuriken.yPos, shuriken.width, shuriken.height); //this is what I want to move diagonally

    if(abs(shuriken.slope) > 0.65) {
        if(shuriken.targetY < shuriken.OrigYPos) {
            shuriken.yPos -= 4;
        } else {
            shuriken.yPos += 4;
        }
        shuriken.xPos = (shuriken.yPos - shuriken.intercept)/shuriken.slope;

    } else {
        if(shuriken.targetX < shuriken.OrigXPos) {
            shuriken.xPos -= 4;
        } else {
            shuriken.xPos += 4;
        }
        shuriken.yPos = shuriken.slope * shuriken.xPos + shuriken.intercept;
    }

The above code is very bad and hacky as the speed varies with the slope of the line.

I tried implementing a trigonometry relationship but still in vain.

Any help/advice will be greatly appreciated!

  • 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-16T12:09:26+00:00Added an answer on June 16, 2026 at 12:09 pm

    Think of it this way: you want the shuriken to move s pixels. If the motion is horizontal, it should move s pixels horizontally; if vertical, s pixels vertically. However, if it’s anything else, it will be a combination of pixels horizontally/vertically. What’s the correct combination? Well, what shape do you get if you project s distance in any direction from a given point? That’s right, a circle with radius s. Let’s represent the direction in terms of an angle, a. So we have this picture:

    image

    How do we get the x and the y? If you notice, we have a triangle. If you recall your trigonometry, this is precisely what the sine, cosine, and tangent functions are for. I learned their definitions via the mnemonic SOHCAHTOA. That is: Sin (a) = Opposite/Hypotenuse, Cos(a) = Adjacent/Hypotenuse, Tan(a) = Opposite/Adjacent. In this case, opposite of angle a is y, and adjacent of angle a is x. Thus we have:

    cos(a) = x / s
    sin(a) = y / s
    

    Solving for x and y:

    x = s * cos(a)
    y = s * sin(a)
    

    So, given the angle a, and that you want to move your shuriken s pixels, you want to move it s * cos(a) horizontally and s * sin(a) vertically.

    Just be sure you pass a in radians, not degrees, to javascript’s Math.sin and Math.cos functions:

    radians = degrees * pi / 180.0
    

    This may be why your trigonometric solution didn’t work as this has bitten me a bunch in the past.

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

Sidebar

Related Questions

I'm working on a game using OpenTK in C#. Currently trying to implement the
I'm currently working on a Poker game for C++, and I'm having trouble properly
When dealing with multiple classes in a Javascript/NodeJS game, I'm having trouble figuring out
I am currently working on designing my first FPS game using JOGL. (Java bindings
I am currently working on a small personal multiplayer game project. I am using
Salut, I'm currently working on a pool game in flash, using box2d for physics
I am new to Objective-C. I am currently working on a game using Cocos2D
I am currently working on creating an import-based pipeline for my indie game using
I'm currently working on a web app/game using C# and ASP.NET(3.5). However, I'm running
I am working on a game in OpenGL using libGDX on Android. I currently

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.