Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

d_a_p_a_n03

Bouncing cube

This topic is 5622 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am writinga program in which a cube will bounce around the screen. I say bouncing, b/c once the cube hits one of the invisible walls (the edge of the screen) it will bounce back in the other direction. However, I can''t get the cube to bounce back. I already created by cube: CVertex vtxTri[] = { //Top Face {-xPos, yPos, -zPos, 0, 1, 0, D3DCOLOR_XRGB(255, 255, 255), 1, 1,}, {-xPos, yPos, zPos, 0, 1, 0, D3DCOLOR_XRGB(255, 255, 255), 1, 0,}, {xPos, yPos, -zPos, 0, 1, 0, D3DCOLOR_XRGB(255, 255, 255), 0, 1,}, {xPos, yPos, zPos, 0, 1, 0, D3DCOLOR_XRGB(255, 255, 255), 0, 0,}, //Front Face {-xPos, -yPos, -zPos, 0, 0, -1, D3DCOLOR_XRGB(255, 255, 255), 1, 1,}, {-xPos, yPos, -zPos, 0, 0, -1, D3DCOLOR_XRGB(255, 255, 255), 1, 0, }, {xPos, -yPos, -zPos, 0, 0, -1, D3DCOLOR_XRGB(255, 255, 255), 0, 1, }, {xPos, yPos, -zPos, 0, 0, -1, D3DCOLOR_XRGB(255, 255, 255), 0, 0, }, //Right Face {xPos, -yPos, zPos, 1, 0, 0, D3DCOLOR_XRGB(255, 255, 255), 1, 1,}, {xPos, yPos, zPos, 1, 0, 0, D3DCOLOR_XRGB(255, 255, 255), 1, 0, }, //Back Face {-xPos, -yPos, zPos, 0, 0, 1, D3DCOLOR_XRGB(255, 255, 255), 0, 1,}, {-xPos, yPos, zPos, 0, 0, 1, D3DCOLOR_XRGB(255, 255, 255), 0, 0, }, //Left Face {-xPos, -yPos, -zPos, -1, 0, 0, D3DCOLOR_XRGB(255, 255, 255), 1, 1,}, {-xPos, yPos, -zPos, -1, 0, 0, D3DCOLOR_XRGB(255, 255, 255), 1, 0, }, //Bottom Face {xPos, -yPos, -zPos, 0, -1, 0, D3DCOLOR_XRGB(255, 255, 255), 1, 1, }, {xPos, -yPos, zPos, 0, -1, 0, D3DCOLOR_XRGB(255, 255, 255), 1, 0, }, {-xPos, -yPos, -zPos, 0, -1, 0, D3DCOLOR_XRGB(255, 255, 255), 0, 1, }, {-xPos, -yPos, zPos, 0, -1, 0, D3DCOLOR_XRGB(255, 255, 255), 0, 0, }, }; xPos, yPos, and zPos are all 2.0, and then when I update the scene(where the cube is translated), I changed the position of the cube: void UpdateScene() { D3DXMATRIX rot_x_matrix; D3DXMATRIX rot_y_matrix; D3DXMATRIX rot_matrix; D3DXMATRIX trans_matrix; D3DXMATRIX matWorld; double rot_x = 0.0, rot_y = 0.0; rot_x += x_speed; rot_y += y_speed; if ((xPos + z_pos) > 5)//??? Must change here, i think z_pos = -z_pos;//??? Must change here, i think D3DXMatrixRotationY(&rot_y_matrix, (float)rot_y); D3DXMatrixRotationX(&rot_x_matrix, (float)rot_x); D3DXMatrixMultiply(&rot_matrix, &rot_x_matrix, &rot_y_matrix); D3DXMatrixTranslation(&trans_matrix, (float)z_pos, 0.0, 0.0); D3DXMatrixMultiply(&matWorld, &rot_matrix, &trans_matrix); g_pDevice->SetTransform(D3DTS_WORLD, &matWorld); } I use keyboard input to change the x_speed and y_speed value, but like I said, the cube doesn''t bounce back, it just ''jumps'' to the negative side of the screen (-x value). I know it has to do with this part --> if ((xPos + z_pos) > 5) z_pos = -z_pos; But I don''t know how to fix it. If anyone can help me, I''d be very grateful!! Thanx a bunch!

Share this post


Link to post
Share on other sites
Advertisement

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!