Animation is the illusion of making us think that an object is really moving on the screen. But underneath they are just complex algorithms updating and drawing different objects.
Aim: A complex animation of a walking dinosaur in 2D.
Method: Using Transformations of individual body parts.
See this Video:
There are 3 main transformations in Computer Graphics – Translation, Rotation and Scaling. All can be implemented using very simple mathematics.
Translation: X = x + tx, tx is the amount of translation in x-axis Y = y + ty, ty is the amount of translation in y-axis Rotation: X = xcosA - ysinA, A is the angle of rotation. Y = xsinA + ycosA Scaling: X = x*Sx, Sx is Scaling Factor Y = y*Sy, Sy is Scaling Factor
For drawing figures, Bresenham’s Line drawing algorithm will be used together with the above equations to draw each line according to need.
The body of the dinosaur is split up into 8 main portions –
Head, upperBody, Tail, downBody, and the four legs
The parts are stores as text files with comma seperated coordinates, which are imported during running of the program:
The centres of rotation of each object were stored in a separate file:
Since, all files were created by hand, there was room for little errors which were corrected in the following file:
Note: Please download all the above files before running the program and keep in the same directory.
Each one will have its own object storing the following:
- All the lines of the object in a big array
- The number of lines
- The current amount of translation
- The centre of rotation
- The offsets
- The current amount of rotation
- The direction of rotation
The program will work in the following order:
- THe OpenGL window will start
- All the files will be read and stored in there respective objects
- An infinite while loop will be started
- The screen will be cleared
- A line will be drawn depicting the grassland
- All the parts will be updated
- All the the parts will be drawn
- The body translation value will be decremented, and if the dinosaur is out of the window, it will be reset.
- The loop will go to its next iteration
During updation of an object, the rotationstate is checked and
if it is overshooting the threshold of rotation, then the direction of rotation is reversed.
The rotationstate is 0 if it is not to be rotated.
This is a sample screenshot:
Don’t forget to download the files before running the program.
This article is contributed by Suprotik Dey. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Basic Transformations in OPENGL
- Getting started with OpenGL
- Rendering a Triangle using OpenGL(using Shaders)
- OpenGL Rendering Pipeline | An Overview
- Scan-line Polygon filling using OPENGL in C
- OpenGL program for Simple Ball Game
- Creating a PortScanner in C
- Creating Repository in GitHub
- Creating a Proxy Webserver in Python | Set 1
- Creating a Proxy Webserver in Python | Set 2
- Creating a Calculator for Android devices
- Creating and Configuring Wi-Fi Hotspot in Ubuntu
- Creating custom domain name instead of localhost in Ubuntu
- Creating Python Virtual Environment in Windows and Linux
- Creating Sheets in Excel File in Java using Apache POI
Improved By : Akanksha_Rai