Dessin des vecteurs accélération et vitesse pour les tests
This commit is contained in:
		
							parent
							
								
									f740cd6441
								
							
						
					
					
						commit
						50210b669e
					
				| 
						 | 
				
			
			@ -8,19 +8,28 @@
 | 
			
		|||
 | 
			
		||||
class PhysicsObject : public Object {
 | 
			
		||||
protected:
 | 
			
		||||
    sf::Vector2f velocity;
 | 
			
		||||
    float mass;
 | 
			
		||||
    sf::Vector2f acceleration;
 | 
			
		||||
    sf::VertexArray accelLine;
 | 
			
		||||
    sf::Vector2f velocity;
 | 
			
		||||
    sf::VertexArray velLine;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Calcule les forces appliquées à l'objet
 | 
			
		||||
     */
 | 
			
		||||
    virtual sf::Vector2f getForces(State state);
 | 
			
		||||
 | 
			
		||||
    static constexpr float GRAVITY = 275;
 | 
			
		||||
    static constexpr float GRAVITY = 20;
 | 
			
		||||
 | 
			
		||||
public:
 | 
			
		||||
    PhysicsObject(float x, float y) :
 | 
			
		||||
        Object(x, y), mass(1) {}
 | 
			
		||||
        Object(x, y), mass(1), accelLine(sf::LinesStrip, 2),
 | 
			
		||||
        velLine(sf::LinesStrip, 2) {}
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Dessine l'objet dans la fenêtre donnée
 | 
			
		||||
     */
 | 
			
		||||
    virtual void draw(sf::RenderWindow& window);
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Met à jour la physique de l'objet juste avant le dessin d'une frame
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,6 +1,8 @@
 | 
			
		|||
#include "ball.hpp"
 | 
			
		||||
 | 
			
		||||
void Ball::draw(sf::RenderWindow& window) {
 | 
			
		||||
    PhysicsObject::draw(window);
 | 
			
		||||
 | 
			
		||||
	// chargement de la texture de test
 | 
			
		||||
	if (!texture.loadFromFile("./res/ball.png")) {
 | 
			
		||||
    	// erreur
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,9 +1,27 @@
 | 
			
		|||
#include "physics_object.hpp"
 | 
			
		||||
 | 
			
		||||
void PhysicsObject::draw(sf::RenderWindow& window) {
 | 
			
		||||
    velLine[0].position = position;
 | 
			
		||||
    velLine[0].color = sf::Color::Green;
 | 
			
		||||
    velLine[1].position = position + velocity * 1.f;
 | 
			
		||||
    velLine[1].color = sf::Color::Green;
 | 
			
		||||
 | 
			
		||||
    accelLine[0].position = position;
 | 
			
		||||
    accelLine[0].color = sf::Color::Red;
 | 
			
		||||
    accelLine[1].position = position + acceleration * 1.f;
 | 
			
		||||
    accelLine[1].color = sf::Color::Red;
 | 
			
		||||
 | 
			
		||||
    window.draw(velLine);
 | 
			
		||||
    window.draw(accelLine);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void PhysicsObject::update(State state) {
 | 
			
		||||
    sf::Vector2f acceleration = getForces(state) / mass;
 | 
			
		||||
    velocity += acceleration * state.delta;
 | 
			
		||||
    // intégration de la vitesse dans la position
 | 
			
		||||
    position += velocity * state.delta;
 | 
			
		||||
 | 
			
		||||
    // intégration des forces appliquées sur l'objet dans la vitesse
 | 
			
		||||
    acceleration = getForces(state) / mass;
 | 
			
		||||
    velocity += acceleration * state.delta;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
sf::Vector2f PhysicsObject::getVelocity() {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue