Explaining Asteroids Movement code
Posted
by
Moaz ELdeen
on Game Development
See other posts from Game Development
or by Moaz ELdeen
Published on 2012-08-31T18:47:11Z
Indexed on
2012/08/31
21:50 UTC
Read the original article
Hit count: 244
I'm writing an Asteroids Atari clone, and I want to figure out how the AI for the asteroids is done.
I have came across that piece of code, but I can't get what it does 100%
if ((float)rand()/(float)RAND_MAX < 0.5) {
m_Pos.x = -app::getWindowWidth() / 2;
if ((float)rand()/(float)RAND_MAX < 0.5)
m_Pos.x = app::getWindowWidth() / 2;
m_Pos.y = (int) ((float)rand()/(float)RAND_MAX * app::getWindowWidth());
} else {
m_Pos.x = (int) ((float)rand()/(float)RAND_MAX * app::getWindowWidth());
m_Pos.y = -app::getWindowHeight() / 2;
if (rand() < 0.5)
m_Pos.y = app::getWindowHeight() / 2;
}
m_Vel.x = (float)rand()/(float)RAND_MAX * 2;
if ((float)rand()/(float)RAND_MAX < 0.5)
{
m_Vel.x = -m_Vel.x;
}
m_Vel.y =(float)rand()/(float)RAND_MAX * 2;
if ((float)rand()/(float)RAND_MAX < 0.5)
m_Vel.y = -m_Vel.y;
© Game Development or respective owner