Skip to content

Instantly share code, notes, and snippets.

@ajweeks
Last active January 31, 2016 15:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ajweeks/c72b7c8e856af3b19fb0 to your computer and use it in GitHub Desktop.
Save ajweeks/c72b7c8e856af3b19fb0 to your computer and use it in GitHub Desktop.
Seventh of Devember

Devember Day 07

Well I actually managed to get an hour of development done in class today. Maybe it helped that I have programming classes on Mondays. But usually the teacher spends a lot more time talking than allowing us to actually code.

Today's lesson was about classes in C++. They are starting at the absolute basics, which is understandable because some kids haven't ever seen programming before. However, I've had four semesters of computer science classes prior to this school so I guess I could say that I won't be worried about passing any programming tests any time soon.

The example program we made in class was a virtual ball pit type thing. We created a Circle class to store position, velocity, colour, and radius. Then just updated and rendered a vector of them. I spent a while trying to implement collision detection, which I got to work somewhat well. I'm interested in learning a more advanced way of doing it than simply checking if two circles intersect every frame and changing their velocities and moving them slightly if they do. I imagine one better way would be to cast some kind of ray somehow, although that seems like it could be quite a difficult thing to implement.

50 circles:

And 2000 circles:

I tried enabling collision detection for the 2000 circle example, I got one frame rendered, then nothing for the next ~15s. The algorithm definitely needs some improvement. I don't think it helps that the game engine I'm using (my school's) is more of a didactic engine than a professional one.

A few more interesting color variations (2500 circles each):

After increasing the number of circles past ~3000 the lag becomes apparent.

Although the saying "a picture is worth a thousand words" is true, a gif is truly worth a thousand pictures.

Unfortunately I don't know of a good way to embed gifs in this format, but if you want to see (a very compressed version of) what I'm seeing, click here, it looks quite different from just the still pictures. Too bad the movement doesn't really show in that gif. Ah well.

I'll attach a code snippet along with this log you can skim over if you're interested in how I'm drawing these. It's basically pseudo-code, because the game engine does so much of the internal work for you. If you're reading this on draft.sx then you won't be able to see the .cpp file, you can just click on the id at the top of the screen (#c72b7c8e856af3b19fb0) to be taken to the gist page. (Or just on that link for the extremely lazy).

That's it for today! See you tomorrow.

Previous Entry | All Entries | Next Entry

class Circle {
DOUBLE2 m_Pos; // Position
DOUBLE2 m_Vel; // Velocity
COLOR m_Color; // Color
double m_Radius; // Radius
Circle(DOUBLE2 pos, DOUBLE2 vel, COLOR color, double radius)
{
m_Pos = pos;
m_Vel = vel;
m_Color = color;
m_Radius = radius;
}
};
void GameStart()
{
int numberOfCircles = 3000;
for (int i = 0; i < numberOfCircles; ++i)
{
int radius = rand() % 15 + 7;
// Random position on the screen
DOUBLE2 pos = DOUBLE2(
rand() % GAME_ENGINE->GetWidth(),
rand() % GAME_ENGINE->GetHeight());
// We don't want any circle to have a velocity
// in the range [-5, 5] (it's too slow & boring)
// So give each circle a random velocity
// in the range [-15, 5] OR [5, 15]
DOUBLE2 vel = DOUBLE2(
(rand() % 15 + 5) * (rand() % 2 == 0 ? -1 : 1),
(rand() % 15 + 5) * (rand() % 2 == 0 ? -1 : 1));
COLOR color = COLOR(
rand() % 255,
rand() % 127 + 127,
rand() % 255);
// Each circle has a pseudo-random starting state
m_CirclesArr.push_back(Circle(pos, vel, color, radius));
}
}
void GameTick(double deltaTime)
{
for (int i = 0; i < m_CirclesArr.size(); ++i)
{
// Add each circle's velocity to its position
m_CirclesArr[i].m_Pos += m_CirclesArr[i].m_Vel;
}
}
void GamePaint()
{
// Fill the background with dark grey
GAME_ENGINE->DrawSolidBackground(COLOR(25, 25, 25));
for (Circle c : m_CirclesArr)
{
// We can either use the circle's set color (boooring!):
// GAME_ENGINE->SetColor(c.m_Color);
// Or we can generate a color based on the circle's position:
GAME_ENGINE->SetColor(COLOR((-c.m_Pos.x+c.m_Pos.y) / 2, (sqrt(c.m_Pos.x) / GAME_ENGINE->GetHeight()) * 255 * 255, (sin(c.m_Pos.y / GAME_ENGINE->GetHeight())) * 255));
GAME_ENGINE->FillEllipse(c.m_Pos, c.m_Radius, c.m_Radius);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment