Bonsoir.
Je réalise un petit code afin d'obtenir un résultat équivalent à celui-ci:
https://www.youtube.com/watch?v=LrEvoKI07Ww&feature=youtu.be&t=7m36sJe me suis aidé d'une petite formule pour faire une rotation autour d'un point:
X = x - a
Y = y - b
xBis = a + X * cos(angle) - Y * sin(angle)
yBis = b + X * sin(angle) + Y * cos(angle)
Cela marche très bien sur GéoGebra, mais moins dans mon programme...
Dans celui-ci, j'utilise le centre de mon personnage comme point de rotation, et le pointeur de la souris comme point auquel appliquer la rotation.
Voyez le résultat par vous même:
#include <SFML/Graphics.hpp>#include <iostream>#include <cmath>#include <vector>using namespace std
;sf
::RenderWindow window
;int speed
=5;sf
::Vector2i positionSouris
;vector
<sf
::RectangleShape> lignes
;vector
<sf
::RectangleShape> decors
;sf
::Color orange
(255,151,27);sf
::CircleShape perso
(20);sf
::VertexArray lgneDeVue
(sf
::LinesStrip,2);sf
::VertexArray lgneDeVue2
(sf
::LinesStrip,2);sf
::VertexArray lgneDeVue3
(sf
::LinesStrip,2);int FOV
=45;sf
::RectangleShape rectCreate
(double,double,double,double,sf
::Color);void gestion_clavier
();vector
<sf
::RectangleShape> createTab
();bool collision
();void vueUpdate
();int main
(){ window.
create(sf
::VideoMode(800,600),"PCS program"); window.
setFramerateLimit(60); std
::cout<<"Lancement du jeu."<<std
::endl; lignes
=createTab
(); std
::cout<<"Grille de jeu creee."<<std
::endl; perso.
setPosition(120,120); perso.
setFillColor(sf
::Color::Yellow); while(window.
isOpen()) { sf
::Event event
; while(window.
pollEvent(event
)) { if(event.
type==sf
::Event::Closed) { window.
close(); } } vueUpdate
(); gestion_clavier
(); for(int i
=0;i
<lignes.
size();++i
) window.
draw(lignes
[i
]); for(int i
=0;i
<decors.
size();++i
) window.
draw(decors
[i
]); window.
draw(perso
); window.
draw(lgneDeVue
); window.
draw(lgneDeVue2
); window.
draw(lgneDeVue3
); window.
display(); window.
clear(sf
::Color(210,210,210)); } return 0;}void vueUpdate
(){ sf
::Vector2i posSouris
=sf
::Mouse::getPosition(window
); lgneDeVue
[0].
position=sf
::Vector2f(perso.
getPosition().
x+20,perso.
getPosition().
y+20); lgneDeVue
[0].
color=sf
::Color::Red; lgneDeVue
[1].
position=sf
::Vector2f(posSouris.
x,posSouris.
y); lgneDeVue
[1].
color=sf
::Color::Red; int a
=perso.
getPosition().
x+20,b
=perso.
getPosition().
y+20; int X
=posSouris.
x-a
,Y
=posSouris.
y-b
; lgneDeVue2
[0].
position=sf
::Vector2f(perso.
getPosition().
x+20,perso.
getPosition().
y+20); lgneDeVue2
[0].
color=sf
::Color::Magenta; lgneDeVue2
[1].
position=sf
::Vector2f(a
+X
*cos(FOV
)-Y
*sin(FOV
),b
+X
*sin(FOV
)+Y
*cos(FOV
)); lgneDeVue2
[1].
color=sf
::Color::Magenta; lgneDeVue3
[0].
position=sf
::Vector2f(perso.
getPosition().
x+20,perso.
getPosition().
y+20); lgneDeVue3
[0].
color=sf
::Color::Magenta; lgneDeVue3
[1].
position=sf
::Vector2f(a
+X
*cos(-FOV
)-Y
*sin(-FOV
),b
+X
*sin(-FOV
)+Y
*cos(-FOV
)); lgneDeVue3
[1].
color=sf
::Color::Magenta;}sf
::RectangleShape rectCreate
(double posX
,double posY
, double width
, double height
, sf
::Color color
){ sf
::RectangleShape rect
(sf
::Vector2f(width
,height
)); rect.
setPosition(posX
,posY
); rect.
setFillColor(color
); return rect
;}vector
<sf
::RectangleShape> createTab
(){ vector
<sf
::RectangleShape> lignes
; for (int i
= 100; i
<700; i
+=40) { sf
::RectangleShape rect
; rect
=rectCreate
(i
,100,400,1,sf
::Color::Blue); rect.
rotate(90); lignes.
push_back(rect
); } for (int i
= 100; i
<520; i
+=40) { sf
::RectangleShape rect
; rect
=rectCreate
(100,i
,560,1,sf
::Color::Blue); lignes.
push_back(rect
); } sf
::RectangleShape rect1
; sf
::RectangleShape rect2
; sf
::RectangleShape rect3
; sf
::RectangleShape rect4
; sf
::RectangleShape rect5
; rect1
=rectCreate
(180,180,80,120,orange
); rect2
=rectCreate
(140,380,160,80,orange
); rect3
=rectCreate
(300,340,40,120,orange
); rect4
=rectCreate
(420,300,40,120,orange
); rect5
=rectCreate
(500,140,120,40,orange
); decors.
push_back(rect1
); decors.
push_back(rect2
); decors.
push_back(rect3
); decors.
push_back(rect4
); decors.
push_back(rect5
); return lignes
;}void gestion_clavier
(){ if(sf
::Keyboard::isKeyPressed(sf
::Keyboard::Up)) { if(perso.
getPosition().
y>100 ) { perso.
move(0,-speed
); if(collision
()) perso.
move(0,speed
); } } else if(sf
::Keyboard::isKeyPressed(sf
::Keyboard::Down)) { if(perso.
getPosition().
y<460 ) { perso.
move(0,speed
); if(collision
()) perso.
move(0,-speed
); } } else if(sf
::Keyboard::isKeyPressed(sf
::Keyboard::Left)) { if(perso.
getPosition().
x>100 ) { perso.
move(-speed
,0); if(collision
()) perso.
move(speed
,0); } } else if(sf
::Keyboard::isKeyPressed(sf
::Keyboard::Right)) { if(perso.
getPosition().
x<620 ) { perso.
move(speed
,0); if(collision
()) perso.
move(-speed
,0); } }}bool collision
(){ for(int i
=0;i
<decors.
size();++i
) { if(perso.
getGlobalBounds().
intersects(decors
[i
].
getGlobalBounds())) { return true; } } return false;} L'angle n'est jamais bon entre les lignes...
Merci d'avance pour toute aide!