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.

Code Snippets / DBC Bouncing Cube Using Motion Physics Equations

Author
Message
Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 8th Oct 2006 11:09 Edited at: 12th Oct 2006 11:57
[EDIT]

JosephB caught an error in my original distance function where I mistyped the variable time (not defined integer) where I should've had time# (defined float). I changed the snippet a bit and added another distance function. This was actually the formula that the sim was using due to my typo, so I thought I would include it and use it instead of the original. This is distance_2(). The difference between the functions distance_1() and distance_2() is that distance_1 accounts for an acceleration or deceleration. In the sim, because I am accounting for gravity with fvel#, if I use distance_1, gravity would be applied TWICE to the cube which would just be too much!

Feel free to experiment and use either function in the sim to see what results work best. If you use distance_1(), you will have to adjust the bounce loop to test for the y# distance differently.

[orig]
I was answering a question on another forum and wrote up a script of a sphere that bounced up and down using gravity. I looked at the code, changed the sphere to a cube, added a few random elements and came up with what I think is a pretty interesting looking cube that bounces around and spins and such.

I also included a bunch of physics functions at the end that are basically inversions of each other that someone might find useful.

If you use any of the code in your programs, just credit me. Hope it's interesting and useful.




[EDIT]
A couple of people asked me how this works, I'll try to explain it.

First thing, I set up the display and seed the random generator with the timer. This is so that when I use the rnd() function later, it likely won't be the same value everytime I run the program.

Next, I make a matrix, 1000,1000 untis across and up, and 50x50 tiles. Nothing too spectacular.

Next I create the cube that I'm going to bounce around:



25 seems to be the size from the center of the cube out so when I position it, I'm positioning it in the middle of the grid and I want to account for the 25 units on the y axis so the cube isn't cut in half by the grid. I know the matrix is 1000x1000 so therefore to place the cube in the middle, I have to set x and z to 500. I get the ground hieght of the matrix at that location and add 25 to it. This will be the y position of the cube so that the cube is in the middle and right on top of the grid.

Next I position the camera lined up with the cube on the x, 50 units up on the y, and 400 units on the z (100 units away from the cube so that we can see it).

Now we get into the fun stuff! We want to treat the cube somewhat like it's in the real world so I want to assign it a mass. For this example, the mass becomes extremely important because it influences the affects of any forces I apply to it.

The idea of this simulation is to apply a random upward force to the cube - a "popping" force if you will. This will be sort of a bounce, but it will be different everytime the cube lands.



The next series of variables that follow mass in this section are:

Sideways = this is a random X value between -2 and 2. This will control which sideways direction the cube will go in once the popping force is applied.

Frontways = this is a random Z value between -1 and 1. This controls how the cube moves into the foreground or background.

Force# = just a random number (force) between 0 and 500 that tells how much "umph" to give the cube.

Accel# = this is where I use one of the acceleration functions to get the acceleration of the cube when it is launched. I chose the specific function accel_2() because that uses force# and mass# to find the acceleration of the object, and I have those so accel# can be solved for.

Ivel# = initial velocity of the cube. I use vel_2() because that function contains the elements I need to solve for ivel#. Since it is assumed that the cube has no velocity before it is launched, and we are assuming an instantaneous velocity after the force is applied we have a starting velocity of 0, and acceleration of accel#, and a time# we'll set at 1 for instantaneous launch - thus the equation looks like ivel#=vel_2(0.0,accel#,1.0)

Fvel# = this is the velocity of the cube as it is affected by gravity. At the initial launch, this is 0 because we only care about ivel# when we start.

Time# = time. Set to zero to refresh the sim at every bounce

The bounce loop:



I start off with checking fvel# against falling below zero. In the real world, this would happen when the object got as high as it could and then started falling. But because I'm using a displacement equation later, if fvel# falls below zero then the object has dropped back to the height from which it launched.

We need to keep track of time so that everything can change (velocity and distance). I didn't want time# to be the loop in which everything happens but I wanted time# to change, so I increment it within the main While Loop so that time# is actually influenced by where the object is, as opposed to the other way around.

The next two functions really do all the work. I'm making gravity -9.8 which represents the acceleration rate towards -y or downward in this example.

