[General] Designing Programs with Physics

Got questions? Got answers? Go here for both.

Moderator: MaxCoderz Staff

User avatar
Wesley
Regular Member
Posts: 137
Joined: Sun 12 Oct, 2008 1:42 am
Location: Boise, Idaho
Contact:

[General] Designing Programs with Physics

Post 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.
ImageImage
ImageImage
Image
User avatar
benryves
Maxcoderz Staff
Posts: 3087
Joined: Thu 16 Dec, 2004 10:06 pm
Location: Croydon, England
Contact:

Re: [General] Designing Programs with Physics

Post 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.
User avatar
Wesley
Regular Member
Posts: 137
Joined: Sun 12 Oct, 2008 1:42 am
Location: Boise, Idaho
Contact:

Re: [General] Designing Programs with Physics

Post 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?
ImageImage
ImageImage
Image
User avatar
benryves
Maxcoderz Staff
Posts: 3087
Joined: Thu 16 Dec, 2004 10:06 pm
Location: Croydon, England
Contact:

Re: [General] Designing Programs with Physics

Post 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.
User avatar
Wesley
Regular Member
Posts: 137
Joined: Sun 12 Oct, 2008 1:42 am
Location: Boise, Idaho
Contact:

Re: [General] Designing Programs with Physics

Post 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...
ImageImage
ImageImage
Image
darkstone knight
New Member
Posts: 67
Joined: Sun 09 Nov, 2008 1:56 pm

Re: [General] Designing Programs with Physics

Post by darkstone knight »

psy.gif
psy.gif (32.39 KiB) Viewed 23597 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)
User avatar
benryves
Maxcoderz Staff
Posts: 3087
Joined: Thu 16 Dec, 2004 10:06 pm
Location: Croydon, England
Contact:

Re: [General] Designing Programs with Physics

Post 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...
builderboy
New Member
Posts: 17
Joined: Wed 10 Jun, 2009 9:58 pm

Re: [General] Designing Programs with Physics

Post 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 :)
User avatar
Wesley
Regular Member
Posts: 137
Joined: Sun 12 Oct, 2008 1:42 am
Location: Boise, Idaho
Contact:

Re: [General] Designing Programs with Physics

Post 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.
Attachments
wabbitemu.gif
wabbitemu.gif (10.64 KiB) Viewed 23587 times
ImageImage
ImageImage
Image
User avatar
benryves
Maxcoderz Staff
Posts: 3087
Joined: Thu 16 Dec, 2004 10:06 pm
Location: Croydon, England
Contact:

Re: [General] Designing Programs with Physics

Post 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. :)
King Harold
Calc King
Posts: 1513
Joined: Sat 05 Aug, 2006 7:22 am

Re: [General] Designing Programs with Physics

Post by King Harold »

That animation doesn't look exactly right to me - it seems as though the ball initially accelerates off the ground
User avatar
Wesley
Regular Member
Posts: 137
Joined: Sun 12 Oct, 2008 1:42 am
Location: Boise, Idaho
Contact:

Re: [General] Designing Programs with Physics

Post 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).
ImageImage
ImageImage
Image
User avatar
benryves
Maxcoderz Staff
Posts: 3087
Joined: Thu 16 Dec, 2004 10:06 pm
Location: Croydon, England
Contact:

Re: [General] Designing Programs with Physics

Post 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
User avatar
calc84maniac
Regular Member
Posts: 112
Joined: Wed 18 Oct, 2006 7:34 pm
Location: The ex-planet Pluto
Contact:

Re: [General] Designing Programs with Physics

Post 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/
~calc84maniac has spoken.

Projects:
F-Zero 83+
Project M (Super Mario for 83+)
User avatar
Wesley
Regular Member
Posts: 137
Joined: Sun 12 Oct, 2008 1:42 am
Location: Boise, Idaho
Contact:

Re: [General] Designing Programs with Physics

Post 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.
ImageImage
ImageImage
Image
Post Reply