Friday, December 13, 2013

Ball Physics Part 1

Hello and welcome to my blog.

First thing I want to say is that this is the first time I write in a blog, so poor design and confusing imagery could be noticed.

In these series of posts I will attempt to recreate some primitive ball physics using Visual C++.

The software I am using is:

Visual Studio 2012, provided by DreamSpark.
DirectX Sdk, obtained from Microsoft website.
Chili DirectX Framework Source code, consists of code used to create a window and functions used to draw pixels onto the screen.

First we shall begin by creating a structure for our balls. It's purpose is to contain the variables which will be used to determine each ball's characteristics such as current position on the screen, velocity and etc.


I will declare the structure within the "Game" class of the framework, located in the Game.h header file. In it are declared some variables which will define each ball.











To draw each ball we must create a function that takes each ball's coordinates and draws it on the screen accordingly. Since this is strictly a programming blog, only simple geometric shapes will be used. Luckily, the framework mentioned comes with  functions for drawing just that. We will use the "DrawCircle" function located in D3DGraphics.cpp file of the framework. It looks like this:







What we notice here are calls of the "PutPixel" function.

It is a function which takes the following values: Position X, Position Y, Red, Green and Blue. Given these values, it draws a pixel on the screen at the given position and colors it with the given values of Red, Green and blue.

Next thing we have to do in order to draw the circle on the screen is to set the initial values of the position of our first ball. We declare an object called ball using our structure. For the first part we will use only one ball.




Next in the "Game" class constructor, located in Game.cpp we set values to our ball's position and size(radius). 





Given the fact the created window is 800x600, I set the initial position of our ball at the center of our screen, with radius of 50 pixels.


Next thing to do is to write a function that will give the ball's coordinates to the DrawCircle function and hopefully display it on the screen.






The D3DGraphics class is declared as gfx in the framework.

Next thing we do is call the function inside the "ComposeFrame" function, which iterates every frame and draws whatever is needs to be drawn on the screen.

Then we compile and look at the results:




So far it works as it should. Our next step is to add some movement to the ball.

First to the Ball class we add some variables which will help our ball move in a desired fashion.
After that we initialize them with desired values. 














Now we have to write some code. First I will create a function which will update the ball's position, given the current position values and the velocity of the ball. I will call it "UpdateBall" and it's purpose will be to update the position of the ball given the current velocity and resolve collision with the edges(borders) of the screen.






























I will also create a function which will receive user input and it will effect the ball's velocities accordingly.

After that we call these functions into the main game loop, where they will iterate every frame.



Then compile and run. The result:




What we observe is correct behavior of the ball, except the fact that it bounces on the bottom border of the window indefinitely. In the next part I will attempt to correct this unintended behavior. I will also update the code to support multiple balls onscreen. Apart from that I plan on programming collision detection and elastic collision resolving.

I will soon post links to the source code.

No comments:

Post a Comment