Getting to know if the car is moving in a curve

Started by
13 comments, last by AhmedSaleh 6 years, 1 month ago

How would I know in Unity if the car is moving, driving into a curve ? I can get the angular velocity of the rigid body, but how would I detect that

Game Programming is the process of converting dead pictures to live ones .
Advertisement

Angular velocity will only tell you it's turning. You need to check velocity over time to see if it's a curve.

The only way to know if a car is moving in a curve is to take samples during the driving then to check if the sample has deviated exponentially.

What are you trying to do? I think there is a easier way to solve this problem.

@Scouting Ninja 

I'm trying to do some logic if the car is moving in a curve so I have to detect that.

Can you provide a pseudo code ?

 

Game Programming is the process of converting dead pictures to live ones .
3 hours ago, AhmedSaleh said:

I'm trying to do some logic if the car is moving in a curve so I have to detect that.

The problem is the car needs to be moving in a curve for +/- 1 second before you can see if it is indeed moving in a curve. Chances are that the logic you want to do has some other way of triggering.

Code is here to keep topic small:

Spoiler

 



public float TimerMax = 1f;
public float Bias = 0.8f;//the smaller the number the steeper the curve

float Timer;

index = 0;
Vector2[] SampleHolder;


void Start(){
  SampleHolder = new Vector2[3] { Vector2.zero, Vector2.zero, Vector2.zero};//Holds 3 vectors zero to prevent null error
  Timer = TimerMax; //Set time 
}

bool IsMathNoneLinear(Vector2 PointA,Vector2 PointB,Vector2 PointC){
  
  Vector2 DifA = Vector2 PointA - PointB;
  Vector2 DifB = Vector2 PointB - PointC;
  
  float DifBetweenDif = Vector2.Dot(DifA,DifB);//1 means same direction
  
  //The bias ignores curves with a small difference
  //Use 1 for any none linear math to triger true
  if (DifBetweenDif < Bias){
    return true;
  }
  
  return false;
}

void Update(){
  
  if (Timer > 0){
    Timer -= Time.DeltaTime;
  }else{
    //First find the slot in the array to sample
    if (index < SampleHolder.length){
      index +=1
    }else
    {
      index = 0;
    }
    //-1 because array start at 0
    SampleHolder[Index-1] = this.transform.position;
    Timer = TimerMax;
  }
  
  if (IsMathNoneLinear(SampleHolder[0],SampleHolder[1],SampleHolder[2]) == true){
    //Do something now that you know it's a curve
  }
}

 

 

 

 

I didn't bother to check it or to even spell check it. As you can see the code for this isn't the best code to use in a game. You could use angular velocity and just check that it's not so large that it's a turn or so small it doesn't matter.

The problem is that knowing if something is a curve takes 2 dimensions in this case position and time. There must be a better solution so can you please elaborate on what you want to do?

Thanks a lot for the sample code.

 

My car is moving in a curve and there are solid lines on its right and on its left, so I need to know if its Moving in a curve AND passing left of the solid line, I have to deduct some score.

 

Game Programming is the process of converting dead pictures to live ones .

You can't set a flag "going into a curve" and reset it again on "leaving a curve" ?

Sorry I miss wrote it, the car is a real car game, so it can drive in straight lines, curves,..etc. 

I want to know if the car is really driving for a curve, then I need to deduct some score if he is passing a left of a solid line which is on the track.

Game Programming is the process of converting dead pictures to live ones .

given angular and linear velocities, you could construct a circle. Assuming velocities remain constant, the car would follow this circle. From that you could use the radius for curve estimination, and you could predict when and where the car would cross a line by intersecting the circle with the line. Of course all those predictions turn out wrong if the car decides to steer, but you could say 'for the moment it seems the car will cross the line at this position in 0.5 seconds.'

1 hour ago, AhmedSaleh said:

Sorry I miss wrote it, the car is a real car game, so it can drive in straight lines, curves,..etc. 

I want to know if the car is really driving for a curve, then I need to deduct some score if he is passing a left of a solid line which is on the track.

I don't see the problem, really. The thing has a steering wheel and you know the position of it, right?

It seems to me it's always in a curve unless the steer is exactly straight ahead. Obviously, "exactly" never happens, so likely you want to allow for some error.

Alternatively, compare the orientation of the front and back wheels (projected on a flat space parallel to the road), if their directions align you're driving straight (with some margin for error, or you never drive straight).

Would you elaborate with some pseudo code please to get the idea ?

Game Programming is the process of converting dead pictures to live ones .

This topic is closed to new replies.

Advertisement