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

Author Topic: Sprite within Object won't move with it  (Read 8688 times)

0 Members and 2 Guests are viewing this topic.

Falke88

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
Sprite within Object won't move with it
« on: February 21, 2015, 11:25:32 pm »
Heya guys!

Long time no posting - which actually seems to be good because I know that I can solve many problems on my own with SFML.NET :P


Still I'm back to some fundamental problem here.

I have a class called "Player" which contains a Sprite object.
While changing "Player"'s Position my Sprite wont move. Player.Position has been moved. Sprite.Position hasn't.
Actually I thought that the sprite within a class would move with it aswell?


I worked with the SFML.NET Dev Book Tutorials.
Here is the Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SFML.Graphics;
using SFML.Window;
using SFML.System;

namespace KompanieGame
{
    class Player : Entity
    {
        int health;
        float speed;
        string id;

        Sprite sprite;
        ResourceHolder<Texture, TextureID> textures;

        public Player(ResourceHolder<Texture, TextureID> textures)
        {
            this.health = 100;
            this.speed = 100;
            this.textures = textures;

            sprite = new Sprite(textures.Get(TextureID.MackMoving));
        }

      //[... Property Getter & Setter]

        public override void DrawCurrent(RenderTarget target, RenderStates states)
        {
            target.Draw(sprite);
        }

        public override void UpdateCurrent(Time dt)
        {
            Position += base.velocity;
        }

        public override uint GetCategory()
        {
            return (uint)Category.Player;
        }
    }
}
 

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Sprite within Object won't move with it
« Reply #1 on: February 21, 2015, 11:42:38 pm »
And what would make the sprite magically follow its owner entity? The usual solution to this problem is to apply the Player's transform to the RenderStates in the DrawCurrent function. See the C++ tutorials for more details.
Laurent Gomila - SFML developer

Falke88

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
Re: Sprite within Object won't move with it
« Reply #2 on: February 22, 2015, 07:53:05 am »
And what would make the sprite magically follow its owner entity? The usual solution to this problem is to apply the Player's transform to the RenderStates in the DrawCurrent function. See the C++ tutorials for more details.

Ha don't get me wrong - I surely know that it wouldn't magically follow. But I wrote the whole stuff side by side with the book. Many things I understood - but I surely missed out the part where I should have realized how the sprite is being moved within the Players boundaries dynamically.


Will check out the transform stuff and hopefully don't have to come back here :)

Falke88

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
Re: Sprite within Object won't move with it
« Reply #3 on: February 22, 2015, 09:42:43 am »
Well I tried things and they didn't work out.

I reviewed my old SFML Game Development Book Tutorial Project.

There I found this piece within the "AircraftMover.cs"

 public override void UpdateCurrent(Time dt)
        {
            this.Transform.Translate(velocity * dt.AsSeconds());
        }


I'm using this but the Translate() method isn't working properly.

I implemented it as followed:

 public override void UpdateCurrent(Time dt)
        {
            this.Transform.Translate(50.0f, 0.0f);
        }

Debug log:

Player.Position = new Vector2f(20.0f, 330.0f)

this.Transform = {[Transform] Matrix(1, 0, 20,0, 1, 330,0, 0, 1, )}
this.Transform = {[Transform] Matrix(1, 0, 20,0, 1, 330,0, 0, 1, )}
this.Transform = {[Transform] Matrix(1, 0, 20,0, 1, 330,0, 0, 1, )}
this.Transform = {[Transform] Matrix(1, 0, 20,0, 1, 330,0, 0, 1, )}
this.Transform = {[Transform] Matrix(1, 0, 20,0, 1, 330,0, 0, 1, )}


as you can see no change had happened there :/ why is that? If I could fix this I think I have a solution!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Sprite within Object won't move with it
« Reply #4 on: February 22, 2015, 11:10:03 am »
The problem here is that we have no idea where the SFML classes are, everything's hidden behind your own classes and variables. Can't you setup a simple minimal code that just reproduces the problem? That would be much easier for us to help you.
Laurent Gomila - SFML developer

Falke88

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
Re: Sprite within Object won't move with it
« Reply #5 on: February 22, 2015, 01:26:44 pm »
Alright I managed to reproduce the problem as tiny as possible :D

You can see that I use Transform.Translate - but it doesn't affect anything as I mentioned before.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SFML.Graphics;
using SFML.Window;
using SFML.System;


namespace KompanieGame
{
    class SceneNodeTest : Transformable, Drawable
    {
        public virtual void UpdateCurrent(float deltaTime) { }

        public void Draw(RenderTarget target, RenderStates states)
        {
            states.Transform *= this.Transform;
            DrawCurrent(target, states);
        }

