From 1fd48952989da35ce6bd0699e4329b0b353b98df Mon Sep 17 00:00:00 2001 From: josh Date: Fri, 14 Nov 2008 01:48:39 +0000 Subject: [PATCH] orb moving and bouncing off walls now git-svn-id: svn://anubis/dwscr/trunk@108 5bef9df8-b654-44bb-925b-0ff18baa8f8c --- ss/LightBounce.cc | 63 +++++++++++++++++++++++++++++++++++++++-------- ss/LightBounce.h | 1 + 2 files changed, 54 insertions(+), 10 deletions(-) diff --git a/ss/LightBounce.cc b/ss/LightBounce.cc index 29f4ecc..ca4466c 100644 --- a/ss/LightBounce.cc +++ b/ss/LightBounce.cc @@ -17,6 +17,7 @@ using namespace std; #define STRIDE_MULTIPLIER 1.0; #define ZOOM 12.0 #define ORB_RADIUS 1.5 +#define ORB_SPEED 6.0 LightBounceBox::LightBounceBox(LogoBox * lb, float x, float y, float z, @@ -76,42 +77,42 @@ LightBounce::LightBounce(SSMain * _SSMain) : SSMode(_SSMain) glMatrixMode(GL_MODELVIEW); float box_stride = m_logoBox.getWidth() * STRIDE_MULTIPLIER; - float half_size = SIZE * box_stride / 2.0; - float y = -half_size + box_stride / 2.0; + m_half_size = SIZE * box_stride / 2.0; + float y = -m_half_size + box_stride / 2.0; int rot = false; for (int i = 0; i < SIZE; i++) { - float x = -half_size + box_stride / 2.0; + float x = -m_half_size + box_stride / 2.0; for (int j = 0; j < SIZE; j++) { // front LightBounceBox * box = new LightBounceBox( - &m_logoBox, x, y, -half_size, 0, 180.0, rot ? 90.0 : 0 + &m_logoBox, x, y, -m_half_size, 0, 180.0, rot ? 90.0 : 0 ); m_boxes.push_back(box); // right box = new LightBounceBox( - &m_logoBox, half_size, y, x, 0, 90.0, rot ? 0 : 90.0 + &m_logoBox, m_half_size, y, x, 0, 90.0, rot ? 0 : 90.0 ); m_boxes.push_back(box); // back box = new LightBounceBox( - &m_logoBox, x, y, half_size, 0, 0, rot ? 90.0 : 0 + &m_logoBox, x, y, m_half_size, 0, 0, rot ? 90.0 : 0 ); m_boxes.push_back(box); // left box = new LightBounceBox( - &m_logoBox, -half_size, y, x, 0, 270.0, rot ? 0 : 90.0 + &m_logoBox, -m_half_size, y, x, 0, 270.0, rot ? 0 : 90.0 ); m_boxes.push_back(box); // top box = new LightBounceBox( - &m_logoBox, x, half_size, y, 270, 0, rot ? 0 : 90.0 + &m_logoBox, x, m_half_size, y, 270, 0, rot ? 0 : 90.0 ); m_boxes.push_back(box); // bottom box = new LightBounceBox( - &m_logoBox, x, -half_size, y, 90, 0, rot ? 0 : 90.0 + &m_logoBox, x, -m_half_size, y, 90, 0, rot ? 0 : 90.0 ); m_boxes.push_back(box); @@ -169,7 +170,10 @@ LightBounce::~LightBounce() /* called every time this screensaver mode should redraw the screen */ void LightBounce::update() { + static Uint32 last_elapsed = 0; SSMode::update(); + if (last_elapsed == 0) + last_elapsed = m_elapsed; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); int numMonitors = m_SSMain->getNumMonitors(); int width = m_SSMain->getWidth(); @@ -182,6 +186,42 @@ void LightBounce::update() float viewer_z = viewer_dist * sin(angle); float viewer_y = SIZE * ZOOM * 2.0 / 3.0; + /* update the orb position */ + float multiplier = ORB_SPEED * (m_elapsed - last_elapsed) / 1000.0; + m_orb_pos[0] += multiplier * m_orb_dir[0]; + m_orb_pos[1] += multiplier * m_orb_dir[1]; + m_orb_pos[2] += multiplier * m_orb_dir[2]; + if (m_orb_pos[0] < -m_half_size) + { + m_orb_dir[0] = -m_orb_dir[0]; + m_orb_pos[0] = -2 * m_half_size - m_orb_pos[0]; + } + else if (m_orb_pos[0] > m_half_size) + { + m_orb_dir[0] = -m_orb_dir[0]; + m_orb_pos[0] = 2 * m_half_size - m_orb_pos[0]; + } + if (m_orb_pos[1] < -m_half_size) + { + m_orb_dir[1] = -m_orb_dir[1]; + m_orb_pos[1] = -2 * m_half_size - m_orb_pos[1]; + } + else if (m_orb_pos[1] > m_half_size) + { + m_orb_dir[1] = -m_orb_dir[1]; + m_orb_pos[1] = 2 * m_half_size - m_orb_pos[1]; + } + if (m_orb_pos[2] < -m_half_size) + { + m_orb_dir[2] = -m_orb_dir[2]; + m_orb_pos[2] = -2 * m_half_size - m_orb_pos[2]; + } + else if (m_orb_pos[2] > m_half_size) + { + m_orb_dir[2] = -m_orb_dir[2]; + m_orb_pos[2] = 2 * m_half_size - m_orb_pos[2]; + } + for (int i = 0, sz = m_boxes.size(); i < sz; i++) { m_boxes[i]->updateDist(viewer_x, viewer_y, viewer_z); @@ -202,12 +242,15 @@ void LightBounce::update() { if (m_boxes[i]->getDist() < orbDist && !drewOrb) { + glPushMatrix(); + glTranslatef(m_orb_pos[0], m_orb_pos[1], m_orb_pos[2]); glCallList(m_orb_dl); + glPopMatrix(); drewOrb = true; } m_boxes[i]->draw(); } - glTranslatef(m_orb_pos[0], m_orb_pos[1], m_orb_pos[2]); } SDL_GL_SwapBuffers(); + last_elapsed = m_elapsed; } diff --git a/ss/LightBounce.h b/ss/LightBounce.h index 35a002c..7bf5d3e 100644 --- a/ss/LightBounce.h +++ b/ss/LightBounce.h @@ -54,6 +54,7 @@ protected: GLfloat m_orb_pos[3]; GLfloat m_orb_dir[3]; GLuint m_orb_dl; + float m_half_size; }; #endif