Click here to Skip to main content
15,888,968 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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

Assuming your car dimensionless (and speed() positive), shouldn't
C++
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?
:)
 
Share this answer
 
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, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900