        public virtual void DrawCurrent(RenderTarget target, RenderStates states) { }
    }

    class EntityTest : SceneNodeTest
    {
        float velocity = 1.0f;

        public override void UpdateCurrent(float deltaTime)
        {
            this.Transform.Translate(velocity, 0.0f);                          // is NOT working
            //this.Position += velocity * new Vector2f(deltaTime, 0.0f);    // is working
        }
    }

    class TestPlayer : EntityTest
    {
        public Sprite sprite;

        public TestPlayer()
        {
            sprite = new Sprite(new Texture("./Resources/Test/TestTxt.png"));
        }

        public override void DrawCurrent(RenderTarget target, RenderStates states)
        {
            target.Draw(sprite, states);
        }
    }

    class TestClass
    {
        RenderWindow window;
        TestPlayer player;
       
        public TestClass()
        {
            window = new RenderWindow(new VideoMode(640, 480), "TestClass");
            player = new TestPlayer();
            player.Position = new Vector2f(100.0f, 100.0f);
        }

        public void Run()
        {
            while(window.IsOpen)
            {
                window.DispatchEvents();

                player.UpdateCurrent(0.016f);

                window.Clear(Color.Blue);
                window.Draw(player);
                window.Display();
            }
        }
    }
}
 

StormWingDelta

  • Sr. Member
  • ****
  • Posts: 365
    • View Profile
Re: Sprite within Object won't move with it
« Reply #6 on: February 22, 2015, 03:40:32 pm »
Odd if I wanted to make a sprite within a sprite I'd just set the sprite that should be in the same place as the other to its position directly.  Or if a follow sprite make its vector aim toward it.


Basically it would be like this for sprite within sprite> Sprite A Pos = Sprite B Pos


As for what's wrong with transform I got no clue.  Maybe the original position of the sprite isn't changing.
I have many ideas but need the help of others to find way to make use of them.

Falke88

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
Re: Sprite within Object won't move with it
« Reply #7 on: February 22, 2015, 03:57:30 pm »
Odd if I wanted to make a sprite within a sprite I'd just set the sprite that should be in the same place as the other to its position directly.  Or if a follow sprite make its vector aim toward it.


Basically it would be like this for sprite within sprite> Sprite A Pos = Sprite B Pos


As for what's wrong with transform I got no clue.  Maybe the original position of the sprite isn't changing.


Hey there,

Well be sure that I already made that "odd" way. But in my already advanced code I came over some ugly glitches with this way - thats why I wanted to try it out with this transform.

Well the sprite position isn't changing thats right but also the position of Player isn't - when I debuglog this.Transform the values are always the same - take a look above at my first post - same reaction

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Sprite within Object won't move with it
« Reply #8 on: February 22, 2015, 04:00:26 pm »
Quote
this.Transform.Translate(velocity, 0.0f);
You can't do that. this.Transform is a readonly instance, although it is not obvious since C# doesn't have anything to enforce this (in C++ it is const). The only way to alter a Transformable's transform is to play with its position, rotation, scale or origin. The Transform property is the result of the combination of all these components.

But you don't need to do that, take the other version (this.Position += ...) and everything should work as expected.
Laurent Gomila - SFML developer

StormWingDelta

  • Sr. Member
  • ****
  • Posts: 365
    • View Profile
Re: Sprite within Object won't move with it
« Reply #9 on: February 22, 2015, 04:12:44 pm »
odd normally if I want something Readonly I make the setter part of the property either protected or private and leave the getter public.  There some reason that can't be done? ???
I have many ideas but need the help of others to find way to make use of them.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Sprite within Object won't move with it
« Reply #10 on: February 22, 2015, 05:29:42 pm »
There's only a getter (you can't call this.Transform = something), but once you get the transform, there's no way to say that this is a constant instance and that you are not allowed to modify it.
Laurent Gomila - SFML developer

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Sprite within Object won't move with it
« Reply #11 on: February 22, 2015, 06:13:50 pm »
Quote
the intersecting of player1 and player2 is mathmatically right - but the displayed sprite of player2 kinda doesn't look right, because player1 isn't hitting it really. I already checked the bounds and positions of player2 and its sprite - can't find a solution.
Is that a different problem?
Laurent Gomila - SFML developer

Falke88

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
Re: Sprite within Object won't move with it
« Reply #12 on: February 22, 2015, 06:45:47 pm »
Quote
the intersecting of player1 and player2 is mathmatically right - but the displayed sprite of player2 kinda doesn't look right, because player1 isn't hitting it really. I already checked the bounds and positions of player2 and its sprite - can't find a solution.
Is that a different problem?


Ah yeh sorry, I was to thrilled to realize that I already gave up a new question - changed it myself already