Original Post
Hello, I have what is probably a very simple conceptual question, but I'm having some trouble thinking it through. I'm learning OpenGL (using C++) and I'm working on a simple application where the user gets to control a train. (At this point, I don't have tracks for the train, so it's really more like a bus with several jointed segments). Suppose you have a train with 4 cars:
And suppose it's moving forward at some speed and the user wants to turn it to the left. I'm a little confused how to go about making the last three cars follow the first one correctly -- in other words, the train shouldn't "fall apart", and also, each car should always face the right direction. At the moment, I have a class for the whole train (Train), and a class for each car (Car). For each Car, I'm keeping track of the current position, the previous position in the last frame, the current direction, the previous direction, and I'm also keeping pointers to the car ahead and the car behind:
Anyways, here's what I'm wondering about: 1. How do I correctly update each car's direction per frame? Am I supposed to interpolate between the direction of the current car and the direction of the car ahead? Should I subtract the car ahead's position from its previous position to obtain a delta_pos vector and add that vector to the current car's direction vector to obtain the new direction? 2. How do I correctly update each car's position per frame? Obviously, this should (eventually) depend on the speed of the train. Should I just use pos and prev_pos from the car ahead? I apologize if this is a simple question -- it certainly doesn't seem that way to me, but then again, I am kind of a beginner and I've never really coded something like this before. (I'm also kind of sleep deprived.) I hope someone can help me understand the intuition behind this problem. Thanks in advance.
And suppose it's moving forward at some speed and the user wants to turn it to the left. I'm a little confused how to go about making the last three cars follow the first one correctly -- in other words, the train shouldn't "fall apart", and also, each car should always face the right direction. At the moment, I have a class for the whole train (Train), and a class for each car (Car). For each Car, I'm keeping track of the current position, the previous position in the last frame, the current direction, the previous direction, and I'm also keeping pointers to the car ahead and the car behind:
float pos_x, pos_y; // Current position
float prev_pos_x, prev_pos_y; // Position during last frame
float dir_x, dir_y; // Current direction
float prev_dir_x, prev_dir_y; // Direction during last frame
Car *ahead, *behind; // Pointers to the car ahead and the car behind
For the Train, I'm keeping track of the first car, the speed, and I also have booleans for whether the train is currently accelerating, decelerating, turning left, and turning right:
Car *first; // The leading car in the train
float speed; // Speed of the whole train
bool is_accelerating, is_decelerating; // Whether the user is pressing up or down
bool is_turning_left, is_turning_right; // Whether the user is pressing left or right
I'm probably going to have to have something for how fast the train is allowed to turn depending on its speed. Not yet sure what else. Suppose we also have the following constants:
const float length = 3.2f;
const float joint_offset = 1.5f;
And just for clarity, the "joint" is what I call the part where two adjacent cars are attached. The cars should pivot about these points.
Anyways, here's what I'm wondering about: 1. How do I correctly update each car's direction per frame? Am I supposed to interpolate between the direction of the current car and the direction of the car ahead? Should I subtract the car ahead's position from its previous position to obtain a delta_pos vector and add that vector to the current car's direction vector to obtain the new direction? 2. How do I correctly update each car's position per frame? Obviously, this should (eventually) depend on the speed of the train. Should I just use pos and prev_pos from the car ahead? I apologize if this is a simple question -- it certainly doesn't seem that way to me, but then again, I am kind of a beginner and I've never really coded something like this before. (I'm also kind of sleep deprived.) I hope someone can help me understand the intuition behind this problem. Thanks in advance.