Click here to Skip to main content
14,639,480 members
Rate this:
Please Sign up or sign in to vote.
hi everyone
I want to write a program that simulate a car .
I have a problem with calculating position in the reverse gear

this is my code in forward gear:
theta is the angle of wheels of the car
-45<angle<45
primary position of car is in (800,500) in the form
if (gearState() == 1)//forward gear
{
position.X += (Int32)(speed() * Math.Sin(theta * Math.PI / 180));
position.Y -= (Int32)(speed() * Math.Cos(theta * Math.PI / 180));
}
this code work very good but doesn't work in reverse gear.
when theta=20 and car is in reverse gear car goes to the left instead of going right
do you have any idea ?
please help me
Posted
Updated 8-Aug-18 23:21pm
v3

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Assuming your car dimensionless (and speed() positive), shouldn't
if (gearState() == -1)//reverse gear
{
   position.X += (Int32)(speed() * Math.Sin(theta * Math.PI / 180));
   position.Y <big>+=</big> (Int32)(speed() * Math.Cos(theta * Math.PI / 180));
}

do the trick?
:)
   
Comments
fika_fa 2-Jul-10 13:21pm
   
your code doesn't work !
my car is a picture that moving on 2d screen
CPallini 2-Jul-10 14:22pm
   
What do you mean, exactly, with 'doesn't work'? What is the expected behaviour? What is the observed behaviour? Are the prerequisites satisfied (i.e. irrelevant car dimensions, positive speed)?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100