Hello,

I'm currently working on a simple SFML game using its 2D library.

Here's the part of the code the problem is coming from:

data.SetCenter(data.GetSize() / 2.0F);
data.Resize(50,50);
data.SetColor(sf::Color(255,255,255));

If I remove the call to SetCenter, the square shows up fine.
But when I add the call to SetCenter, the square isn't visible.

Help?

Dani AI

Generated

The square disappearing when you call SetCenter is almost always an origin/position issue rather than a color or drawing bug. Setting the center changes the point that the object’s position refers to; if you set that center before changing the size (or without reapplying a position), the sprite can be moved off-screen. What suggested about repositioning is correct — the origin affects where the object is drawn.

Practical checklist to fix this kind of problem:

  • Resize the shape to its final size before you change its center/origin.
  • After changing the center/origin, set (or reapply) the position you expect, because position is measured from the new origin.
  • If you use a newer SFML version, the API name changed (use setOrigin / getLocalBounds or setSize), so compute the origin from the final size or local bounds.
  • For debugging, print the object’s size, origin and position or draw a temporary outline/cross at the position to verify where SFML thinks the object is.

If the object still does not show, double-check the alpha channel and the active view (the object might be outside the current view). The SFML docs explain how origin and transforms work and are useful reference material: the Transformable reference and the graphics transform tutorial both describe why order (size -> origin -> position) matters and how to compute a centered origin in modern SFML (Transformable class, Graphics transform tutorial).

Recommended Answers

All 2 Replies

Um, help anyone?

I don't know much about SFML, but if you are changing the center (I am guessing thats the same thing as an origin), it is essentially changing location of where the square is draw relative to it's position. You should try repositioning the square relative to the new center.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.