Creating an animated wire car using OpenGL. Wire Car is a project that I developed for the Computer Graphics lesson.
This code creates a wire car that the user can play with steering and wheel movements. Four Tires, Cube, Axles, Steering Wheel, and Steering Column are required to create the Wire Car. In this code, the Tires function creates four tires, the DrawCube function creates cube, the SteeringWheel function creates the steering wheel and steering column, the Axles function creates axles.
1- The initial situation when the project is started. The tires seem to go forward. Initially, the wire car cannot be moved.
2- Tires rotate to the left when the 'A' key is pressed and to the right when the 'D' key is pressed.
3- The tires stop turning when the 'M' key is pressed. Turning the car forward, backward, left or right is left to the user. It can be moved forward by pressing the 'W' key, backward by pressing the 'S' key, left by pressing the 'A' key, and right by pressing the 'D' key.
void DrawCube(double* center, double heightlenRatio, double minorlenRatio, double majorlenRatio, double Ratio)
Front-right point of Wire Car = ( Cx - cos(β) * d, Cy + sin(β) * d , Cz )
Back-right point of Wire Car = ( Cx + cos(β) * d, Cy + sin(β) * d , Cz )
Front-left point of Wire Car = ( Cx - cos(β) * d, Cy - sin(β) * d , Cz )
Back-left point of Wire Car = ( Cx + cos(β) * d, Cy - sin(β) * d , Cz )
It is enough to raise these four points by h to get the cube.
void Tires(double radius, double num_segments) void SteeringWheel(double radius , double num_segments)
Why do I calculate "d" to find the vertex points of the cube in figure 1 ? Why don't I just add or subtract x' and y' to the center?
The wire car is intended to provide forward, backward, left, right turn and movement with keyboard controls. This is basically achieved by forming all the determined points connected to the center. Therefore, the distance of the point to the center is calculated.
Let us visualize that the x obtained according to the definition of 'x= x * cos(α) – y * sin(α) ' in Figure 2 is added to the x and y coordinates of the center.
The front wheels looked like ellipses when I added the same value to the x and y coordinates of the center of the tires. I managed to fix this error on the rear tires, but when I tried the front wheels, this time I prevented the wheels from turning.
c_b = cosf(beta *PI / 180; s_b = sinf(beta *PI / 180);
I tried this for the front wheels =>
x = (c_b - s_b) * x + cx;
y = (c_b - s_b) * x + cy;
I tried this for the rear wheels =>
x = cos(CubeRotateAngle * PI / 180) * x + cx;
y = sin(CubeRotateAngle * PI / 180) * x + cy;
Thus, the position of the rear wheels will always be parallel to the cube. Since no CubeRotateAngel is used on the front wheels, a 45-degree angle is formed as shown in figure 3. For the front wheels to be parallel to the cube, the initial value of beta must be equal to 'CubeRotateAngel'. For example, let us say CubeRotateAngel is 30. Beta would then be -15 degrees. This allows the front wheels to move about 15 degrees to the left. Since an angle of 45 degrees with respect to the x-axis is formed initially, 30 degrees is obtained from '45 + (-15) 'and the front wheels are made parallel to the cube.
- https://learnopengl.com/Getting-started/Camera
- http://slabode.exofire.net/circle_draw.shtml
- https://stackoverflow.com/questions/4704986/switch-statement-using-or
- https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/gluLookAt.xml
- https://www.opengl.org/resources/libraries/glut/spec3/node51.html
- https://www.opengl.org/resources/libraries/glut/spec3/node50.html
- https://www.opengl.org/resources/libraries/glut/spec3/node49.html
- https://www.opengl.org/resources/libraries/glut/spec3/node46.html