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

Author Topic: Merging sprite's position with the position of the owning class?  (Read 5310 times)

0 Members and 1 Guest are viewing this topic.

Falke88

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
Hey Guys!

I have a little thing I would like to make more nicer.

I have a Class called "Actor" - it contains a Sprite Object.

Both are inheriting from "Transformable" - so both have a unqiue Position Vector.
Is there a way to merge the Sprite's Position to the Actor's one?
I could assign "sprite.Position = actor.Position;" for sure.
But maybe theres a nicer solution to do this. Something with Transform mybe.
Sadly I'm not that fresh with that.


greets Charluie

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Merging sprite's position with the position of the owning class?
« Reply #1 on: May 31, 2015, 06:29:16 pm »
It depends for what you need to do that. The usual use case is at draw time, which is well documented: you just multiply the RenderStates' transform with the owner class one before drawing the inner sprite. If it's another use case, please describe it.
Laurent Gomila - SFML developer

Falke88

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
Re: Merging sprite's position with the position of the owning class?
« Reply #2 on: May 31, 2015, 07:10:08 pm »
I'm sure it's well documentated. But I'm also sure I'm too dumb to find it :D


Here is my Draw-Method. It's within an Render Object. Because I wanted a little decoupling from the Actor-Class.



It is called from here the Actor's Draw-Method FYI:



Can you mybe give a hint on how to approach now? I read through the doc, the Transformable Class and the Transform Class. But I find a lot of ways which may fit - but still I don't even know how I should use them.
Should I use Translate() or just multiply.
You see I'm a bit overwhemled by the possibilites :D


Thanks in advance!


Falke88

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
Re: Merging sprite's position with the position of the owning class?
« Reply #3 on: May 31, 2015, 07:43:04 pm »


AH ! I managed it !

I never was thinking about "states" being some kind of transporter through the method so I can assign Transform Values to it while its going through my Draw-Method. Kinda cool way!


Thanks for you time anyway!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Merging sprite's position with the position of the owning class?
« Reply #4 on: May 31, 2015, 11:09:23 pm »
A more correct version is

states.Transform *= this.Transform

... so that the initial transform of the RenderStates, if there is one, is preserved and not overwritten.
Laurent Gomila - SFML developer

Falke88

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
Re: Merging sprite's position with the position of the owning class?
« Reply #5 on: June 04, 2015, 09:34:31 pm »
Hey my friends! It's me again!

The above problem was solved and I successfully could decouple the Sprite from my GameObject.

Now I have a new problem which lets me thing about if I should couple Sprite to the GameObject again.

We want to have a collision check. Since Sprite Draw Position is actually temporarily because of the way I used states (see above) and its Transform - The Sprite Position is never updated with the GameObjects Position. The Drawing works well but collision checks will be wasted.

I could do the following after Sprite is coupled to the GameObject:



_sprite.Position = _player.Position;
 

I don't like that because I'd have to couple stuff.

What I tried should work but it doesn't. I think I just use the method the wrong way.

My Approach:

Since I multiplied the GameObject's Transform (this) to the states - states now holds the Position for the GameObject:



The next call is the Draw Method within the Render-Class (which owns the Sprite object)



Why I've chosen the Combine-Method here? Because the Doc tells me it works like multiplie (which I used in the GameObject-Class above) and I can't "combine" it otherwise because the Sprite's Transform is immutable)



Sadly _sprite.Position did not changed as you can see in this debug (Breakpoint active @ 100 iterations of Draw-Method)




I hope I provided you guys with enough information about the problem!
I'd really like to keep things decoupled but still changing the _sprite.Position.


Thanks for any help in advance


Charlie

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Merging sprite's position with the position of the owning class?
« Reply #6 on: June 05, 2015, 09:30:35 pm »
Its not exactly clear what you are trying to achieve. But with regards to the transform question. Transform.Combine(...) function returns the combined transform, so you need to assign it to something.

states.Transform = _sprite.Transform.Combine(states.Transform);

Which if you were also reading the documentation you would know it is the same as...

states.Transform *= _sprite.Transform
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Falke88

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
Re: Merging sprite's position with the position of the owning class?
« Reply #7 on: June 06, 2015, 10:39:36 am »
_sprite.Transform = _sprite.Transform.Combine(states.Transform);

The problem is I can't assign the returned Transform because its read-only. So I don't see a way to assign _sprite's Transform to the Transform within "states".

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Merging sprite's position with the position of the owning class?
« Reply #8 on: June 06, 2015, 12:26:06 pm »
Read the code I posted again. I never assigned it to the sprite. If you want to change the transform when drawing the only way to do it is with the render states' transform.
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Falke88

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
Re: Merging sprite's position with the position of the owning class?
« Reply #9 on: June 06, 2015, 01:10:25 pm »
I got you there. I thought there would be a other way around. So i can assign the render states Transform to the sprite.