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

Author Topic: Object.Position and Object.sprite's displayed texture don't match  (Read 3053 times)

0 Members and 1 Guest are viewing this topic.

Falke88

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
Please take a look at the code - run it once.

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.


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 = 5.0f;

        public override void UpdateCurrent(float deltaTime)
        {
            this.Position += velocity * new Vector2f(deltaTime, 0.0f);
        }
    }

    class TestPlayer : EntityTest
    {

        public Sprite sprite;

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

        public override void UpdateCurrent(float deltaTime)
        {
            base.UpdateCurrent(deltaTime);
            sprite.Position = this.Position;
        }

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

    class TestClass
    {

        RenderWindow window;
        TestPlayer player1;
        TestPlayer player2;
       
        public TestClass()
        {
            window = new RenderWindow(new VideoMode(640, 480), "TestClass");
            player1 = new TestPlayer();
            player2 = new TestPlayer();

            player1.Position = new Vector2f(0.0f, 0.0f);
            player2.Position = new Vector2f(50.0f, 0.0f);
            player2.sprite.Position = player2.Position;

        }

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

                player1.UpdateCurrent(0.016f);

                window.Clear(Color.Blue);
                window.Draw(player1);
                window.Draw(player2);
                window.Display();

                if (player1.sprite.GetGlobalBounds().Intersects(player2.sprite.GetGlobalBounds()))
                {
                    Console.WriteLine("Hit!");
                }
            }
        }
    }
}

 

Falke88

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
Re: Object.Position and Object.sprite's displayed texture don't match
« Reply #1 on: February 23, 2015, 11:38:31 pm »
A little hint to find the solution by myself would be already enough brothers!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Object.Position and Object.sprite's displayed texture don't match
« Reply #2 on: February 24, 2015, 07:53:09 am »
Can't you be more specific about your problem? Is it sprite1 which is drawn in wrong coordinates (and if so, which coordinates?)? sprite2? the bounding rectangles?

Debug, print values, ... try to figure out by yourself which variables don't have the value you expect. It shouldn't be hard with such a small code.
Laurent Gomila - SFML developer

Ztormi

  • Jr. Member
  • **
  • Posts: 71
  • Web developer by day. Game developer by night.
    • View Profile
Re: Object.Position and Object.sprite's displayed texture don't match
« Reply #3 on: February 24, 2015, 11:52:56 am »
I believe I figured out what's happening here. You are setting your sprites' position twice. You already set the position of your sprites on update. Then on Draw, you apply entity's current transform (position) into renderstates.  SFML.Graphics.Sprite is a Transformable too so you are actually drawing sprite into entity's position + sprite's position. Result is that the sprites position property doesn't represent what you see on screen, it's real position is actually to the left but it's drawn more to the right.

If you comment states states.Transform *= this.Transform; it works. But I'd suggest you to implement your own GetGlobalBounds for entities/physicsobjects instead. Chances are you're gonna need a different bounding box from the sprite anyway (ie. sprite is not cropped or has empty space around it) so I think it'd be a good idea to do that first. Separating things like this from sprites and drawing is a good idea anyway imo.
« Last Edit: February 24, 2015, 12:01:03 pm by Ztormi »

Falke88

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
Re: Object.Position and Object.sprite's displayed texture don't match
« Reply #4 on: February 24, 2015, 03:36:34 pm »
I believe I figured out what's happening here. You are setting your sprites' position twice. You already set the position of your sprites on update. Then on Draw, you apply entity's current transform (position) into renderstates.  SFML.Graphics.Sprite is a Transformable too so you are actually drawing sprite into entity's position + sprite's position. Result is that the sprites position property doesn't represent what you see on screen, it's real position is actually to the left but it's drawn more to the right.

If you comment states states.Transform *= this.Transform; it works. But I'd suggest you to implement your own GetGlobalBounds for entities/physicsobjects instead. Chances are you're gonna need a different bounding box from the sprite anyway (ie. sprite is not cropped or has empty space around it) so I think it'd be a good idea to do that first. Separating things like this from sprites and drawing is a good idea anyway imo.


Thanks for the tip - it really was that issue. I just need to figure out what to change now exactly - because having an Transformable Object without having it transform being changed when commenting out the mentioned code is a bit redundant.

I think I'm gonna apply a BoundingBox in my Player Class which depends on position and known dimensions of the texture of the sprite. That way it doesn't matter whats up with the drawn position etc.

 

anything