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.

Author
Message
D0rk
15
Years of Service
User Offline
Joined: 28th Apr 2011
Location:
Posted: 11th Jul 2011 17:40
Hello there I've been using sparky's collision for my map collision. It all works fine, except that I can't seem to make gravity collision. I can make the gravity code, but the collision is somehow messed.




I've cut all the non collision parts.
I hope it is understandable.
Thanks.
D0rk
15
Years of Service
User Offline
Joined: 28th Apr 2011
Location:
Posted: 11th Jul 2011 18:42
Sorry for double post I didn't know I had to wait for my post to be approved.
29 games
20
Years of Service
User Offline
Joined: 23rd Nov 2005
Location: not entirely sure
Posted: 11th Jul 2011 22:35
As far as I can tell the code you posted works. I took your subroutine and built a quick demo and the collision detections seems to be working.





Quote: "...the collision is somehow messed"


What do you mean by the collision was messed? How did you want the collision to work and what happened when you ran it to make you think it was messed?
D0rk
15
Years of Service
User Offline
Joined: 28th Apr 2011
Location:
Posted: 11th Jul 2011 23:40
Thanks for your help, 29 games.
I've created a new project and pasted your code and it does what I expected and what I call "messed".

Just add
after the keystate's to see for yourself and you'll notice that the variable ground keeps changing from 0 to 1 very fast, which causes issues.
I hope you understand my problem. Thanks.
29 games
20
Years of Service
User Offline
Joined: 23rd Nov 2005
Location: not entirely sure
Posted: 13th Jul 2011 00:26 Edited at: 13th Jul 2011 00:28
Yes, I see what you mean now and I've managed to sort this out.

It was this line:



that was causing the "flickering" of both the ground variable and the collision variable.

Essentially, after the collision, ground = 1 and the sphere is positioned so that it is no longer in contact with the cube. When the program loops round, the if statment you had stopped the sphere from falling so, for that loop, no collision was detected, so ground = zero. In the next loop of the program the sphere starts to fall, collision is detected, ground = 1 and round we go again.

The best way to think about gravity is to assume the sphere is always falling downward. The sliding collision commands will reposition the sphere if it hits the cube so that the sphere never falls through the cube, even though the sphere is always being pulled down. I hope that makes senses.


I've made changes to the sub-routine and made comments to highlight the changes.




Hope this is what you were after.
D0rk
15
Years of Service
User Offline
Joined: 28th Apr 2011
Location:
Posted: 13th Jul 2011 03:30
Thanks, 29 games.
That works, but I had already tried to remove the ground check in the gravity and it worked - the ground value wouldn't keep changing from 0 to 1. But then there comes another problem: now, as gravity is always acting (no ground check), when the sphere falls onto the green cube, it will slide down no matter how steep the ground is. In reality, you wouldn't slide in a so little steep ground. If my explanation is not understandable, just replace



with




Now you'll notice how unrealistic that looks if the sphere was to be a box or an human.
Anyway you helped me a lot already, so I decided to learn by doing and I took a peek to one of sparky's demos that shows how to do it.
I created another sub with an optimized code from the demo:



Now I just need some help for when I jump the sphere does a realistic jump and gravity acts.
Thanks.
Neuro Fuzzy
19
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 13th Jul 2011 09:10
Have you checked how the sparky's collision demos do it?


Why does blue text appear every time you are near?
D0rk
15
Years of Service
User Offline
Joined: 28th Apr 2011
Location:
Posted: 13th Jul 2011 14:56
If you look at the post above yours you'll see I have and it works. Now I would only like to replace the vx's, vy's and vz's with position/move object commands. Example:

Replace



with



But I understand there are many things that I have to change for it to work.
29 games
20
Years of Service
User Offline
Joined: 23rd Nov 2005
Location: not entirely sure
Posted: 13th Jul 2011 23:15
Quote: "In reality, you wouldn't slide in a so little steep ground"


That would depend on the coefficient of stiction and / or the coefficient of friction between the two objects, but I'm being facetious.

Quote: "Have you checked how the sparky's collision demos do it?"


This is excellent advice! And having had a look at the "Sliding demo", everything is in there so I would really check that out and stop listening to me

However:

Quote: "...as gravity is always acting... "


This is the nature of gravity, it always acts. If you look at Sparky's sliding collision demo, he sets up gravity as a constant and then uses that to change the velocity in the y direction. This is generally what I do with all my jumping code.

The general order of things to keep in mind (and this goes for all movement) is: velocity changes position, acceleration changes velocity.

Gravity essentially accelerates things downward. That's why people use the phrase "acceleration due to gravity".

In your code you are using gravity as a velocity, it directly changes the y position, and this is incorrect. It doesn't really matter but I think this adds confusion, especially if you want to do more complicated things in the future, like adding in drag effects.

So back to Sparky's demo, when the sphere (the player controlled object) hits the map, the position is set using the collision code and the y velocity is set to zero but gravity is, you guessed it, still acting (it is never set to zero).

He then adds in extra code to deal with the sphere sliding down slopes (something I've been struggling with) otherwise you can get a kind off "hopping" effect when moving down slopes.

He's also using a combination of static collision and sliding collision.

Anyway, have a look at the demo, it really does have all the answers.
D0rk
15
Years of Service
User Offline
Joined: 28th Apr 2011
Location:
Posted: 13th Jul 2011 23:40
I already had a look at the sliding demo.
Here's the full code:

Login to post a reply

Server time is: 2026-07-11 02:36:19
Your offset time is: 2026-07-11 02:36:19