fvel# will change as time# changes. fvel#=vel_2(ivel#,gravity#,time#) takes ivel (the launch velocity) and calculates it against gravity as time changes. Since gravity is in the opposite direction of ivel#, as time increases, gravity (-9.8) will influence the acceleration in the opposite direction... this means fvel# will continue to slow down until it stops, then it will decrease at the rate of gravity. In the sim, you see the cube rising and slowing down.

distance# calculates how far the cube is traveling from it's launch point along the y axis. It is greatly influnced by fvel#. Of course gravity and time# play important roles also. If fvel# didn't change, the cube would just keep rising, but at a fixed speed determined by gravity. distance# is the real work horse and is the value I add to the cubes original y position to actually make it move. Very much like fvel#, distance# value inceases as the cube goes up, but once the cube reaches it's peak height, the influence of gravity is too much and the cube will fall at a velocity realtive to gravity. This displacement with each time iteration increase, so we see the cube speed up as it get's closer to the ground.

All of the x#, z#, and rot# calculations are based on the sideways and frontways variables I created earlier. These are just added to or subtracted from, or rotated within, x#, z# y# - to make the cube move side to side, from to back, and to spin. The numbers I chose don't have any real significance, they are just low, and a little different from each other to try and make the tumbling look a little more real.

Essentially the whole simulation is brought together by position the object according to the new values calculated from distance#, sideways, and frontways - then rotating the object with rot#. I want to keep the camera at a z distance 100 units less than the cube so the camera follows the cube around while it bounces but never changes it's orientation.

I put a couple of limits on the x# and z# directions based on the size of the matrix so the cube doesn't go out of bounds.

I actually only use three of the motion equations. The rest I included so anyone could use them as variations on the methods I used in this example.

Enjoy your day.
Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 8th Oct 2006 13:57
That's pretty damn cool.

Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 8th Oct 2006 21:51
Thanks!

I edited the above code and changed a line

x#=x#+sideways

to

x#=x#-sideways

it helps make the cube tumble in the direction it's moving - looks a little bit better.

Enjoy your day.
Mr Kohlenstoff
17
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Germany
Posted: 9th Oct 2006 12:51
Well... that looks cool, even if not very realistic.
Anyway.. for this little peace of code, really good work!

Visit the DBPro - Speed up your game-Thread. http://forum.thegamecreators.com/?m=forum_view&t=88661&b=1
Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 9th Oct 2006 14:21
Thank you!

Try changing the gravity (the values that are currently -9.8) to -15.0 ...more "realistic"?

Enjoy your day.
JosephB
17
Years of Service
User Offline
Joined: 12th Sep 2006
Location:
Posted: 9th Oct 2006 15:42
I had to change your last function:

