Simulating Gravity

Talk about your favorite PC games, Steam and building awesome custom rigs!

Moderator: Moderators

Post Reply
ghosstt
Senior Member
Posts: 1551
Joined: Mon Feb 26, 2007 4:14 pm

Simulating Gravity

Post by ghosstt »

Well, this is vb.net (like usual :lol: ). Here is what I have so far. Just a rough start.

Code: Select all

        Do While obj.Bounds.IntersectsWith(Nothing)
            Application.DoEvents()
            loc = New Point(obj.Location.X - 1)
            obj.Location = loc
            Application.DoEvents()
        Loop
I have that happening on a button click. obj is a picture box, and I have another pic box named floor. I want obj to move down until it collides with floor. :?
APHawkes
Posts: 179
Joined: Fri May 02, 2008 7:50 am

Post by APHawkes »

VB is not my thing, but if it compiles that's a good sign!

As for the simulation of "gravity", I would have to refer to my physics classes in high-school. Falling object accelerate toward the ground at approx 32 ft/sec/sec (9.2 m/sec/sec, which often gets rounded to 10 for convenience). If you're really trying to simulate something "falling", you'd have to account for the time and the object's velocity.

In this simple example, you don't need actual "time" - you may be able to get away with the iterations of the loop. What you ought to do is have a variable for the velocity, a variable for the acceleration, and then adjust accordingly.

Like I said, I don't really know VB, so consider this pseudo-code.

Code: Select all

Dim velocity = 0
Dim acceleration = 1
Do While obj.Bounds.IntersectsWith(Nothing) 
    Application.DoEvents()
    velocity = velocity + acceleration
    loc = New Point(obj.Location.X - (velocity/1))
    obj.Location = loc
    Application.DoEvents()
Loop
I don't know how serious you want to get with this, but here are some formulas to help. Keep in mind that you can use algebra to re-arrange the equations to suit your needs.

velocity = distance / time (this is why I divided velocity by 1 in the code)

acceleration = velocity / time

distance = (final velocity - initial velocity) / 2

distance = (initial velocity) * time + (1/2)*(acceleration)*(time ^2)
ghosstt
Senior Member
Posts: 1551
Joined: Mon Feb 26, 2007 4:14 pm

Post by ghosstt »

Well, It didn't have to be exact gravity. :lol: I basically wanted something to move down until it hit something else. And then when it fell off the edge, it would keep falling.
APHawkes
Posts: 179
Joined: Fri May 02, 2008 7:50 am

Post by APHawkes »

oh.

Never mind then. You need a VB guy.
ghosstt
Senior Member
Posts: 1551
Joined: Mon Feb 26, 2007 4:14 pm

Post by ghosstt »

Well, you have the right concept, its just that its not exactly what i need. I need it so that when obj is not touching floor, obj moves down 3 pixels at a time. and then when it hits floor, it stops. repeat.
APHawkes
Posts: 179
Joined: Fri May 02, 2008 7:50 am

Post by APHawkes »

In that case, I guess what you have should work. Can't really say without more context. If "obj.Bounds.IntersectsWith()" works then I don't see why your method would have a problem.
Post Reply