Archived

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

Bouncing cube

This topic is 5378 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