So as the title says, Im having trouble coming up with a good way to have gravity/falling affect all objects, instead of just the one object that is my player character.
I have it worked out for the player, but cant think of a good way to implement it for enemies, NPCs, etc.
For the player character, I basically have this written in my PlayerMovement() function. The following is psuedocode, but it gets the idea across
if upkey() then inc playerPosX#,1 `we change the playerPos variables based on arrow key inputs
if downkey() then dec playerPosX#,1
if leftkey() then dec playerPosZ#,1
if rightkey() then inc playerPosZ#,1
if object collision(PlayerObject,0) = 0 then falling = 1 `if the player object isnt colliding with anything, we set Falling to 1
if object collision(PlayerObject,0) then falling = 0 : fallSpeed # = 0 `if the player IS colliding with something, we set falling to zero
if Falling => 1 `if falling is equal to or greater than 1
if falling =< 20 then inc FallSpeed#,1 `and if its less than 21, we increase fallspeed
PlayerPosY# = PlayerPosY# - FallSpeed# `then we update the players Y position by the amount off fallspeed
inc falling,1 `and we increase the value of falling by 1. This way we accelerate for the first 20 frames of the fall, then fall at a constant rate after
endif
Position object PlayerObject,PlayerPosX#,PlayerPosY#,PlayerPosZ# `position the player at the playerposition values
Theres more to the player movement function than that, but thats basically the relevant part.
So yeah, I have falling and gravity sorted out for the player, but I cant think of a good way to apply it to enemies, NPCs, etc.
The best idea Ive come up with would be to run a FOR-NEXT loop, something like this:
MaxObject = number of objects created so far `this would just be updated every time I create an object, so it would be equal to the total number of objects in the level
For X = 1 to MaxObject `we run this loop a number of times equal to the total number of objects created
if object exist(X) `if an object with object number of X exists. This is only here to prevent "object does not exist" errors
if object collision(X,0) = 0 then move object down X,1 `if that object is not colliding with any othger object, we move it down by 1, and this repeats each loop, until it eventually IS colliding with something
endif
next
This would
work, but theres several issues.
For one, its inefficient. We're checking EVERY single object in existence EVERY loop. This would probably get very slow with larger levels.
For another, it could return false positives. If an object is falling, then collides with the SIDE of an object, this method would still make it stop falling as if it was standing on top of something. Its basically like if you jumped off a skyscraper, but then you stopped falling completely if you touched the wall with your finger. Or like if you touched your hand to the ceiling, suddenly gravity no longer applied and you could lift your feet off the ground without falling because you are technically colliding with something, even if its not something
below you.
For a third thing, everything would fall at a constant rate, rather than accelerating as it fell. I can do this for the player because the player is only one object, and so I only need 1 variable to count how long its been falling. But since any given level could have ANY number of enemies, theres no way I can think of to have such a variable for each and every object that could fall.
Is there a known method for implementing global gravity that gets around these issues? Is there maybe a plugin or a sort of standard method, like how theres the method of timer-based movement for handling frame rates and such. If it matters, what Im making is mostly a third-person shooter game, so I would need the player, enemies, NPCs, and certain objects to be affected by gravity.