Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

Finding Index of Class Array


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 GameCreator   Members   -  Reputation: 451

Like
0Likes
Like

Posted 23 October 2012 - 03:52 PM

Pretend I have a simple class which includes the following function:
int object::getIndex()
{
   return ??????????;
}

I then declare the class in main and go through each object.
object myobjects[50];

for (int i = 0; i <50; i++)
   cout << "Object index: " << myobjects.getindex();

What do I replace the question marks with to make the program print the index of the specifc object being addressed?  Is this possible?  I wasn't able to find the answer after several searches on Google.

Edited by GameCreator, 23 October 2012 - 03:53 PM.


Sponsor:

#2 SiCrane   Moderators   -  Reputation: 6672

Like
1Likes
Like

Posted 23 October 2012 - 03:57 PM

In general, no, this isn't possible. Classes don't have any information about how they are allocated, if they are part of an array or part of a larger object or what now. If myobjects is a single global array then you could do something like that. What problem are you trying to solve with a function like this?

#3 GameCreator   Members   -  Reputation: 451

Like
0Likes
Like

Posted 23 October 2012 - 05:03 PM

I think I'm going about it the wrong way. I was thinking of creating an array class of characters who can move around. There would be a character[i].move() function that detects collisions. But the characters need to collide against eachother too. So I was trying to somehow figure out how the move function could check to see if the specific character collided against the others in the array (excluding itself). But it seems like I would need to do this in some outside function instead.

#4 Brother Bob   Moderators   -  Reputation: 4654

Like
1Likes
Like

Posted 23 October 2012 - 05:06 PM

Your physics code is responsible for moving objects around and handling collisions. So yes, that would be handled in code outside the character.

#5 GameCreator   Members   -  Reputation: 451

Like
0Likes
Like

Posted 23 October 2012 - 05:16 PM

Hmm. character.move() seems so natural to me. So you'd do something like physics.movecharacter(i)?

#6 Brother Bob   Moderators   -  Reputation: 4654

Like
1Likes
Like

Posted 23 October 2012 - 05:33 PM

How you choose to name your functions, if you make them members of a physics object, if want the physics code to have direct access to the characters position of if the physics code moves it with a move function in the character class, or any other details was not the point of my reply. I just commented on that the character should not be responsible for moving itself and checking for collisions, but that the you physics code is responsible for the physics if your game.

If you want to do character.move(), then go ahead and do so. But it is the physics code that should be responsible for deciding how to move an object (the distance and direction to move the character, not how to actually access and alter the characters X and Y coordinates) and if there are any collision. For example, the physics code will call move() on the character only if there are no collisions. If the physics code tells the character to move 5 units forward, then the character should do just that, and it is up to the physics code to ensure that the character can move 5 units forward.

Edited by Brother Bob, 23 October 2012 - 05:34 PM.


#7 L. Spiro   Crossbones+   -  Reputation: 5186

Like
1Likes
Like

Posted 23 October 2012 - 05:35 PM

In general physics systems don’t know what characters are.
They know what primitives such as spheres, boxes, and convex hulls are.
But explaining a full physics system on a forum is neither practical nor likely necessary, since for a basic system it can be dumbed down significantly.
Why don’t you explain what kind of physics you are planning to do? 2D or 3D? How advanced does it need to be?


I am going to guess you just want physics that are so basic, the only functionality you need is to separate players from each other (and walls) on contact (but I will only talk about characters).

The overall plan would be to advance all the characters in time as you normally do, moving them either by AI logic for NPC’s or from keyboard/mouse input for actual players.
Then your primitive “physics” system runs. Optimizations aside, it looks at each character compared to each other character and checks for intersection via sphere (3D) or circle (2D).
Then both characters are moved away from each other by the exact amount that causes them to no longer be touching.
Move on to the next character pair.

You don’t have to keep track of what character pairs were already made because the loop prevents duplicates.

for ( int I = 0; I < Characters; ++I ) {
    for ( int J = I + 1; J < Characters; ++J ) {
        // Check characters I and J.
    }
}


L. Spiro
It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#8 GameCreator   Members   -  Reputation: 451

Like
0Likes
Like

Posted 23 October 2012 - 08:41 PM

Thanks for the help. I'm trying to do a 2D platformer with a world, characters and moving platforms. Characters would bump into each other (so you can't run past an enemy). Characters and platforms will be represented by simple rectangle collision boxes. The world is more complex as there are ramps. I'll try to figure it out but I may have to hire someone. (I've also looked at documentation of physics engines like box2d and chipmunk but they seem more complex than I can handle.)




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS