Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / How to implement global gravity that affects ALL objects?

Author
Message
Omega gamer 89
16
Years of Service
User Offline
Joined: 10th Sep 2007
Location: Pittsburgh, PA
Posted: 13th Sep 2020 00:47 Edited at: 13th Sep 2020 06:50
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




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:



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.
Omega gamer 89
16
Years of Service
User Offline
Joined: 10th Sep 2007
Location: Pittsburgh, PA
Posted: 13th Sep 2020 23:44
Update: i got something set up that seems to work decently well so far, but Ive yet to test it out with large numbers of object.

What I did was to create a user-defined Type called "GravityObjects" with fields for object number, X Y and Z position,an integer to track how long the object has been falling for, and a float to track fall speed, and another integer for the player jumping. Then I created an Array called "Gravity list" and did "Dim Gravity list as GravityObjects" so that each entry in the array would have those fields I mentioned earlier. Then I created a function called "GravityListAdd" that I run every time I create a new object that I want to be affected by gravity, and this function basically adds a new entry to the array and stores the object's information in the custom type fields listed above.



Then, I have a function simply called "Gravity()" That I run in the main loop.


This allows me to solve the problem of being to make objects accelerate as they fall, rather than only at a set rate, and also allows me to only check objects that Ive specified I want to be affected by gravity, rather than having to check EVERY object. However, this still leaves the issue of false positives from an object colliding with something next to it, rather than below it.

Still open to any ideas on other potential ways to handle gravity, or to improve the method Ive come up with so far.
Code Maker
8
Years of Service
User Offline
Joined: 4th Dec 2015
Location:
Posted: 14th Sep 2020 21:16
Why not just use a physic engine like Dark Physic or the Newton plugins that will take care of all the physics
in your game the correct way, and it handles collision, and many other things if needed.
Omega gamer 89
16
Years of Service
User Offline
Joined: 10th Sep 2007
Location: Pittsburgh, PA
Posted: 15th Sep 2020 01:44 Edited at: 15th Sep 2020 22:05
Quote: "Why not just use a physic engine like Dark Physic or the Newton plugins that will take care of all the physics"


Honestly, Im just not familiar with them at all, and the help file for dark physics is... less than helpful. Its not organized into sections or anything; its just one long, unbroken list of ALL the commands for darkphysics, sorted alphabetically rather than by what kind of work they do. Im sure theres normally a proper help file with examples and tutorials and such, but I cant find anything. I wouldnt even know where to begin, and the only way to really figure it out would be to read the help page for every single command 1 by 1 in order to figure out which ones to use.

That being said, I did get the rest of the problems with this function sorted out. I realized I was an idiot for using object collision rather than raycasting.

I simply changed the collision check to a doing a raycast, and checking if that would result in a collision. If no, it falls the full length it would normally fall. If yes, it measures the distance between its current Y coord, and the Y coord of the collision, and positions it directly above that.

However, now Im having a new problem. So far Ive been testing all this with just cubes and spheres as and such, but I wanted to see how everything looked with an actual model, so I loaded one in, in place of the box Id been using for the player character. Turns out, that objects created through "make object cube" and such have their coordinates based on the center point of their shape, whereas the object I loaded in through "load object" seem to have its coords based on the lowest point of their model. As a result, the humanoid model I loaded in basically floats several units above the ground.

I adjusted this using the offset limb command, and that DID get it to position correctly on the ground, but now it vanishes every time the camera gets too low. Im using a mouse-controlled third-person camera, and since offsetting the limb, if the camera gets below a certain point, the player model just disappears. Everything still functions like its there, but I just cant SEE it.
Code Maker
8
Years of Service
User Offline
Joined: 4th Dec 2015
Location:
Posted: 15th Sep 2020 20:55
Yes raycasting is very usefull for positioning objects or bullet calculations.

As for your player height positioning problem the right way to fix it would be
to move the models zero point on the y axis in a modeling program and
reexport it.

A workaround could be to add the y offset in your positioning code like this



where the -25 is the offset for the model, this way it will not influence with
your camera control code.
Jason487
User Banned
Posted: 26th Sep 2020 06:37
I simply changed the collision check to a doing a raycast, and checking if that would result in a collision. If no, it falls the full length it would normally fall. If yes, it measures the distance between its current Y coord, and the Y coord of the collision, and positions it directly above that.

However, now Im having a new problem. So far Ive been testing all this with just cubes and spheres as and such, but I wanted to see how everything looked with an actual model, so I loaded one in, in place of the box Id been using for the player character. Turns out, that objects created through "make object cube" and such have their coordinates based on the center point of their shape, whereas the object I loaded in through "load object" seem to have its coords based on the lowest point of their model. As a result, the humanoid model I loaded in basically floats several units above the ground.

I adjusted this using the offset limb command, and that DID get it to position correctly on the ground, but now it vanishes every time the camera gets too low. Im using a mouse-controlled third-person camera, and since offsetting the limb, if the camera gets below a certain point, the player model just disappears. Everything still functions like its there, but I just cant SEE it.

Login to post a reply

Server time is: 2024-04-23 11:20:26
Your offset time is: 2024-04-23 11:20:26