Parabolic motion Ball Two-dimensional OpenGL C + +

Source: Internet
Author: User
Tags clear screen

Tag: UTC OLE mode bit POI coordinate window return 3.1

1. Oblique-throw motion of the rebound of the contact wall

The key lies in the use of oblique parabolic motion formula in programming animation

Because the animation itself is a frame by box, with the idea of calculus, the superposition process is the animation itself, we need to write in the function each time the amount of change on the line.

Each change is that the velocity varies with acceleration and time, and then the displacement changes as the speed and time change.

Each time we calculate the amount of variation we write our own size, set to T

For lateral motion, the horizontal velocity is constant, and each delta T is more vx*t than before, so write x = x +vx*t in the Move function, (the loop call of move is automatically implemented in OpenGL animation, for us, the motion is called the move function)

For vertical motion, the vertical direction of the speed, within each delta T, will change the a*t, so the vertical direction of the speed, each iteration is written in this way, VY = Vy + a*t;

The displacement in the vertical direction, each delta T within the change, vy*t, you write y = y +vy*t; This makes the Y coordinate two times the T and is controlled by the acceleration.

If the next decision is to make the ball not out of the view frame, we can always see its motion, as long as the ball in the reach of the boundary, the speed of the direction of the movement.

//DrawBall.cpp:Defines the entry point for the console application.//#include"stdafx.h"#include<gl/glut.h>#include<stdlib.h>#include<math.h>#include<stdio.h>#include<iostream>#include<time.h>using namespacestd;Doublev =0.3;//Initial Speed SizeDoubleVX =v;DoubleVY =2.5*v;DoubleA =-0.2;//Downward AccelerationStatic Doublex, y;//Center coordinatesDoublet =0.01;voidMoveball (void) {x= x + vx*T; VY= VY + *T; Y= y + vy*T; if(Y <=-1.5) Vy= -VY; if(x >=1.8|| X <=-1.8) VX= -VX;}//draw a circle oncevoidDrawfilledcircle (DoubleXDoubleYDoubleRadiusintsections) {    floatAlpha;    Glbegin (Gl_triangle_fan);    glvertex2f (x, y);  for(intCount =0; Count <= sections; count++) {Alpha= Count *2*3.1415926/sections;//Central Angleglvertex2f (x + radius*cos (alpha), Y + radius*sin (alpha)); } glend ();}voidInitvoid)//Initialize{x= -1.5; Y= -1.5; Glclearcolor (1.0,1.0,1.0,0.0);//Clear Screen ColorGlortho (-2.0,2.0, -2.0,2.0,2.0, -2.0);//Visual Body}voidDisplayvoid)//Redraw the screen{glclear (gl_color_buffer_bit);//clear the color cacheGLCOLOR3F (0.0,0.0,1.0);//Current ColorDrawfilledcircle (x, Y,0.2, -);//write a drawing function hereglutswapbuffers ();//Animated Display//Glflush ();//static display to show quickly}voidMyTime (intvalue)    {Moveball ();    Glutpostredisplay (); Gluttimerfunc (1, MyTime,1);}intMainintargcChar**argv) {Glutinit (&AMP;ARGC, argv);//Glut Library and console initialization functionsGlutinitdisplaymode (glut_double | GLUT_RGB);//display mode single dual cache color mode depth//Glutinitdisplaymode (Glut_single | GLUT_RGB);Glutinitwindowposition ( -, $);//The top left corner of the window's position window is from the upper left corner of the screenGlutinitwindowsize ( -, -); Glutcreatewindow ("ballmovement");//window nameInit ();//invoking a custom initialization functionGlutdisplayfunc (display);//registering the display callback functionGluttimerfunc (1, MyTime,1); Glutmainloop ();//Enter event Loop    return 0;}

2. Click on the mouse to control the movement of the ball every time the start of oblique motion

3. Use the mouse button to control the initial velocity of the ball movement, so as to adjust the horizontal distance of the ball oblique throwing motion.

4. The viewport tracks the movement of the small ball, so that to keep seeing the ball, we do not let the ball bounce, the ball in the world coordinate system has been moving, we let the viewport follow it movement.

The ultimate goal is to use OpenGL to achieve a simulated jump-hop game.

Parabolic motion Ball Two-dimensional OpenGL C + +

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.