-2
\$\begingroup\$

I have a c++ problem.

So I have a base GAME class. In this class I have an instance of the player class. I want to check this player for collision with tiles. Tiles are also stored in the game class. How do I get the instance of these tiles.

I tried passing in an instance of the game class into the constructor of the player class and accessing it that way, but no success.

So for example:

Class game()
{
     Player p(x, y);
     Tile t(x, y);

     //game loop
     p.update(); //check collision

     p.render();
     t.render();
}

Player::Player(int x, int y)
{
    this.x = x;
    this.y = y;
}

Player::update()
{
     //get an the instance of tile to check the collision
}
\$\endgroup\$
1
  • 5
    \$\begingroup\$ I do not think this is the good place for this question, as it is only a C++ level issue. I would suggest you to read a good tutorial again and everything will make sense to you eventually \$\endgroup\$ Commented Nov 29, 2016 at 9:33

2 Answers 2

0
\$\begingroup\$

Simply pass a reference to the tile to the update function:

Class game()
{
     Player p(x, y);
     Tile t(x, y);

     //game loop
     p.update(&t); //check collision

     p.render();
     t.render();
}

Player::Player(int x, int y)
{
    this.x = x;
    this.y = y;
}

Player::update(Tile* tile)
{
     //You now have a pointer to tile
}
\$\endgroup\$
2
  • 1
    \$\begingroup\$ I love when the people just downvotes with no reason or explanation... \$\endgroup\$
    – Gusman
    Commented Nov 29, 2016 at 19:41
  • \$\begingroup\$ Better is when they downvote because they disagree with your approach to answering a question. I would say though, that you should expand your answer to include why your reference pass addresses the issue. OP doesn't appear to have a grasp on the fundamentals of scope and how to pass references around. \$\endgroup\$
    – BobGneu
    Commented Nov 29, 2016 at 21:19
-1
\$\begingroup\$

There is definitely a level of guesswork in understanding your question, but a couple things that may be of note.

  1. your player has no reference to your tileset and would need to have access to it if you wanted to brute force detection - iterating through all of them and acting once you find the tile(s) she is touching.
  2. your player could use space partitioning, as with a BSPTree, QuadTree or OctTree, to run queries against subsets of valid elements. You may want to reach into wikipedia for more details on this and to think through how to implement it in your context.

Both solutions would likely work, the second being a better, albeit more involved, approach.

Here is a discussion of some of the techniques used in 2D.

Hopefully this is helpful.

\$\endgroup\$
3
  • \$\begingroup\$ Better ask OP for clarification than posting an out of topic answer \$\endgroup\$ Commented Nov 29, 2016 at 9:29
  • \$\begingroup\$ Although in part i do agree with the previous note, the OP has a pretty clear goal of handling collision between tiles and a player character. \$\endgroup\$
    – BobGneu
    Commented Nov 29, 2016 at 9:50
  • 2
    \$\begingroup\$ The question is : "How do I get the instance of these tiles?". Whenever tiles are involved it is a good idea to use space partitionning of course, but that would fit in a comment as it does not answer the actual question \$\endgroup\$ Commented Nov 29, 2016 at 9:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .