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

Author Topic: Collision with enemies being managed by a 'manager' class  (Read 4336 times)

0 Members and 1 Guest are viewing this topic.

anoynlk

  • Newbie
  • *
  • Posts: 15
    • View Profile
Collision with enemies being managed by a 'manager' class
« on: April 03, 2017, 09:06:53 pm »
Hey guys,

Programming my first game using SFML here and encounter a problem when testing collision between the player and an enemy.

Too add some context, the player is falling past enemies and is using A + D to avoid them, when he collides with an enemy it will kill the enemy or move it off screen (thus killing it) and for testing purposes is moving the player to the top left of the screen aswell. The enemies are being managed by a manager class and the player is not.

code as follows for the collision check in game.update();
Code: [Select]
for (int i = 0; i < enemy1M.getEnemies().size(); i++)
{
if (enemy1M.getEnemies()[i].isAlive()) // check if alive first
{
// call the slow enemies function if they collide
player.collisionResponse();
enemy1M.getEnemies()[i].collisionResponse();
//enemy1M.getEnemies()[i].setPosition(850, 850); <-- this does not work either
}
}

if you need to see any other code let me know and i'll post asap (i'll be monitoring this thread) - didn't want to spam the page with unnecessary code!

Thanks for any time you contribute!   :)

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Collision with enemies being managed by a 'manager' class
« Reply #1 on: April 04, 2017, 01:08:54 am »
"this does not work either"
How do you know? What does it do? Why is that different from what you expect it to do?

What is the object type of:
enemy1M.getEnemies()[i]
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

anoynlk

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Collision with enemies being managed by a 'manager' class
« Reply #2 on: April 04, 2017, 02:08:49 am »
Hey Hapax thanks for responding!

the line
Code: [Select]
enemy1M.getEnemies()[i].setPosition(850, 850);

Does pretty much the exact same as the line

Code: [Select]
enemy1M.getEnemies()[i].collisionResponse();

As the collision response is simply to move the enemy off screen.

The enemy1M is an object of the enemy 1 manager class which is creating enemies using a vector like so

Code: [Select]
// inside the enemy 1 manager constructor
for (int i = 0; i < 6; i++)
{
enemies.push_back(Enemy1());
enemies[i].setAlive(false);
enemies[i].setTexture(&texture);
enemies[i].setSize(sf::Vector2f(50, 50));
}

and the line you quoted is the following;

Code: [Select]
// from the enemy 1 manager .cpp
std::vector<Enemy1> Enemy1Manager::getEnemies()
{
return enemies;
}

Hope this clears it up a bit, trying to be as informative as possible (i'm a noobie ;D)

thanks again!

anoynlk

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Collision with enemies being managed by a 'manager' class
« Reply #3 on: April 04, 2017, 02:10:55 am »
Also when you say;
How do you know? What does it do? Why is that different from what you expect it to do?

It doesn't seem to actually do anything, the object just continues on its normal path (-y axis). When it should(?) set it off screen.(the window i'm using is 800x800)

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Collision with enemies being managed by a 'manager' class
« Reply #4 on: April 04, 2017, 03:13:15 am »
I suppose, then, this transforms my question into:
what is an Enemy1 and what is in its setPosition method?

'It not working' seems to be that it is not being moved with setPosition() but is being moved with collisionResponse(). Is that correct?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

anoynlk

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Collision with enemies being managed by a 'manager' class
« Reply #5 on: April 04, 2017, 03:38:51 am »
I suppose, then, this transforms my question into:
what is an Enemy1 and what is in its setPosition method?

Enemy 1 is a class being managed by enemy manager. This is its constructor & update function:

Code: [Select]
Enemy1::Enemy1()
{
setSize(sf::Vector2f(50, 50));
position.x = 800;
position.y = 800;
setPosition(position);
scale = 10.f;
gravity = -5.0f * scale;
falling = true;
velocity.y = -100;
} // essentially making it travel upwards (all enemies are being being moved this way to give the illusion the player is falling


void Enemy1::update(float dt)
{
// this is always applying negative gravity to the enemy
if (falling)
{
velocity.y += (gravity)* dt;
move(velocity * dt);
}
updateAABB(); // updating AABB collision
}

Quote
'It not working' seems to be that it is not being moved with setPosition() but is being moved with collisionResponse(). Is that correct?

