Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: moving a sprite relative to another.  (Read 4778 times)

0 Members and 1 Guest are viewing this topic.

Sirt

  • Newbie
  • *
  • Posts: 27
    • View Profile
moving a sprite relative to another.
« 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???

Sirt

  • Newbie
  • *
  • Posts: 27
    • View Profile
moving a sprite relative to another.
« Reply #1 on: March 28, 2009, 04:50:35 am »
A bit of code if it helps to see what I am trying to achieve ...

Code: [Select]

        // 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 ] );

Imbue

  • Full Member
  • ***
  • Posts: 104
    • View Profile
moving a sprite relative to another.
« Reply #2 on: March 28, 2009, 09:59:49 am »
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.

Sirt

  • Newbie
  • *
  • Posts: 27
    • View Profile
moving a sprite relative to another.
« Reply #3 on: March 28, 2009, 11:49:51 am »
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.

Sirt

  • Newbie
  • *
  • Posts: 27
    • View Profile
moving a sprite relative to another.
« Reply #4 on: March 28, 2009, 01:46:21 pm »
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.

Imbue

  • Full Member
  • ***
  • Posts: 104
    • View Profile
moving a sprite relative to another.
« Reply #5 on: March 28, 2009, 06:43:03 pm »
When you override the Render method, you draw only the lights in that function.

So your code may look like:

Code: [Select]
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.

 

anything