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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T23:41:57+00:00 2026-05-11T23:41:57+00:00

I would like to use Cocos2d on the iPhone to draw a 2D car

  • 0

I would like to use Cocos2d on the iPhone to draw a 2D car and make it steer from left to right in a natural way.

Here is what I tried:

  1. Calculate the angle of the wheels and just move it to the destination point where the wheels point to. But this creates a very unnatural feel. The car drifts half the time

  2. After that I started some research on how to get a turning circle from a car, which meant that I needed a couple of constants like wheelbase and the width of the car.

After a lot of research, I created the following code:

float steerAngle = 30; // in degrees
float speed = 20;
float carWidth = 1.8f; // as in 1.8 meters
float wheelBase = 3.5f; // as in 3.5 meters

float x = (wheelBase / abs(tan(steerAngle)) + carWidth/ 2);
float wheelBaseHalf = wheelBase / 2;
float r = (float) sqrt(x * x + wheelBaseHalf * wheelBaseHalf);

float theta = speed * 1 / r;
if (steerAngle < 0.0f)
    theta = theta * -1;

drawCircle(CGPointMake(carPosition.x - r, carPosition.y),
           r, CC_DEGREES_TO_RADIANS(180), 50, NO);

The first couple of lines are my constants. carPosition is of the type CGPoint. After that I try to draw a circle which shows the turning circle of my car, but the circle it draws is far too small. I can just make my constants bigger, to make the circle bigger, but then I would still need to know how to move my sprite on this circle.

I tried following a .NET tutorial I found on the subject, but I can’t really completely convert it because it uses Matrixes, which aren’t supported by Cocoa.

Can someone give me a couple of pointers on how to start this? I have been looking for example code, but I can’t find any.

EDIT After the comments given below
I corrected my constants, my wheelBase is now 50 (the sprite is 50px high), my carWidth is 30 (the sprite is 30px in width).

But now I have the problem, that when my car does it’s first ‘tick’, the rotation is correct (and also the placement), but after that the calculations seem wrong.

The middle of the turning circle is moved instead of kept at it’s original position. What I need (I think) is that at each angle of the car I need to recalculate the original centre of the turning circle. I would think this is easy, because I have the radius and the turning angle, but I can’t seem to figure out how to keep the car moving in a nice circle.

Any more pointers?

  • 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-11T23:41:57+00:00Added an answer on May 11, 2026 at 11:41 pm

    You have the right idea. The constants are the problem in this case. You need to specify wheelBase and carWidth in units that match your view size. For example, if the image of your car on the screen has a wheel base of 30 pixels, you would use 30 for the WheelBase variable.

    This explains why your on-screen circles are too small. Cocoa is trying to draw circles for a tiny little car which is only 1.8 pixels wide!

    Now, for the matter of moving your car along the circle:

    The theta variable you calculate in the code above is a rotational speed, which is what you would use to move the car around the center point of that circle:

    Let’s assume that your speed variable is in pixels per second, to make the calculations easier. With that assumption in place, you would simply execute the following code once every second:

    // calculate the new position of the car
    newCarPosition.x = (carPosition.x - r) + r*cos(theta);
    newCarPosition.y = carPosition.y + r*sin(theta);
    
    // rotate the car appropriately (pseudo-code)
    [car rotateByAngle:theta];
    

    Note: I’m not sure what the correct method is to rotate your car’s image, so I just used rotateByAngle: to get the point across. I hope it helps!

    update (after comments):

    I hadn’t thought about the center of the turning circle moving with the car. The original code doesn’t take into account the angle that the car is already rotated to. I would change it as follows:

    ...
    if (steerAngle < 0.0f)
        theta = theta * -1;
    
    // calculate the center of the turning circle,
    // taking int account the rotation of the car
    circleCenter.x = carPosition.x - r*cos(carAngle);
    circleCenter.y = carPosition.y + r*sin(carAngle);
    
    // draw the turning circle
    drawCircle(circleCenter, r, CC_DEGREES_TO_RADIANS(180), 50, NO);
    
    // calculate the new position of the car
    newCarPosition.x = circleCenter.x + r*cos(theta);
    newCarPosition.y = circleCenter.y + r*sin(theta);
    
    // rotate the car appropriately (pseudo-code)
    [car rotateByAngle:theta];
    carAngle = carAngle + theta;
    

    This should keep the center of the turning circle at the appropriate point, even if the car has been rotated.

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

Sidebar

Related Questions

In cocos2d-iphone, I would like to use a sprite for the menu button item
I'm using the CCNode class in cocos2d and would like to use its schedule:interval:
I would like to use python to make system calls to programs and time
I would like to use integrated authentication to access a SQL database from a
I would like to use this Core Animation code in Cocos2D, or I just
I would like use from Spring.NET Aspect library Logging aspect together with log4Net. I
I would like use unmanaged code from C in C#. I built a DLL
I would like to use the group_by method, but instead of using a column
I would like to use complex numbers as defined in C99, but I need
I would like to use HTML 4.01 Strict, and used a DOCTYPE of it

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.