Skip to main content
GameDev.net gamedev.net
🔒 Locked

Finding Index of Class Array

Started by GameCreator Oct 23, 2012 at 9:52 PM 6 replies 1.1k views
Original Post
GameCreator
GameCreator
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.
SiCrane
SiCrane
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?
GameCreator
GameCreator
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.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.
Brother Bob
Brother Bob
Your physics code is responsible for moving objects around and handling collisions. So yes, that would be handled in code outside the character.
GameCreator
GameCreator
Hmm. character.move() seems so natural to me. So you'd do something like physics.movecharacter(i)?
Brother Bob
Brother Bob
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.
L. Spiro
L. Spiro
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
I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid
GameCreator
GameCreator
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.)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.