Unfortunately not. The collision response for the enemy is simply

Code: [Select]
void Enemy1::collisionResponse()
{
position.y = 850;
setPosition(position);
}

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Collision with enemies being managed by a 'manager' class
« Reply #6 on: April 04, 2017, 05:54:30 pm »
Quote
'It not working' seems to be that it is not being moved with setPosition() but is being moved with collisionResponse(). Is that correct?
Unfortunately not.
Does this mean I was incorrect or that it is not being moved?

Again, though, what is in the setPosition method?

Note that you are only seemingly changing the y value when using collision response whereas your are explicitly setting both x and y when using setposition directly.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

anoynlk

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Collision with enemies being managed by a 'manager' class
« Reply #7 on: April 04, 2017, 07:18:41 pm »
Yes I mean it has not moved it anywhere. If I do the same for the player however it does indeed move it.
The problem I think lies within the enemies being managed by a manager class.

As for setPosition();

As far as I was aware this was from #include <SFML/Graphics.hpp>?

I should note also that when im changing its y position to offscreen, the manager for the enemy will then update on the next frame and because it is offscreen, it will kill it and respawn it at a new x & y pos.

Hope that makes sense

Thanks again for your time :)

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Collision with enemies being managed by a 'manager' class
« Reply #8 on: April 05, 2017, 01:33:34 am »
Since you haven't shown what the internal value position actually does, I presume that this would be the internal representation of its position. This isn't being updated when you just set the position; it is being updated when you use collision response.

As for setPosition();

As far as I was aware this was from #include <SFML/Graphics.hpp>?
Only if the class inherits from sf::Transformable.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

anoynlk

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Collision with enemies being managed by a 'manager' class
« Reply #9 on: April 05, 2017, 02:30:21 am »
Since you haven't shown what the internal value position actually does, I presume that this would be the internal representation of its position. This isn't being updated when you just set the position; it is being updated when you use collision response.

As for setPosition();

As far as I was aware this was from #include <SFML/Graphics.hpp>?
Only if the class inherits from sf::Transformable.

setPosition(); does indeed inherit from sf::Transmorable. The value of 'position' is set by giving the x and y values such as

Code: [Select]
position.x = xxx;
position.y = xxx;

setPosition(position);

Does that clear it up anymore?

Sorry if i'm being a noob I am pretty new to this!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Collision with enemies being managed by a 'manager' class
« Reply #10 on: April 05, 2017, 06:34:26 am »
Quote
std::vector<Enemy1> Enemy1Manager::getEnemies()
This function returns a copy of the enemy array, not the original one. Therefore, any modification to what is returned won't impact the actual array of enemies.
Laurent Gomila - SFML developer

anoynlk

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Collision with enemies being managed by a 'manager' class
« Reply #11 on: April 05, 2017, 04:54:21 pm »
Quote
std::vector<Enemy1> Enemy1Manager::getEnemies()
This function returns a copy of the enemy array, not the original one. Therefore, any modification to what is returned won't impact the actual array of enemies.

Oh I see.

I thought enemy1M.getEnemies().collisionResponse(); would find the correct enemy in the array.

Am I right in saying I must use pointers to solve this?

And if so could you help me set them up? I'm not very confident using them yet.

Thanks again for the time guys

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Collision with enemies being managed by a 'manager' class
« Reply #12 on: April 05, 2017, 11:11:46 pm »
You can avoid pointers in this case by just returning a reference instead of copy:
std::vector<Enemy1>& Enemy1Manager::getEnemies()
notice the added ampersand (&)

Then, the thing returned would be a reference to that vector; it can be used as if it was the original vector:
getEnemies()[2].setPosition(1.f, 2.f);



Regarding setPosition and sf::Transformable, for a class to have setPosition as its method, that class would inherit from sf::Transformable. Since you didn't provide the declaration of the class (Enemy1?), the inheritance was not shown.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

anoynlk

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Collision with enemies being managed by a 'manager' class
« Reply #13 on: April 06, 2017, 12:22:55 am »
Yeah that's my fault for including vague code.

However simply changing

std::vector<Enemy1> Enemy1Manager::getEnemies()

to

std::vector<Enemy1>& Enemy1Manager::getEnemies()

has fixed it!

Thanks for the input guys! :)

 

anything