Page 1 of 2

[General] Designing Programs with Physics

Posted: Thu 10 Sep, 2009 2:34 am
by Wesley
I was sitting in my physics class today, bored as can be. So, what did I do? I pulled out my calculator of course! At first, I was refreshing myself with TI-Basic and of xLib when the thought crossed my mind, "Hey, since I am in physics class, why don't I code something that has to do with physics?"

I then commenced working out dropping a sprite (in the form of a ball) down the screen with constant acceleration. Then, after showing a classmate (who is also majoring in computer science), he said, "Now that would be cool if you could make it bounce."

Hm... That would be cool. But how would I do it? My knowledge in physics is okay, but doesn't go far enough to elasticity, potential energy, kinetic energy, and heat transfer, of objects bouncing (yet anyways).

So, my question is this. How would I go about making an object "appear" to be bouncing like a normal ball (say a basketball)?

I'm wanting to use the equation sf = si + vi*t + 1/2at^2.

Oh, the beauty of programming.

Re: [General] Designing Programs with Physics

Posted: Thu 10 Sep, 2009 9:30 am
by benryves
Given a surface with a normal n and an incident vector i, v=i-2n(i·n). No energy is lost in this bounce (so if you drop your ball onto a floor it'll bounce forever), so you'd probably want to multiply the resulting v with a value <1 to make it lose some velocity every time it bounces.

Re: [General] Designing Programs with Physics

Posted: Thu 10 Sep, 2009 1:12 pm
by Wesley
So, what exactly are n and i? I'm not sure what a normal is or what the incident vector is supposed to be. Are these constants?

Re: [General] Designing Programs with Physics

Posted: Thu 10 Sep, 2009 1:40 pm
by benryves
i is the vector that represents the current velocity of the ball. n is the normal of the surface that the ball is bouncing on. The surface normal is a unit vector (length = 1) that is perpendicular (= at right angles) to the surface, eg the normal for the floor is a vector pointing straight up. i·n is the dot (scalar) product of i and n. For 2D pair of vectors, this would be xi*xn+yi*yn.

Re: [General] Designing Programs with Physics

Posted: Thu 10 Sep, 2009 6:40 pm
by Wesley
Wow, I think I'm completely lost. I got the ball to bounce correctly, but all I used was kinematics equations.

Today, I showed my physics professor the equation v=i-2n(i·n), but he said he's never seen it before. I was trying to grasp it better, but I'm really confused :(.

The kinematics equations will only get me a single axis. I can see how this equation would be better for movement along both axes. Then, if i is the current velocity of the ball, what is v? I though v was the current velocity...

Re: [General] Designing Programs with Physics

Posted: Thu 10 Sep, 2009 8:05 pm
by darkstone knight
psy.gif
psy.gif (32.39 KiB) Viewed 23649 times
s = x-velocity
t = y velocity

basely, you are adding gravity, and air resistance .99(T-.1 -> T to the vecor

its pretty basic.... untill your "ball" skips multiple pixels in one frame... (f*ck collisions)

Re: [General] Designing Programs with Physics

Posted: Thu 10 Sep, 2009 8:13 pm
by benryves
The formula I'm using is one for reflection (you may have heard of the angle of incidence?) The principle is the same if you're bouncing something other than light off a surface, but the letters may be different.

I assumed above that you were familiar with vector maths, which may have been jumping the gun a little! My assumption came from the phrase "majoring in computer science", which I thought was something Americans said in universities, but I could well be wrong...

Re: [General] Designing Programs with Physics

Posted: Fri 11 Sep, 2009 12:38 am
by builderboy
:) I once wrote a pixel based physics engine in Ti Basic. Given a pic, and a starting position/velocity, it would simulate physics for a single pixel. I eventually incorporated this into my arcade version of Peggle http://www.ticalc.org/archives/files/fi ... 41981.html

The physics might not be the fastest (even on my SE) but they work fairly well.
I don't have the original program, but i remember how it worked.

You had variablex X and Y for coordinates and Variables A and B for velocity in the x and y direction. These are effectively vectors. You run a single pixelTest every frame to see of the next position is inside a wall. Once you detect this, move the ball backwards until it is out of the wall, calculate the surface normal with a couple of pxltests, and flip the angle over the angle of the normal. Then resume simulation.

I use a toned down version of this in Peggle that was optimized for speed, but it is nowhere near perfect, as it doesn't have rolling physics (tibasic!! :P) and occasionally gets stuck, but i think it might be helpful in what you're trying to accomplish :)

Re: [General] Designing Programs with Physics

Posted: Fri 11 Sep, 2009 1:49 am
by Wesley
Yes, "majoring in computer science" is what we say. To "major" in something means that's degree you're seeking.

I have some basic knowledge of vector math, but I don't know how I'd go about updating the variables in that equation for each iteration of the loop. Since my ball is just dropping straight down, in a single dimension, I can neglect the x coordinates altogether.

I've attached a screeny of what I accomplished the other day. It looks off, but I think most of it is because of the arc the ball would take as it reaches its max height. Also, the bounce doesn't look nearly realistic, like darkstone knight's example.

I was going to ask if there was a way to make sure the ball never goes below the floor, but I guess there isn't a very good way without losing too much speed.

Re: [General] Designing Programs with Physics

Posted: Fri 11 Sep, 2009 11:23 am
by benryves
Wesley wrote:I have some basic knowledge of vector math, but I don't know how I'd go about updating the variables in that equation for each iteration of the loop.
You wouldn't need to, you use that equation to calculate the new velocity after a bounce.
Since my ball is just dropping straight down, in a single dimension, I can neglect the x coordinates altogether.
In which case the new velocity is just -(old velocity*bounciness), where bounciness < 1 (eg 0.5 to bounce half as high each time).
I was going to ask if there was a way to make sure the ball never goes below the floor, but I guess there isn't a very good way without losing too much speed.
Once you have detected a collision and calculated the new velocity, just move the ball so that it's sitting exactly on the surface it bounced off. :)

Re: [General] Designing Programs with Physics

Posted: Fri 11 Sep, 2009 1:33 pm
by King Harold
That animation doesn't look exactly right to me - it seems as though the ball initially accelerates off the ground

Re: [General] Designing Programs with Physics

Posted: Fri 11 Sep, 2009 2:42 pm
by Wesley
benryves wrote:
Wesley wrote:I have some basic knowledge of vector math, but I don't know how I'd go about updating the variables in that equation for each iteration of the loop.
You wouldn't need to, you use that equation to calculate the new velocity after a bounce.
How are x and y updated then, for the new position of the ball?
benryves wrote:
Wesley wrote: Since my ball is just dropping straight down, in a single dimension, I can neglect the x coordinates altogether.
In which case the new velocity is just -(old velocity*bounciness), where bounciness < 1 (eg 0.5 to bounce half as high each time).
Yeah, I did that in the program demonstrated above. I'm using 0.8 as the elasticity constant.
benryves wrote:
Wesley wrote:I was going to ask if there was a way to make sure the ball never goes below the floor, but I guess there isn't a very good way without losing too much speed.
Once you have detected a collision and calculated the new velocity, just move the ball so that it's sitting exactly on the surface it bounced off. :)
I shouldn't have said "speed". Bad wording. I meant speed of the processing. But I did do that too in the program.
King Harold wrote:That animation doesn't look exactly right to me - it seems as though the ball initially accelerates off the ground
I wondered about that too. Is there a way to convert calculator programs to a text file without having to type it all out? Then I could show it to you. If not, I will probably just end up typing it, regardless :).

