Jump to content
  • Advertisement
Sign in to follow this  
Crusable77

Is this bad?

This topic is 1870 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I am working on a project in sfml and I am making a very basic tile map editor. I am working on the tile selection and this is how I got the selected tile to snap to a 64* 64 tile:

 

 
m_sfSelectedTilePos.x = (float)(((int)m_sfEvent.mouseButton.x / 64) * 64);
m_sfSelectedTilePos.y = (float)(((int)m_sfEvent.mouseButton.y / 64) * 64); 

Share this post


Link to post
Share on other sites
Advertisement
I don't think it is bad, but I think it is better to work with integers. Assumming the position of a tile is the top left corner, then a tile at index (1, 2) should have the position (1 * 64, 2 * 64) = (64, 128). So the float position can be calculated only when absolutely needed.

So then the index of a tile would be calculated by
m_sfSelectedTileIndex.x = m_sfEvent.mouseButton.x / 64;
m_sfSelectedTileIndex.y = m_sfEvent.mouseButton.y / 64;
Edited by ultramailman

Share this post


Link to post
Share on other sites

When you mean work with ints do you mean make all my floats into ints. Most SFML's classes I am using uses sf::Vector2f's.

Share this post


Link to post
Share on other sites

Two maxims: Programmers should never repeat themselves, and magic numbers are bad.

 

 

Create a function to snap to a tile.  Perhaps use the signature Vector2F SnapToTile(Vector2F source) { }.

 

Second, rather than using the constant 64, instead use a static const int defined in a header file somewhere.

 

 

Your version will always snap to the nearest border, and perhaps someday you would rather the function snap to the nearest tile boundary.

Share this post


Link to post
Share on other sites

When you mean work with ints do you mean make all my floats into ints. Most SFML's classes I am using uses sf::Vector2f's.

Not at all, the position can still be float. Simply multiply the index by 64, and cast to a float.

Share this post


Link to post
Share on other sites

Two maxims: Programmers should never repeat themselves, and magic numbers are bad.

 

 

Create a function to snap to a tile.  Perhaps use the signature Vector2F SnapToTile(Vector2F source) { }.

 

Second, rather than using the constant 64, instead use a static const int defined in a header file somewhere.

 

 

Your version will always snap to the nearest border, and perhaps someday you would rather the function snap to the nearest tile boundary.

 

Your way of doing things is not bad, but Frob's way will probably work better in the long run.  Quickly adding to Frob's answer, I would poll the values from somewhere.  Most editors have a drop down menu that lets you select snapping values.

Share this post


Link to post
Share on other sites

I'd recommend using floor() rather than casting to int back and forth. The casting can be several times slower (if performance is an issue)

 

Note that floor( -3.3 ) = -4; while (int)-3.3 = -3; but floor's behavior is usually how you want snapping to work if negative numbers are possible.

Share this post


Link to post
Share on other sites

By the way, you can use floor() instead of converting to ints:

 

m_sfSelectedTilePos.x = floor(m_sfEvent.mouseButton.x / 64) * 64;
 

Edit: oops, just saw that Matias already said it. Sorry!

Edited by DaleyPaley

Share this post


Link to post
Share on other sites


Two maxims: Programmers should never repeat themselves...

What did you mean by this?


The code in the first post contains two lines of code that are almost identical. Chances are you can give the operation a good name and put it in a function.

I also recommend writing operations on vectors or points instead of having high-level code do everything component by component.
float snap_to_multiple(float x, float size) {
  return std::floor(x / size) * size;
}

Point snap_to_grid(Point p, float tile_size) {
  Point result;
  result.x = snap_to_multiple(p.x, tile_size);
  result.y = snap_to_multiple(p.y, tile_size);
  
  return result;
}

// ...

m_sfSelectedTilePos = snap_to_grid(m_sfEvent.mouseButton, 64.0f);

Edited by Álvaro

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!