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
|
#pragma once
#include "Box2D\Box2D.h"
#define DEG2RAD 0.017453f
#define RAD2DEG 57.29577f
class PhysicalBody {
protected:
b2Body * m_body = nullptr;
const float mk_fDensity = 2.0f;
const float mk_fFriction = 0.4f;
const float mk_fRestitution = 0.6f;
public:
void setUserData(void* data) { m_body->SetUserData(data); };
};
#pragma once
#include "Box2D\Box2D.h"
#include <SFML\Graphics.hpp>
#include "PhysicalBody.h"
class DynamicPlayer : public sf::CircleShape, PhysicalBody
{
private:
public:
sf::CircleShape player;
DynamicPlayer(b2World * world, const sf::Vector2f& position, const float radius, const float orientation);
void update();
void draw(sf::RenderTarget &Target, sf::RenderStates states) const;
};
#include "DynamicPlayer.h"
DynamicPlayer::DynamicPlayer(b2World * world, const sf::Vector2f& position, const float radius, const float orientation)
{
b2BodyDef l_bodyDef;
b2CircleShape l_shape;
b2FixtureDef l_fixtureDef;
l_bodyDef.type = b2_dynamicBody;
l_bodyDef.position.Set(position.x, position.y);
l_bodyDef.angle = orientation * DEG2RAD;
m_body = world->CreateBody(&l_bodyDef);
l_shape.m_radius = radius;
l_fixtureDef.density = mk_fDensity;
l_fixtureDef.friction = mk_fFriction;
l_fixtureDef.restitution = mk_fRestitution;
l_fixtureDef.shape = &l_shape;
m_body->CreateFixture(&l_fixtureDef);
setPosition(position);
setRadius(radius);
setOrigin(radius, radius);
setRotation(orientation);
setFillColor(sf::Color(255.f, 150.f, 25.f));
}
void DynamicPlayer::update()
{
b2Vec2 pos = m_body->GetPosition();
setPosition(pos.x, pos.y);
float angle = m_body->GetAngle()* RAD2DEG;
setRotation(angle);
}
void DynamicPlayer::draw(sf::RenderTarget & target, sf::RenderStates states) const
{
target.draw((sf::CircleShape)*this);
sf::RectangleShape line(sf::Vector2f(getRadius(), 0.01f));
line.setPosition(getPosition());
line.setOrigin(0.f, 0.005f);
line.rotate(getRotation());
target.draw(line);
}
|