I think this is all pretty interesting, especially the comments Ben made about the formula for reflection and angle of incidence. MaxCoderz, the "max" source for all coding (at least in some respects).

Re: [General] Designing Programs with Physics

Posted: Fri 11 Sep, 2009 4:33 pm
by benryves
Wesley wrote:How are x and y updated then, for the new position of the ball?
Sorry, I misinterpreted your question. Every frame (or "physics tick", if you like), add the velocity to the ball's current position. If you detect a collision, then use that formula to modify the velocity.
I think this is all pretty interesting, especially the comments Ben made about the formula for reflection and angle of incidence. MaxCoderz, the "max" source for all coding (at least in some respects).
I found that writing a crude raytracer taught me lots of useful vector arithmetic, including collisions and reflections - shiny spheres are always fun. :D

Re: [General] Designing Programs with Physics

Posted: Sat 12 Sep, 2009 4:43 pm
by calc84maniac
Wesley wrote:Is there a way to convert calculator programs to a text file without having to type it all out?
http://sc.cemetech.net/

Re: [General] Designing Programs with Physics

Posted: Sat 12 Sep, 2009 10:47 pm
by Wesley
Thanks calc84maniac, that's a pretty nifty program. So here's my code:

Code: Select all

:57.9→F
:57.9→I
:.1→V
:.1→N
:-9.8→A
:1E-99→θ
:θ→T
:randInt(0,87→R
:real(7,0
:While 1
: real(0,0
: real(1,R,int(-(F-57)),1,8,0,0,0,0,0,1
: F→I
: N→V
: V+AT→N
: I+VT+.5AT^2→F
: T+.01→T
: If F≤0
: Then
:  θ→F
:  -.8N→N
:  θ→T
: End
:End
F - Sf final position
I - Si initial position
V - Vf final velocity
N - Vi initial velocity
A - a acceleration due to gravity
T - t time

I'm using xLib to clear the screen and draw the ball.