function distance(ivelocity#,time#,acceleration#)
s#=(ivelocity#*time#)+(acceleration#*(time^2))/2
endfunction s#

to (added two parentheses):

function distance(ivelocity#,time#,acceleration#)
s#=((ivelocity#*time#)+(acceleration#*(time^2)))/2
endfunction s#
Gamer Making
17
Years of Service
User Offline
Joined: 20th Sep 2006
Location: sitting at the comp programming
Posted: 9th Oct 2006 22:47
How do you guys learn so much about code and programming? I never programmed before and it is so hard Latch! Can you write a tutorial for me?

Minh Tran
Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 10th Oct 2006 02:39 Edited at: 10th Oct 2006 02:46
@ JosephB

I don't think that function should be changed. I think that function should be ok as it was. If you wanted to add additional parentheses, they would go here:

function distance(ivelocity#,time#,acceleration#)
s#=(ivelocity#*time#)+((acceleration#*(time^2))/2)
endfunction s#

It's an equation for displacement of an object:

s = ut + 1/2 at^2

where
s = displacement (distance)
u = initial velocity
a = acceleration
t = time

I didn't intend on dividing the whole equation by 2 - that looks like how you modified it. I'm only dividing at^2 by 2. It's based on constant acceleration. On a graph, the velocity increases proportionately over time (a triangle. area = 1/2 base x height = 1/2(vf-vi)t = 1/2(acceleration*time)t = 1/2at^2)

Thanks for keeping me on my toes though!

@Gamer Making
If you had a specific topic in mind, I might be able to. I'm better at answering questions than writing tutorials out of the blue. I wouldn't write the tutorial in this thread, I don't think it's the right place. But here's a great post to start with:

http://forum.thegamecreators.com/?m=forum_view&t=89180&b=7

Enjoy your day.
JosephB
17
Years of Service
User Offline
Joined: 12th Sep 2006
Location:
Posted: 10th Oct 2006 18:55
@Latch

Thanks.

The modification keeps the cube in the camera's view. When I run it without changing the function then s(distance)=16,106,127,360 almost instantaneously.
Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 11th Oct 2006 03:18
@JosephB

Wow, I don't know why that is. I copied the code snippet from this forum back to my computer to see if maybe I made a typo or something, but it runs just as I expected it to. The highest distance I see when I specifically change the force# to 500 (maximum in this example) is 63.something

I don't have an explanation for what you are experiencing.

Enjoy your day.
JosephB
17
Years of Service
User Offline
Joined: 12th Sep 2006
Location:
Posted: 11th Oct 2006 19:04
@Latch

I found the problem. Your snippet includes the following:

s#=(ivelocity#*time#)+(acceleration#*(time^2))/2

I had to add # to the second instance of "time" as follows:

s#=(ivelocity#*time#)+(acceleration#*(time#^2))/2

Very nice functions.
Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 12th Oct 2006 02:05
@JosephB

Good Eyes! Thanks for spotting that! (But I really hate making mistakes!) Now that the sim has the correct calculations, it behaves badly on my computer! The calculations are doing what they are supposed to, but now distance is a lot smaller! I think I'll have to rewrite the sim to make it behave how I want (more properly)!

It was "working" before because it was just calculating a smaller fvel# with every iteration then plugging that into distance# I guess distance# wasn't even accounting for gravity since it was using the incorrect variable time=0 as opposed to time#=the proper time. That must by why you were getting those huge distance values. I wonder why the cube didn't bounce very far away on mine?


Thanks for catching that!

Enjoy your day.
Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 12th Oct 2006 19:49
Another way to affect the cube by gravity is by calculating the weight (mass*gravity), and using that weight as a force. Then you get all the forces applied on the cube and resolve them (add them all up) and use that value as your actual force that affects the cube.

StevetS
19
Years of Service
User Offline
Joined: 19th May 2004
Location:
Posted: 13th Oct 2006 00:18
Very cool snippet!

Jrock
18
Years of Service
User Offline
Joined: 20th Feb 2006
Location: Riven
Posted: 13th Oct 2006 00:51 Edited at: 13th Oct 2006 00:51
Nice! Just covered this in science class, pretty cool!

Ok the mods got rid of my sig...
Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 13th Oct 2006 01:45
@StevetS
Thank you

@Jrock
Thank you

@Zotoaster
Right on. That's actually being done indirectly in the sim. Since mass# and gravity# are constants, the weight force is being calculated by having gravity# set to negative. This "automagically" totals the positive and negative forces in the up and down directions. Since the cube is only pushed up once, we're essentially only left with weight after each launch. I cheat a little bit with the initial velocity. I have the cube accelerate for a whole second. When the loop against gravity starts, I have it decelerating over .05 seconds per loop.

If I set the initial force to 98, and set the initial time to .05, the ivel# equation would look like ivel#=vel_2(0.0,accel#,.05) and the cube would be held in equilibrium because the forces of push and weight would be the same. Thus, this is the total of the forces applied on the cube (in the up and down direction).

Enjoy your day.
Gamers for sale
18
Years of Service
User Offline
Joined: 19th Nov 2005
Location: Some where beneath the elements
Posted: 1st Jan 2007 06:14
Wow, amazing stuff latch. I was going thought your threads and I have to say you have really gotten a lot done since the last time I was on the forums.

This will be very useful for a physics engine!

I have one wave example that I am trying to figure out. Maybe you could help with it.

I just learned a constant growth equation and encorperated the use of sine and I made a wave.

Just post on my thread - "How could db communicate to another program with the use of data files."

Your signature has been erased by a mod because it's larger than 600x120
Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 2nd Jan 2007 05:39
@GFS
Thaks!

Post your question about the wave in the thread you mentioned and maybe I can help.

Enjoy your day.

Login to post a reply

Server time is: 2024-05-07 18:10:15
Your offset time is: 2024-05-07 18:10:15