|
Adjust camera depending on mouse location |
Bingocat
Member #15,424
December 2013
|
I'm making a multiplayer shooter game and added a sniper to the game. I want the camera to move away from the player depending on how far the mouse is from the player whenever they get the sniper. For some reason I can't get it right. Whenever I did get it sorta working, it would only work whenever the player was in a certain place on the map. I want it to be sorta like the one in terraria in this video https://www.youtube.com/watch?v=GqmLiupdhzw&feature=player_detailpage#t=38 . I am using ALLEGRO_TRANSFORM for the camera. Nevermind... I got it with this code: 1void UpdateCamera(float *cameraPos, Player player) {
2 if (player.weapon != SNIPER) {
3 cameraPos[0] = -(screenWidth / 2) + (int)player.x;
4 cameraPos[1] = -(screenHeight / 2) + (int)player.y;
5 }
6 else if (player.weapon == SNIPER) {
7 float cameraPos2[2];
8 cameraPos2[0] = -(screenWidth / 2) + (int)player.x;
9 cameraPos2[1] = -(screenHeight / 2) + (int)player.y;
10
11 ALLEGRO_MOUSE_STATE mouse;
12 al_get_mouse_state(&mouse);
13
14 cameraPos[0] = cameraPos2[0] + ((screenWidth / 2 + mouse.x) / 2) - screenWidth / 2;
15 cameraPos[1] = cameraPos2[1] + ((screenHeight / 2 + mouse.y) / 2) - screenWidth / 2;
16 }
17
18 if (cameraPos[0] < 0)
19 cameraPos[0] = 0;
20 if (cameraPos[1] < 0)
21 cameraPos[1] = 0;
22
23 if (cameraPos[0] > levelWidth - screenWidth)
24 cameraPos[0] = levelWidth - screenWidth;
25 if (cameraPos[1] > levelHeight - screenHeight)
26 cameraPos[1] = levelHeight - screenHeight;
27}
|
|