412lop 2022-09-28 01:49
I don’t know how to do gravity can someone please help me?
nathanielbabiak 2022-09-28 03:30 (Edited)
Gravity is really complicated. Your upload already has a variable tracking position, but you need to add variables to track velocity and you need to model acceleration too. This webpage is really janky, but the first table is a solid starting point.
Also, once you "model" gravity, any object will fall off the bottom of the screen! You'll need to "model" collisions too.
nathanielbabiak 2022-09-28 03:32 (Edited)
That webpage above is such crap as far as explanations go, but seriously that table is the best one you'll find.
For a better explanation, check out this heading, or honestly the whole webpage.
was8bit 2022-09-28 14:45
Here is your program redone... its only one method but it seems to work ok :)
was8bit 2022-09-28 14:51
There are 3 basic tricks to get gravity to work right...
1) no gravity effect when standing on a solid surface
2) when applying gravity in a free fall, accelleration is constantly being increased by the force of gravity
3) restrict downward movement to 1 pixel at a time to avoid
... getting player stuck inside a solid
... gettin a player stuck floating above a solid
was8bit 2022-09-28 14:53
Jumping and landing is another issue, i added jumping with the (A) button, but like gravity, there is more than one approach.. i used my method ;)
... feel free to experiment :)
was8bit 2022-09-28 14:58
I did allow a bug to remain... if you test my code you will find, when falling, it is possible to get stuck if you slam yourself to the right into the higher floor... you can easily get loose by tapping left...
... test each change to the code carefully to see if it behaves as expected, or to see why it isnt... thats how you learn and discover ;)
SP4CEBAR 2022-09-30 11:16 (Edited)
I usually implement gravity like this:
FX= all horizontal forces added together FY= all vertical forces added together
This is the net force (vector)
FX= 0.01*(LEFT(0)-RIGHT(0)) FY= 0.01*(UP(0)-DOWN(0)) + 0.01
'the 0.01 is gravity,
To set a max velocity you can add friction as a constant when the ground is touched, and drag depending on the velocity, watch out for oscillations, especially with realistic velocity-squared drag
This doesn't have to be an accurate simulation, so you can just tweak the intensity of the forces until it feels about right
According to one of Newton's laws: acceleration is the net force divided by the mass, the mass doesn't change so I usually ignore it and say (F=A)
@was8bit: our acceleration due to gravity doesn't change, in the real world it's always equal to about 9.81 M/S^2, the thing that continuously changes is the velocity
In recursive physics simulations it's realistic to write:
M=10 'Mass of the vehicle AX=FX*M AY=FY*M DT=1/60 'Delta time is 1/60 frames ADD VX,AX*DT ADD VY,AY*DT
But I usually write:
ADD VX,FX ADD VY,FY
it's realistic to write:
ADD PX,VX*DT ADD PY,VY*DT
But I usually write:
ADD PX,VX ADD PY,VY
And when collision is detected, I find it convenient to set the velocity (VX or VY or both) to zero
was8bit 2022-09-30 15:37
I reckon its more correct to say that the current speed is constantly changing ;)
The point being is that the actual coding isnt as straight forward as the math... for example, i have never found nor can figure out how to code a billiards game.. i understand the math, but cannot figure out the code...
nathanielbabiak 2022-09-30 21:08
I think that's the second time I've seen you mention billiards math... I'll think on it and post soon...
was8bit 2022-10-01 02:34
I tend to get “sticky” results.. in a worse case scenario I’ve seen 3 orbit each other :/
It’s mostly that the math gets so layered ... my mind starts to draw a blank when you do a break with the cue ball and try to apply the impact on all of them at the same time :/
When it’s just 2 balls colliding, my math works great... 3 or more :P
was8bit 2022-10-01 02:34
Any help is greatly appreciated :)
nathanielbabiak 2022-10-01 04:09 (Edited)
I kind'a figured that was the issue. It's P=NP hard. You have to develop a scheduler that evaluates each ball's collision with any other (...after developing the two-ball scenario, reversing the velocity along the plane of contact, yada yada).
The paradox is that triplets of balls don't collide simultaneously in the real world. In the real world, (reasonably modeled) collisions are instantaneous. In software, we're queuing all collisions that occur over a 1/60th of a second timespan, and evaluating them together. The trick is to back-calculate the precise moment within the past 1/60th of a second that the collision actually occurred, and process all collisions in that order.
Initially, my mind went to this (schizophrenic?) explanation... If a triplet of balls A, B, and C collides simultaneously, two evaluations would occur. The first evaluation would be between A and B, and flip their velocities. But, then the second evaluation would flip C and one of A or B. So one of A or B would flip *twice*, and then one of B or A would overlap ball C.
was8bit 2022-10-01 05:04
Yea, turns my brains into scrambled eggs ;)
SP4CEBAR 2022-10-02 07:27 (Edited)
@nathanielbabiak, -I don't think you need a scheduler-, I think when collision is detected on the main ball, you can process all collisions in the same frame: by for example looping through all the balls that touch the main ball, and then the each of those balls will do the same (kind of like one of the flood filling algorithms), and then multiple balls will be rolling, and...
nvm. the scheduler is necessary
Edit: or not: a loop through all balls should work too