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.

Dark Physics & Dark A.I. & Dark Dynamix / Character controller - Jumping with Smooth Gravity

Author
Message
BearCDP
14
Years of Service
User Offline
Joined: 7th Sep 2009
Location: NYC
Posted: 20th Sep 2009 19:39 Edited at: 13th Oct 2009 03:35
Update: Go here: http://forum.thegamecreators.com/?m=forum_view&t=159101&b=8

I did a search on here for jumping implementations, and at least from what I saw, not many people have figured out a solid way to smoothly jump with a character controller. If I'm wrong, let me know--I wanna read their code!. Either they use Sparky, DBP's own collision, or create their characters as dynamic rigid bodies and apply forces to them.

I like the last solution--having a character that interacts a lot with the physics of the environment can be a lot of fun in a game. However, sometimes you don't really want that. When reading the Information CHM about DarkPhysics, it seemed as though the whole advantage of a character controller was that it's detached from standard physics calculation, allowing more stable and controllable player movement. Personally, I'm trying to create a game in GDK with a style similar to N64 Zelda titles right now, and the character control in that game with regards to jumping has a sort of rigidity that makes the whole jumping experience seem more dramatic. Therefore, I was a little reluctant to leave my jumping control absolutely in the hands of DarkPhysics' dynamic rigid bodies.

So I learned from reading various morose threads that Character Controller gravity and jumping can be a little difficult to get right. Thankfully I found some invaluable posts. One was from Jammy, who posted about the "PHY MOVE CHARACTER CONTROLLER <objId>, 0.1 : PHY MOVE CHARACTER CONTROLLER <objId>, -0.1" trick after setting up displacement with "PHY SET CHARACTER CONTROLLER <objID>, 0, <backward#>". Also, I kept trying to use "PHY RAY CAST" when apparently you need to use "temp = PHY RAY CAST ALL SHAPES", and you can't specify whether you want static, or dynamic rigid bodies. (forgot who posted that one, couldn't find the thread, but thank you whoever you are!)

With the information above, it wasn't too terribly painful to write a smooth jumping implementation with smooth gravity. It's basically just high school physics, using the projectile equation.

y = 1/2*A*t + initialVelocity*t + initialY
dy/dx [velocity] = a*t + initialVelocity

The code is posted below, and uses a character model that should come default with a DBPro installation. If you have fixed the orientation of any of your sample DBPro models in an 3d editor, then go to the "setupScene()" function and comment out the XRotate and Fix Object Pivot statements. I tried to comment thoroughly, not line-per-line but giving overviews of how blocks of code work and why they're there. You'll notice there's sort of a double-check on the collision with the ground. The first part is a ray cast, the second part checks to see if the Y position has changed since the ray cast, and only then will enable the jumping = TRUE state. This will probably be problematic for capsule controllers--I have yet to test that. Why don't I just get rid of the ray cast check if I'm already checking Y positions? Well, it seemed safer to me, and now it may be easier to implement the "cliff-hanger" state; if the player is almost close enough to the ledge to fall then he will fall, but grab onto the edge, and the player must press a button to pull themselves up. I'll have to see. I also may be completely wrong on this, can anyone shed light?

If anybody finds any glitches or a faster/better way to do things, please post! It's been a while since I've written anything in DB.

Oh, and what is the deal with "Phy Set Character Controller Displacement <objId>, <x#>, <y#>, <z#>"? Is it just the POSITION OBJECT command for DarkPhysics?

