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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:40:12+00:00 2026-05-25T09:40:12+00:00

I’ve been working on a top down car game for quite a while now,

  • 0

I’ve been working on a top down car game for quite a while now, and it seems it always comes back to being able to do one thing properly. In my instance it’s getting my car physics properly done.

I’m having a problem with my current rotation not being handled properly. I know the problem lies in the fact that my magnitude is 0 while multiplying it by Math.cos/sin direction, but I simply have no idea how to fix it.

This is the current underlying code.

private void move(int deltaTime) {

double secondsElapsed = (deltaTime / 1000.0);// seconds since last update
double speed = velocity.magnitude();
double magnitude = 0;

if (up)
    magnitude = 100.0;
if (down)
    magnitude = -100.0;
if (right)
    direction += rotationSpeed * (speed/topspeed);// * secondsElapsed;
if (left)
    direction -= rotationSpeed * (speed/topspeed);// * secondsElapsed;

double dir = Math.toRadians(direction - 90);
acceleration = new Vector2D(magnitude * Math.cos(dir), magnitude * Math.sin(dir));

Vector2D deltaA = acceleration.scale(secondsElapsed); 
velocity = velocity.add(deltaA); 

if (speed < 1.5 && speed != 0)
    velocity.setLength(0);

Vector2D deltaP = velocity.scale(secondsElapsed); 
position = position.add(deltaP);

...
}

My vector class emulates vector basics – including addition subtraction, multiplying by scalars… etc.

To re-iterate the underlying problem – that is magnitude * Math.cos(dir) = 0 when magnitude is 0, thus when a player only presses right or left arrow keys with no ‘acceleration’ direction doesn’t change.

If anyone needs more information you can find it at

http://www.java-gaming.org/index.php/topic,23930.0.html

  • 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-05-25T09:40:12+00:00Added an answer on May 25, 2026 at 9:40 am

    Yes, those physics calculations are all mixed up. The fundamental problem is that, as you’ve realized, multiplying the acceleration by the direction is wrong. This is because your “direction” is not just the direction the car is accelerating; it’s the direction the car is moving.

    The easiest way to straighten this out is to start by considering acceleration and steering separately. First, acceleration: For this, you’ve just got a speed, and you’ve got “up” and “down” keys. For that, the code looks like this (including your threshold code to reduce near-zero speeds to zero):

    if (up)
        acceleration = 100.0;
    if (down)
        acceleration = -100.0;
    
    speed += acceleration * secondsElapsed;
    
    if (abs(speed) < 1.5) speed = 0;
    

    Separately, you have steering, which changes the direction of the car’s motion — that is, it changes the unit vector you multiply the speed by to get the velocity. I’ve also taken the liberty of modifying your variable names a little bit to look more like the acceleration part of the code, and clarify what they mean.

    if (right)
        rotationRate = maxRotationSpeed * (speed/topspeed);
    if (left)
        rotationRate = maxRotationSpeed * (speed/topspeed);
    
    direction += rotationRate * secondsElapsed;
    
    double dir = Math.toRadians(direction - 90);
    velocity = new Vector2D(speed * Math.cos(dir), speed * Math.sin(dir));
    

    You can simply combine these two pieces, using the speed from the first part in the velocity computation from the second part, to get a complete simple acceleration-and-steering simulation.

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

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.