So, after that big discussion in this thread about the ‘best’ storage method for various things, it got me thinking about a problem that I’m planning on tackling for my platformer.
Basically, when an entity is standing on the floor, he should be clamped to it, unless certain special criteria are met (The entity runs off the edge, the entity jumps, the entity runs up a slope and gathers enough vertical momentum that going over the crest allows it to overcome gravity for more than a timestep or two). To accomplish this, I’m making basically turning floors into a faux-list of tiles that the entity gets attached to and ‘slides’ across. The primary benefit of this is that if an entity is standing on static, solid ground then you shouldn’t need to do physics calculations on them to figure out whether they fall through the scenery or need to change their state, ja? Instead, you’d just need to calculate where on this list of tiles the player is, then figure out their Y position by checking the tiles in question and assigning the entity’s height based on the tiles beneath them.
Tiles have some information like height by pixel (Used to perform ‘slope’ positions), a general slope for the whole tile (Used for calculating inclination based speed) and… Other stuff that I can’t remember the importance of at this point. Hah!
I’m torn between several different alternatives for how to do this.
- Store all of the tiles that make up a floor area in an array (Or ArrayList), then convert the entity position into a tile position, then figure out the correct offsets from tile-edges so that I can figure out where in that tile the entity is standing.
- Drag all of the height information out of the tiles and into an Array (Or ArrayList) so that I can do an easy range check across the list from the left and right position of the entity (This would still require a check to the Tile list to get the slope information though).
- Compute height-range sets for the floor (IE- From i to j, the height is ‘y’) and iterate through them to find where the player is. (Just like 2, it’d require the check to the Tile list).
1 and 2 are my favorite options, with 1 coming out ahead, except that it requires quite a few more math ops and method calls than 2 would. Might be premature optimization there, but…
Then the question of whether an array or an ArrayList would be more suitable for these would be appreciated. The number of elements in a floor is going to be a static number that won’t be changing (In option 1 it’d be around a max of 100 with an average of 20-30, while option 2 would require the height list to be much larger).
If anyone has any other suggestions for how to accomplish this I’m all ears.