SFML community forums

Bindings - other languages => DotNet => Topic started by: Falke88 on January 11, 2015, 05:10:47 pm

Title: Them GlobalBounds are wrong !
Post by: Falke88 on January 11, 2015, 05:10:47 pm
Heya Sisters and Brothers!

I'm on my little learning project game and therefore was just about to solve a little mind puzzle for shooting a projectile onto an enemy and destroy it so hard!!!

El Problemo appears when my hitBox fields I just created aren't working. Actually I didn't tested them before that's why I came along this semantic error just now :(

I have two Entity Class types - one is called "Mack" and one is called "Mob" - Mack is the player here :D

Both have assigned a hitbox field (hitBox = this.sprite.GlobalBounds()).

(http://up.picr.de/20661388cu.jpg)


Buttttt as you can maybe see in my screenshot - even tho these two entitys wont touch each other in the level scene they still have the same hitbox values. Actually the documentation says the hitbox will be the coordinates of bounds within a 2D Space. But in my example it kinda isn't. Or maybe it is but the 2D Space isn't the same as the Level view and gameplay scene actually.

When I move my character along the hitbox aka GetGlobalBounds() won't change either. It stays with the same values all the time.

I want to show the Class Hierarchy of an Enemy or Player Entity mybe the problem lies within' polymorphy here anyhow:

(http://up.picr.de/20661405bo.jpg)

If I move my Entities they manipulate the Position Property of Transformable. And If I want my sprites GlobalBounds I'll get them within Enemy or Mack. Well Actually there shouldn't be a problem or am I wrong?



Title: Re: Them GlobalBounds are wrong !
Post by: eXpl0it3r on January 11, 2015, 05:50:12 pm
Do you copy the GlobalBounds() once or do you actually query it once you want to use the hitbox. If you just create a copy of the bounds, then they obviously won't be updated.
Title: Re: Them GlobalBounds are wrong !
Post by: Falke88 on January 11, 2015, 07:56:46 pm
hitBox = sprite.GetGlobalBounds();

Well thats how I assign it. Implicit assignment of a reference type always delivers a reference isn't it? Since it isnt a value type I don't need to add anything to make it explicit a ref.

Maybe I miss some core C# syntax here - can you point me to it? Because I feel like I did everything alright.

Title: AW: Them GlobalBounds are wrong !
Post by: eXpl0it3r on January 11, 2015, 08:39:06 pm
I don't know the C# specifics, but if you look around you'll see other threads stating that you don't get a referenced object.
Title: Re: Them GlobalBounds are wrong !
Post by: Falke88 on January 11, 2015, 09:42:18 pm
ouhhhh me stupid...

I just realised that FloatRect is a struct == valueType.

Damn therefore I need to make this assignment a reference ...ha funny thing I didn't used valueTypes that much and never needed a reference to it...now its going to be interesting :P
Title: Re: Them GlobalBounds are wrong !
Post by: eXpl0it3r on January 11, 2015, 09:52:22 pm
I'm not sure if you can just use a reference, since underneath SFML.NET uses CSFML which uses SFML directly and the returned FloatRect from the C++ API is returned by value (http://www.sfml-dev.org/documentation/2.2/classsf_1_1Sprite.php#a203d2d8087bfdca2ebc3c0485cdb7409), so it won't update automatically and you instead have to always query the GlobalBounds.
Title: Re: Them GlobalBounds are wrong !
Post by: Falke88 on January 14, 2015, 09:03:38 am
Hmm well...

Quering for GlobalBounds all the time makes me change my method to this:


public FloatRect GetHitBox()
        {
            return sprite.GetGlobalBounds();
        }


Still the weird thingie here is:

[FloatRect] Left(-25) Top(-25) Width(50) Height(50)

thats the GlobalBounds Values for my player and the mob I spawned in the level. Both have the same values even tho they both are moving around.

What I'm missing here so badly?

EDIT: One solution might be that I calculate the hitbox Position by calculating it out (Position.x + hitbox.left...etc) But I don't think that's the best approach here.


greets Charlie
Title: Re: Them GlobalBounds are wrong !
Post by: Laurent on January 14, 2015, 10:26:24 am
Could you please provide a complete and minimal code that reproduces the problem?
Title: Re: Them GlobalBounds are wrong !
Post by: Falke88 on January 15, 2015, 07:52:57 am
Sorry for that early post - but gladly you slammed in before I made anything redundant.
Here's how I tried to sum it up as short as possible.

GameWorld is actually the level with window, backgrounds etc. As I read through the documentation GetGlobalBounds() will return the Coords within 2D space.
Now actually in the if statement of GameWorld "Touching" is called all the time even tho the two entitys aren't nearby. Printing their GlobalBounds will always show the same values even when they're moving - and they both have the same values aswell.


class Enemy{
  Sprite sprite = new Sprite();
  Utility.CenterOrigin(sprite); // centers the LocalBounds only;

  public FloatRect GetHitbox(){
    return sprite.GetGlobalBounds();
  }
}
 

class Player{
  Sprite sprite = new Sprite();
  Utility.CenterOrigin(sprite); // centers the LocalBounds only;

  public FloatRect GetHitbox(){
    return sprite.GetGlobalBounds();
  }
}
 

class GameLevel{
  RenderWindow window;
  [...etc.]
  Player player = new Player();
  List<Enemy> enemy = new List();
  enemy.Add(new Enemy(), new Enemy());

  if(enemy[0].GetHitBox().Intersects((player.GetHitbox())))
                Console.WriteLine("Touching!");
}
 
Title: Re: Them GlobalBounds are wrong !
Post by: Laurent on January 15, 2015, 08:04:56 am
We don't care about understanding the design of your game code, we just want some simple, complete and minimal code that only reproduces the problem (and only that). If it's not clear, you have to write this code yourself, don't try to post parts of your original code ;)

http://sscce.org/
Title: Re: Them GlobalBounds are wrong !
Post by: Falke88 on January 15, 2015, 08:23:10 am
*Changed
Title: Re: Them GlobalBounds are wrong !
Post by: Falke88 on January 15, 2015, 04:00:52 pm
Did I managed to give you the idea and reproduction of the problem here good enough?
Title: Re: Them GlobalBounds are wrong !
Post by: Laurent on January 15, 2015, 04:04:41 pm
No. Please read http://sscce.org/ carefully.
Title: Re: Them GlobalBounds are wrong !
Post by: Falke88 on January 15, 2015, 04:08:21 pm
So...should I post an code example which you guys can actually run ??? ?
Title: Re: Them GlobalBounds are wrong !
Post by: Jesper Juhl on January 15, 2015, 04:10:44 pm
Yes. That is the whole point.
A minimal runnable example.
Just a main function with just enough to reproduce the problem.
Title: Re: Them GlobalBounds are wrong !
Post by: Falke88 on January 15, 2015, 04:47:51 pm
Wow in this whole process of making this understandable for you in a test class I managed to resolve the problem by myself :D

The problem was: GetGlobalBounds will give back the coords of the next higher instance the sprite belongs to. In my case sprite is within a Player or Enemy object. These object actually move but the Sprite within the object range isn't moving.

Thanks for your patience anyway guys! finally I can move along coding !
Title: Re: Them GlobalBounds are wrong !
Post by: Laurent on January 15, 2015, 04:55:09 pm
Quote
Wow in this whole process of making this understandable for you in a test class I managed to resolve the problem by myself
Now you understand why we insist so much on the complete & minimal code ;)
Things become so clear once you've removed all the unnecessary code.