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

Author Topic: Contains() usable on multiForm Sprites?  (Read 6648 times)

0 Members and 1 Guest are viewing this topic.

Falke88

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
Contains() usable on multiForm Sprites?
« on: September 30, 2014, 09:09:15 am »
Hey Guys!

I have a decent problem that I find difficult to solve due the possibilities provided by SFML.net yet.

Please have a look at this picture of the game:


As you can see there are HexFields on it. Actually every Hex is a sprite so the field wasnt really drawn onto the map itself due a specific reason. I want the sprites to act as magnetic objects to my Icon Transitions. So I can set new positions relative to the middle of each hexfield.

The problem which eruptes here is the rectangle containment of every Hex Sprite. I didn't read the documentation right and skipped the fact of the Contains() Method just provided for Rectangle Sprite Textures. Therefore even the "invisible" rectangle border is used.
One Major Problem that appears here in this case is when I try to click on a hexfield where the i.e. right lower edge of the hex above may be drawn over the other one...so when I think I clicked the chosen hexfield the icon actually moves to the one above due the fact that I really just clicked the containment rectangle of the above hex sprite.

Is there any way to only let the visible part being able to be clicked?

Even just a circle contains() method might work fine enough

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Contains() usable on multiForm Sprites?
« Reply #1 on: September 30, 2014, 09:21:11 am »
You can find a bunch of good info on hex grids and coordinates here.
I hope that helps you  :)

Falke88

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
Re: Contains() usable on multiForm Sprites?
« Reply #2 on: September 30, 2014, 09:39:50 am »
Hey there,

Thanks for the nice information source really worth a look. Sadly I am already into the current game and dont wanna start everything over with new information....yet.

Also the problem doesnt has something to do with hex' in fact - more with the use of Contains() Method @ non rectangle sprites

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Contains() usable on multiForm Sprites?
« Reply #3 on: September 30, 2014, 09:44:47 am »
Since SFML only provides very basic rectangle collision detection functions, you have to implement your own test that matches the real shape of the tiles. SFML doesn't have a function that will magically match this specific case, you must implement one of the algorithms described in the article given by Jesper Juhl.

Quote
the use of Contains() Method @ non rectangle sprites
A sprite is always a rectangle. SFML knows nothing about the fact that the content of the texture makes it look non-rectangular...
Laurent Gomila - SFML developer

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
AW: Contains() usable on multiForm Sprites?
« Reply #4 on: September 30, 2014, 09:52:05 am »
The contains() function is part of sf::Rect, which is short for rectangle, thus it will only check for rectangle shapes.

If you something similar for hexagon shapes, use the info provided by Jesper Juhl.
For circles it's as easy as to check whether the distance between the center and your point is smaller or equal to the circle radius.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Contains() usable on multiForm Sprites?
« Reply #5 on: September 30, 2014, 05:56:13 pm »
There is some source on the wiki that allows pixel collisions and circle collisions, if they are enough for what you need.

That said, calculating whether a point is inside a hexagon or not should be trivial and you could create your own function for that and use it instead of SFML's rectangular contains().
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Falke88

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
Re: Contains() usable on multiForm Sprites?
« Reply #6 on: September 30, 2014, 09:07:59 pm »
Well I think Ill do it that way then - seems to be no other solution...am suspicious if I can get this thingie to work :3

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Contains() usable on multiForm Sprites?
« Reply #7 on: September 30, 2014, 09:14:26 pm »
I don't see where the big problem is.
SFML gives you easy tests for whether rectangles intersect, but that's not what you need (except maybe to do some early tests to see if you could possibly be intersecting a given hex at all). So don't use that.
You need to figure out what hex a given mouse coordinate is in, so just implement one of the algorithms I already linked to that enables you to do this - it's not that hard and there are plenty of examples.

Falke88

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
Re: Contains() usable on multiForm Sprites?
« Reply #8 on: September 30, 2014, 09:43:42 pm »
ALright will do...

BTW I found something here : http://www.sfml-dev.org/tutorials/2.0/graphics-shape.php

@ Regular polygons sections...

theres -> sf::CircleShape octagon(80, 8); //surely changeable to hexagon...may this work out anyhow aswell?

EDIT: meh just saw the GlobalBounds of type RECT again - so Contains() again with RECT only :P doh

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Contains() usable on multiForm Sprites?
« Reply #9 on: September 30, 2014, 10:29:25 pm »
If you want to try something interesting, you could try the even-odd rule using line crossing checks:

You create a line between a point which you know for certain to be outside of the hexagon (any polygon, actually) to the point you wish to test. That certainly outside point could be just next to the hexagon or way off the screen; it's your choice.

You test this line to see if it crosses any of the six lines around the hexagon and count how many times it cross a line. For a hexagon, this will be zero, one, or two.

If it crosses an even number of times (zero or two), it's outside. If it crosses an odd number of times (one), it's inside.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Falke88

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
Re: Contains() usable on multiForm Sprites?
« Reply #10 on: October 01, 2014, 07:07:11 am »
Heya Dude,

Well this sounds simple  but still abstract to me since I don't have any mathmatical background related to hexagons at all - gonna read the source mentioned above for a couple of weeks to get a good touch on this.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Contains() usable on multiForm Sprites?
« Reply #11 on: October 01, 2014, 06:46:09 pm »
this sounds simple  but still abstract to me since I don't have any mathmatical background
You only need to be able to check if one line crosses another  ;)
It works for any polygon too, if you need it to.

It may be (a little) faster to use a dedicated hexagon detection routine but you'll probably need more mathematical understanding for that. That said, learning the hexagon way will probably help general understand of things. I may go read it too. Learning is fun  :)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*