SFML и C++ Уроки \ Разработка игр › Форумы › SFML Graphics › Проблема со следованием камеры за персоонажем. › Ответ в теме: Проблема со следованием камеры за персоонажем.
Перенесите
View view;
в main и всё заработает.
Из глобальной области не хочет работать.
Вот рабочий вариант
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
#include <SFML/Graphics.hpp> #include "map.h" #include <iostream> #include <fstream> #include <math.h> #define PI 3.14159265 using namespace sf; sf::Time t1 = sf::microseconds(10000); float resistance = 0.1 ; class Player { public: float w, h, x, y, currentSpeed, maxSpeed, weight, power, braking, controllability, clutch, acceleraation; float ang; String file; Image image; Texture texture; Sprite sprite; Player(String File, int X, int Y, int W, int H, float Weight, float Power, float Braking, float Controllability, float Clutch, float Acceleraation, float Ang); ~Player(); int getPlayerCoortdsX() { return x; } int getPlayerCoortdsY() { return y; } void CarMove(float time) { sprite.move(cos(PI / 180 * ang)*currentSpeed*time, sin(PI / 180 * ang)*currentSpeed*time); if (Keyboard::isKeyPressed(Keyboard::Left)) { sprite.setRotation(ang); } if (Keyboard::isKeyPressed(Keyboard::Right)) { sprite.setRotation(ang); } } void CarCalculations() { maxSpeed = power / weight * 2; if (Keyboard::isKeyPressed(Keyboard::Up)) { acceleraation = power * controllability * clutch / weight / 100; currentSpeed = currentSpeed + acceleraation; } else { if (currentSpeed > 0) currentSpeed = currentSpeed - resistance * controllability / weight * 10; else currentSpeed = 0; } if (Keyboard::isKeyPressed(Keyboard::Down)) { if (currentSpeed > 0) { acceleraation = 0; currentSpeed = currentSpeed - braking * controllability * resistance / weight; } else currentSpeed = -0.05; } if (currentSpeed > 0.001 || currentSpeed < -0.001) { if (Keyboard::isKeyPressed(Keyboard::Left)) { ang = ang - 0.2; } if (Keyboard::isKeyPressed(Keyboard::Right)) { ang = ang + 0.2; } } if (currentSpeed > maxSpeed) currentSpeed = maxSpeed; } }; Player::Player(String File, int X, int Y, int W, int H, float Weight, float Power, float Braking, float Controllability, float Clutch, float Acceleraation, float Ang) { currentSpeed = 0; file = File; x = X; y = Y; w = W; h = H; weight = Weight; power = Power; braking = Braking; controllability = Controllability; clutch = Clutch; acceleraation = Acceleraation; ang = Ang; image.loadFromFile("images/" + file); texture.loadFromImage(image); sprite.setTexture(texture); sprite.setTextureRect(IntRect(0, 0, w, h)); } Player::~Player() { } void main() { View view; LoadTrack(); Clock clock; RenderWindow window(sf::VideoMode(800, 600), "2D Racing"); view.reset(sf::FloatRect(0, 0, 640, 480)); Player p("car.png", 250, 200, 206, 142, 650, 150, 1.5, 0.65, 0.5, 0, 0); p.sprite.setPosition(p.x, p.y); p.sprite.setOrigin(p.w / 3, p.h / 2); Image map_image; map_image.loadFromFile("images/map.png"); Texture map; map.loadFromImage(map_image); Sprite s_map; s_map.setTexture(map); while (window.isOpen()) { float time = clock.getElapsedTime().asMicroseconds(); clock.restart(); time = time / 800; sf::Event event; while (window.pollEvent(event)) { if (event.type == sf::Event::Closed) window.close(); } p.CarCalculations(); p.CarMove(time); view.setCenter(p.sprite.getPosition()); window.setView(view); window.clear(Color(100,20,250)); for (int i = 0; i < WIDTH_MAP; i++) for (int j = 0; j < HEIGHT_MAP; j++) { if (trackMaxrix[i][j] == 0) s_map.setTextureRect(IntRect(0, 0, 32, 32)); if (trackMaxrix[i][j] == 1) s_map.setTextureRect(IntRect(32, 0, 32, 32)); if ((trackMaxrix[i][j] == 2)) s_map.setTextureRect(IntRect(64, 0, 32, 32)); if ((trackMaxrix[i][j] == 3)) s_map.setTextureRect(IntRect(96, 0, 96, 96)); if ((trackMaxrix[i][j] == 4)) s_map.setTextureRect(IntRect(128, 0, 128, 128)); s_map.setPosition(j * 32, i * 32); window.draw(s_map); } window.draw(p.sprite); window.display(); } } |