Screenshot (because who doesn't love them?):


Code here:
BearCDP
14
Years of Service
User Offline
Joined: 7th Sep 2009
Location: NYC
Posted: 20th Sep 2009 19:44
Screenshot

Attachments

Login to view attachments
Slooper
21
Years of Service
User Offline
Joined: 13th Feb 2003
Location: Sweden
Posted: 20th Sep 2009 19:49
This could be what i have searched for, will take an look


You never fail, only make mistakes.
BearCDP
14
Years of Service
User Offline
Joined: 7th Sep 2009
Location: NYC
Posted: 20th Sep 2009 19:52
Glad it may possibly be of help

Forgot to mention, you can change the acceleration of gravity with [ and ], and the player's jump velocity with - and =.

If gravity is higher, then your jump will be shorter, if it's lower, jump will be longer. For jump velocity, the larger it is, the higher the player will jump.
Slooper
21
Years of Service
User Offline
Joined: 13th Feb 2003
Location: Sweden
Posted: 20th Sep 2009 20:07
sent you an email


You never fail, only make mistakes.
BearCDP
14
Years of Service
User Offline
Joined: 7th Sep 2009
Location: NYC
Posted: 20th Sep 2009 22:27
For anyone having issues locating the model you can do one of two things.

1. Try to locate your DBPro media directory with the ColZ model in it, and under the setupScene() method, change the mediaPath$ variable and subsequent LOAD OBJECT statement to reflect your copy of DBPro's directory structure.

2. Command out everything related to loading the ColZ.x model, and instead put this statement "MAKE OBJECT CUBE theColonel, 10)". Then, go up to the "Adjustable Values" block of global variables, and change minimumRayCastDistance# to "15.2".

This is a quick and dirty fix, in a little bit I'll put out a revised version of the code that uses a cube for the model instead of ColZ.x, and also uses timers.
HowDo
21
Years of Service
User Offline
Joined: 28th Nov 2002
Location: United Kingdom
Posted: 21st Sep 2009 11:11 Edited at: 21st Sep 2009 11:12
Well done BearCDP2, very good way of doing it, who knows what else you'll come with if you can crack this hard one.


BearCDP2 do know if you using capsule gets the same results?

I know form reading the SDK that they like to capsule more than box.

Dark Physics makes any hot drink go cold.
BearCDP
14
Years of Service
User Offline
Joined: 7th Sep 2009
Location: NYC
Posted: 22nd Sep 2009 00:49
Capsules are proving to be a little complicated. It would be wonderfully easy if there were collision commands that responded to Character Controller activity.

Capsule would indeed be ideal, movement over stairs are much more smooth when using capsules instead of boxes.

The problem is, I took the path of least resistance and calculation, and checked the change in Y position to determine if a player has slipped off the ledge of not. I think I might be able to get it somewhat working so long as you don't allow the player to step into emptiness. I'll post as soon as I can with updated code.

Todo for next time:
Use MAKE OBJECT BOX instead of Colonel Z (as cool as he is)
Capsules!

Soon to follow after that:
Timing
BearCDP
14
Years of Service
User Offline
Joined: 7th Sep 2009
Location: NYC
Posted: 22nd Sep 2009 02:18
Here's version 1.0148375839 - (1.0148375839 mod 1.01)

Almost there. Set the "controllerType" variable to either BOX_CC or CAPSULE_CC to see how it works with both types of controllers. I kept the same implementation for box controllers in there because it's fast and doesn't need to be complicated, but the character controller jump/fall check is something else entirely.

I'm moving toward a distance between [x,z]-coordinate-when-raycast-missed-ground and radius check. This should make the capsule code more efficient and aesthetically pleasing. However, the code below sorta works for capsules for anyone interested.

bandM
17
Years of Service
User Offline
Joined: 27th Sep 2006
Location: Home
Posted: 22nd Sep 2009 16:45
That's great and works like a charm. Good work but i have to ask something. When i suddenly release space key sometimes the character controller jumps a little bit but it can't reach maximum height level. How may i solve that issue?
BearCDP
14
Years of Service
User Offline
Joined: 7th Sep 2009
Location: NYC
Posted: 22nd Sep 2009 17:17
Thank you bandM, I very much appreciate your reply.

Are you using your own models? I've found that it will do that if the minimumRaycastDistance# value is set a little too low. Usually, you will need to set it to the height of your character controller plus about 0.1 to 0.5 depending on how large your model/controller is.

I've noticed that DarkPhysics isn't perfectly accurate when creating rigid character controllers and performing ray casts. If you have a cube with a height of 10.0 and a corresponding character controller with height 10.0, then the ray cast distance actually ends up being 10.00009991 or something like that.

So if your ray cast distance reports 10.00009991, and your minimumRaycastDistance# is set to 10, then there will be a period for 0.00009991 units where your character is under the control of the default DarkPhysics gravity. During this time, if the spacebar is not being pressed to encourage more upward motion from my code, then checkFallOrJump() will think that you've hit the ground already--even though there's a still a small amount of space between the character's feet and the ground--and will thus stop updating gravity.

Your safest bet is to make sure that minimumRaycastDistance# is always larger than the height of your model, and in fact the larger you make minimumRaycastDistance#, the more responsive your character will be.
ie: Hold down space with a low minimumRaycastDistance# and there may be a small delay between jumps; then hold down space with a higher minimumRaycastDistdance# and the character will happily bob up and down continuously.

Of course, set it too high and it starts looking a little ridiculous



I'm still clobbering away at capsule controllers. I realized that the radius check won't work. How many times have you played a game and just danced around the very edge of cliffs and other ledges just for kicks? That totally killed my algorithm when I did that

Next thing I'm going to try is a vector check rather than a simple radius check, though I don't foresee that going well.
Another idea is, once you're in that limbo land of sticking to the side of the ground, to put a slight push on the player in the direction of he last moved. This could be defined in a function called cliffHangerResponse(), and then you guys could overwrite that with whatever you wanted if you don't like my implementation. A useful thing to do would probably be to put the player in the state of "Oh noes, I'm hanging off a cliff, push button [X] to make me pull myself up!"

Or better yet, set a timer to see how long the player is in that limbo land, and if it exceeds a certain amount of time (meaning that they're lollygagging around the edge, and haven't ran head on into space) then you could run the cliffHanger routine, otherwise just use default behavior.
sindore
19
Years of Service
User Offline
Joined: 2nd Jul 2004
Location: Bedfordshire, UK
Posted: 22nd Sep 2009 21:26
I can't get this to compile at all, I've tested dbp with other programs but it seem to be the code, is there a plugin that I need besides dp to run this?

soul sucking devils, twisted body of the damed, slivering slim drips from every poor, sin licking at your ears, and the smell stinging your eyes, and if you don't like it, get out of my kitchen!
BearCDP
14
Years of Service
User Offline
Joined: 7th Sep 2009
Location: NYC
Posted: 22nd Sep 2009 21:29 Edited at: 22nd Sep 2009 21:31
Shouldn't need anything other than DBP and DP. Have you updated DP to the latest version? My code uses of commands that are only available in the update, like PHY SET CHARACTER CONTROLLER DISPLACEMENT. Let me know, if you are indeed updated, then I've got some hardcore debugging to do.

Too bad my DBP debugger's broken
bandM
17
Years of Service
User Offline
Joined: 27th Sep 2006
Location: Home
Posted: 23rd Sep 2009 00:12
@BearCDP2
Solved it!! Thank you I've dealt with that problem on Newton Pyhsics too. But it was a long time ago so i forgot(what a shame ) Thanks a lot, your algorithm now works flawless.

Here's the code
HowDo
21
Years of Service
User Offline
Joined: 28th Nov 2002
Location: United Kingdom
Posted: 23rd Sep 2009 12:57
BearCDP2 when you use the capsule system is done diffrent to how you think it should be in the SDK it like two sphere sitting one on top the other, plus all the values need to be half.

so if your model was say 20 high you would use 10 if it was 15 wide you would use 12.5.

Hope this info may be of ues to you.

Dark Physics makes any hot drink go cold.
BearCDP
14
Years of Service
User Offline
Joined: 7th Sep 2009
Location: NYC
Posted: 24th Sep 2009 06:54
That is indeed very helpful, thank you HowDo!

Still working on that capsule's problem, it makes sense that it would be two spheres. There's a strange problem where if you move very slowly off the ledge, then the character will sort of stick to the edge, even though by all reason they should have fallen off a long time ago. I'm currently trying to put in a timer to determine if they're *likely* just sticking on the edge, and if so figures out the direction they last moved away from the wall and moves in that direction until they're out of the "cliffhanger zone".

Is anyone interested in a DBPro and GDK version that is #includeable?
HowDo
21
Years of Service
User Offline
Joined: 28th Nov 2002
Location: United Kingdom
Posted: 24th Sep 2009 11:36
BearCDP2 if you know DBPro, GDK and C++ then you may find looking at the SDK for Nvidia Physx very useful as it has a lot of demos and theres one that is a character controller using capsules, plus have you found and down load their newest physX visual debugger, found here link.

BearCDP2 if you can do the same in GDK you will be very popular in the GDK section as they always looking for some who's worked out how to do things.

Dark Physics makes any hot drink go cold.
BearCDP
14
Years of Service
User Offline
Joined: 7th Sep 2009
Location: NYC
Posted: 12th Oct 2009 07:20
Slooper
21
Years of Service
User Offline
Joined: 13th Feb 2003
Location: Sweden
Posted: 12th Oct 2009 21:41 Edited at: 12th Oct 2009 22:58
Think you copied wrong adress, here it is http://forum.thegamecreators.com/?m=forum_view&t=159101&b=8


You never fail, only make mistakes.

Login to post a reply

Server time is: 2024-03-29 00:06:20
Your offset time is: 2024-03-29 00:06:20