My 'body' class in SFML: How does it rotate member shapes correctly on its own?
Hey so i've made a 'body' class, which inherits from sf::drawable in SFML that holds shapes together so i can form more complicated shapes and figures. I found i had to do a few updates on each individual member shape at render time, based on what's happened to the body over-all since last render.
This included:
- Rotation
- Translations
- Scaling
I thought i would have to manually write the code to factor in these changes that could have occured to the overall body, so the shape positions turned out right. However when coding the Rotation part, i found after writing the manual code that even though i made Bodies Render() function only iterate through and render each shape without changing them, they somehow came out with the right orientation anyway. HOW CAN THIS BE?
I commented out the code i 开发者_如何学编程wrote and apparently didn't need, and i use the Draw() function to render. Please explain to me my own code! (God i feel retarded).
here's the class:
class Body : public sf::Drawable{
public:
Body(const sf::Vector2f& Position = sf::Vector2f(0, 0), const sf::Vector2f& Scale = sf::Vector2f(1, 1), float Rotation = 0.f, const sf::Color& Col = sf::Color(255, 255, 255, 255)){
SetPosition(Position);
SetScale(Scale);
SetRotation(Rotation);
SetColor(Col);
RotVal=0;};
////////////////// Drawable Functions ////////////////////
void SetX(float X){
MoveVal.x += X - GetPosition().x;
Drawable::SetX(X);};
void SetY(float Y){
MoveVal.y += Y - GetPosition().y;
Drawable::SetY(Y);};
void SetRotation(float Rotation){
RotVal+= Rotation-GetRotation();
Drawable::SetRotation(Rotation);};
////////////////////////////////////////////////////////////
bool AddShape(sf::Shape& S){
Shapes.push_back(S); return true;};
bool AddSprite(sf::Sprite& S){
Sprites.push_back(S); return true;};
void Draw(sf::RenderTarget& target){
for(unsigned short I=0; I<Shapes.size(); I++){
//Body offset
Shapes[I].SetPosition(
Shapes[I].GetPosition().x + MoveVal.x,
Shapes[I].GetPosition().y + MoveVal.y);
// Aparrently Body shapes rotate on their own...
//WWTFFFF>>>>??????????
//Body Rotation
//float px= GetPosition().x,
// py= GetPosition().y,
// x= Shapes[I].GetPosition().x,
// y= Shapes[I].GetPosition().y,
// rot= ConvToRad(RotVal);
/*Shapes[I].SetPosition(
px + ((x-px)*cos(rot)) - ((y-py)*sin(rot)),
py - ((x-px)*sin(rot)) + ((y-py)*cos(rot)));*/ //TODO: put this in a math header
//Shapes[I].Rotate(RotVal);
}
target.Draw(*this); // draws each individual shape
//Reset all the Change Values
RotVal=0;
MoveVal.x=0;
MoveVal.y=0;
};
private:
sf::Vector2f MoveVal;
float RotVal;
std::vector<sf::Shape> Shapes;
std::vector<sf::Sprite> Sprites;
virtual void Render(sf::RenderTarget& target) const{
for(unsigned short I=0; I<Shapes.size(); I++){
target.Draw(Shapes[I]);}
for(unsigned short I=0; I<Sprites.size(); I++){
target.Draw(Sprites[I]);}
};
};
My guess was confirmed by the SFML source code.
When you call target.Draw(*this)
it eventually calls Drawable::Draw(RenderTarget& Target) const
, which sets up a render matrix which has the rotation that you gave it with the Drawable::SetRotation
call. Your virtual Render
function is then called, with that rotation environment set up. This means that your body-parts get rotated.
Have a look at the source yourself to get a better understanding. (;
精彩评论