SFML community forums
Help => Graphics => Topic started by: Sirt on March 28, 2009, 02:56:55 am
-
I have a spaceship,it moves across the screen while slowly rotating and scaling down (into the distance).
Then I add a flashing light to the crafts wing and everyframe rotate/scale the light to the same degree as the craft to keep it on the wing,but the light keeps slipping off the wing?
Is this where TransformToLocal is useful??
Can I get some advice how to keep the light always on the wing of the moving craft please???
-
A bit of code if it helps to see what I am trying to achieve ...
// Give the craft motion.
craft1.Rotate( 0.01 );
craft1.Move( 0.1, -0.1 );
craft1.Scale( scale, scale );
// Keep the lights size relative to the craft.
light[ lightCount ].Scale( scale, scale );
scale = (scale > 0.0 ? scale -0.000001 : 0.0);
// Get crafts position and blit light to it's wing.
craftPos = craft1.GetPosition();
light[ lightCount ].SetPosition( craftPos.x+50.0, craftPos.y+50.0 );
App.Draw( craft1 );
App.Draw( light[ lightCount ] );
-
One way to do it, is to make a space ship class that inherits from either sf::Drawable or sf::Sprite. Then override the Render method and do your drawing there. When the object is moved, scaled, rotated, etc, it will apply to everything drawn inside of that Render method too.
Otherwise, there is no easy way, other than just calculating it yourself. This would be a good reason for SFML to make the matrix class public.
Hmmm, since your case is so simple, the TransformToGlobal function may work. You would give it the local coordinates that you want draw on your ship's sprite, and it will give you the global (actually just up one in the hierarchy, which may happen to be global) coordinates.
-
Cheers Imbue,much appreciated.
I will make a ship class derived from sf::Sprite and do what you suggested,if that works then it will be a simple solution.
Thanks mate.
-
No,making sf::Sprite a base class for my class 'Craft' and overriding the Render method does nothing for it.
The light still slips off the wing.
This craft was obviously made in china.
-
When you override the Render method, you draw only the lights in that function.
So your code may look like:
class Ship : public sf::Sprite
{
public:
Ship(const Image &Img,
const Vector2f &Position=Vector2f(0, 0),
const Vector2f &Scale=Vector2f(1, 1),
float Rotation=0.f,
const Color &Col=Color(255, 255, 255, 255))
:Ship::Sprite(Img, Position, Scale, Rotation, Col)
{
//Load your light here and set its position relative to the ship.
light = sf::Sprite(XXXXX);
}
virtual ~Ship()
{}
private:
sf::Sprite light; //Stores the light to draw.
virtual void Render(sf::RenderTarget& Window) const
{
Sprite::Render(Window); //Draws the ship.
Window.Draw(light); //Draws the light.
}
};
In your main loop, you then move, scale, rotate, and draw one ship object. The light will be moved along and drawn with it automatically.