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

  • SEARCH
  • Home
  • 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 7496345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T18:34:33+00:00 2026-05-29T18:34:33+00:00

Working with box2d and cocos2d, I’ve calculated two vectors: one is the vector pointing

  • 0

Working with box2d and cocos2d, I’ve calculated two vectors:

  • one is the vector pointing in the direction the car is travelling. (center of the vehicle to it’s north point).
  • two is the vector pointing in the direction of the target. (center to the vehicle to the target center.)

I need to rotate the vehicle to also point in the direction of the target. This is done by setting the vehicle’s steering either 90 degrees to turn right or -90 to turn left.

At the moment i’m querying the box2d body of the vehicle for its angle, then calculating the angle of the target direction vector using:

if(vector.y == 0){ vector.y = 0.000001f; }
float baseRadians = atan(vector.x/vector.y);
if(vector.y < 0){ baseRadians += PI_CONSTANT; } //Adjust for -Y
return -1 * baseRadians;

The following is crude and where I need help…!

I then compare the angle of the vehicle against the angle returned from the direction vector and set the steering as follows:

if(vehicleAngle < targetAngle)
        {
            steeringInput = -90.0;    
        }
        else if(vehicleAngle > targetAngle)
        {
            steeringInput = 90.0;    
        }
        else
        {
            steeringInput = 0.0;     
        }

The problem is two fold:

  • The target angle jumps from -4.71 to 1.57 radians, causing the vehicle to switch to the wrong direction
  • The vehicle angle is continous and keeps increasing rather than staying within a range.

Is there a way to get both the target vector and the vehicle angle in a set range where, for example, if the target angle is 0-360 and the van angle is 0-360, i can then compare them accurately?

Any other way to do this…? Any help 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-05-29T18:34:34+00:00Added an answer on May 29, 2026 at 6:34 pm

    Use atan2 instead of atan in your code. It gets the angle from one object to another.

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

Sidebar

Related Questions

I am working with cocos2d + box2d and as I look through the 'Hello
I'm working on a platformer for the iPhone that is using Box2D and cocos2D.
Hi all im working on an ipad app using cocos2d objective c and box2d.
I have two classes, using Box2d,Cocos2d. Construct.mm and Level1.mm Construct contains all the methods
Currently I am working on a cocos2d+Box2D project so I have deal with some
I'm working on a Cocos2D and Box2D game. I just created a class for
I recently ran across this code in one of the projects I'm working on,
I'm working on a educational physics project and I've chosen Box2D as my main
I am new to Objective-C. I am currently working on a game using Cocos2D
Greetings stackoverflow community! I am working on an Android game that uses Box2D for

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.