Tracking Forums, Newsgroups, Maling Lists
Home Scripts Tutorials Tracker Forums
  HOME    TRACKER    Visual Basic


how to add gravity to a VB game?

View Complete Forum Thread with Replies

See Related Forum Messages: Follow the Links Below to View Complete Thread
Ok, i am starting a sidescrolling shooter game, in the game you will be able to jump, but i have a problem with gravity. What kind of code would i use to set a force of gravity always to down? if its hard to explain please post an example thanks

Gravity Help
i need a bit (or alot) of help using gravity...

i'm writing a basketball program and i need an image of a ball to drop and bounce back up.

i have already got code for the gravity which i borrowed but i need to know how to make it incorporate the ball the move.

any help is good help.
thanks in advance.

p.s i am not the best at V.B.

im trying to model a projectile really simply with gravity, but my maths isnt too good! heres what i have in the timer:

Private Sub Timer1_Timer()
Dim NPosition As PositionType
Seconds = Seconds + 1
NPosition.x = (Position.x) + (Acceleration * Position.x * Seconds)
NPosition.y = (Gravity * Position.y) + (Acceleration * Position.y * Seconds)
If NPosition.x > MinCoOrd And NPosition.x < MaxCoOrd Then
If NPosition.y > MinCoOrd And NPosition.y < MaxCoOrd Then
Position = NPosition
Else: Timer1.Enabled = False
End If
Else: Timer1.Enabled = False
End If
End Sub
the NPosition.y is less than 0 on my first run. my minCoOrd is 0 and my MaxCoOrd is 1000, should it be bigger? also im currently displaying my tank as a label like this:

Private Sub DrawTank()
lblB.Left = Position.x
lblB.Top = Position.y
End Sub
should it be

Private Sub DrawTank()
lblB.Left = Position.x
lblB.Top = 1000 - Position.y
End Sub

(nb. gravity = -9.81, acceleration = 1.1)


I'm creating a game in DirectX7, and I just want to please see some gravity code to get it to work. I've looked on Lucky's, but it's hard to understand. I want some gravity commented code please. Thank you in advance for anybody who helps.

is there a way of adding gravity to a vb program?

Hi I am writing a 2d simulation of gravity, can anyone help me with a function which detects if any two shapes come in "contact." It would output a number wheather the falling shape (shape(0)) hits the other shape on its own bottom(output 1) left(2) right(3) bottom(4). Thanks for any help.

I am trying to create gravity for a flight of a ball.  The code i am using causes the ball to travel in a more triangular pattern as opposed to a arc.  I am trying my best to follow the laws of gravity (for a crossover lesson with physics).

g is a constant value -- 9.8
CODE    conv = Val(hsbAngle.Value) / 180 * 3.141592654
    shpBall.Left = shpBall.Left + Vel * Cos(conv)
    If blnDown = False Then
        Vel = Vel - g
        shpBall.Top = (shpBall.Top - ((Vel) * Sin(conv)))
        Vel = Vel + g
        shpBall.Top = (shpBall.Top + ((Vel) * Sin(conv)))
    End If
    If Vel <= 0 Then blnDown = True

Gravity &amp; Installation
Hi there

I'm having 2 questions, and i hope they will be answered.

1. Does someone have a gravity code in here. (realistic). I hope someone can do it like

If (colision with ground) = false then
"What formule do i have to put here so the y axis of my player changes realistic" real gravity
end if

2. Installation question moved here

Thx for all help

Game Gravity
is there a way for me to program in sort of gravity so that when the character on the screen "jumps" it will fall back down slowly and will stick to the ground when it walks and so on

Gravity Ball
i know someone asked this earlyer buy the thread is gone now. I am a fairly new programer and wanted to make a game using some gravity or what every you would call it with a ball. Here is an example of what i mean.

Gravity, Orbiting...
Ok. I have been playing around with this for a while, and I'm kinda confused...

I don't want any code, just a concept right now. What is the easiest way to simulate graviy? For example, the Earth and Moon. How would I make the Moon six times "lighter" than the Earth? Once I have that it shouldn't be too hard to make the Moon orbit the Earth, at least I don't think so...

Jacob Sheehy

The more I C, the less I see.

Gravity Problem
I'm using DX7, and I'm having some trouble with Gravity. It's being looped but when your pressing both buttons it doesn't really work. SpeedX seems to be wrong because Mario seems to go the opposite way. Here's my BLTGraphics sub and this is really all you need to see. Why isn't Mario going forward?

Sub BLTGraphics()
Dim rBack As RECT

If bIsJumping And Not bIsMoving Then
If Not bDown Then
SpeedY = -10
SpeedY = 0
End If

If SpritePosY < 100 Then SpeedY = 0: bDown = True
SpeedY = SpeedY + Gravity
SpritePosY = SpritePosY + SpeedY
End If
If bIsJumping And bIsMoving Then
If Not bDown Then
SpeedY = -10
SpeedX = -5
SpeedY = 0
SpeedX = -5
End If

If SpritePosY < 100 Then SpeedY = 0: bDown = True
SpeedY = SpeedY + Gravity
SpritePosY = SpritePosY + SpeedY
SpeedX = (SpeedX + Gravity)
SpritePosX = (SpeedX) + SpritePosX
End If
If SpritePosY >= origSpritePosY Then bIsJumping = False

backbuffer.BltColorFill rBack, 0
backbuffer.BltFast SpritePosX, SpritePosY, MainSurf(SpriteNum), rBack, DDBLTFAST_WAIT Or DDBLTFAST_SRCCOLORKEY
End Sub

P.S. I'm having some problems with Direct Input. Can it register two keys at once because its so messed up. I can hit up and go up and then hit right, but I can't hit right and then up and go up

Center Of Gravity
I am looking for some help on selecting the best appoach for this problem

I am programming in AutoCad and I am making 3D assemblies built out of many 3D entities. Each of the 3D entities can return its centroid(x,y,z) and volume which I can then derive weight from.

If I had lets say 30+ solid entities and I knew their x,y,z coordinates, weight, and the centroid(center of gravity), then how could I determine the overall center of gravity of the entire assembly.

I have a plan of how I think it should work. it goes like this.

Take 2 centroids and create a line between them, then based on their weight slide kind of a virtual fulcrum between them that would move toward the entity that weighed the most. Now I would have a new centroid with the new x,y,z and it would take on both of the weights. The I would just keep looping through everything until I created the last virtual centroid which would then be the center of gravity for the entire assembly. The I would know where to pick up the assembly with a crane and have it balanced.

Before I go and try to re-invent the wheel I have to believe this problem must have been already solved.

Any help and ideas would be greatly appreciated and thanks i advance.

Dave Baranas

Mario + Gravity
Im making a mario clone, and wondering if anyone has code that is close to that of mario, i can make platform movement, but the original mario settings are so perfect

Artificial Gravity
Look at this

It looks like something for school, but no it aint, just made it for fun.

Noobie In Need Of Assistance With Gravity
i have made a simple VB platform game (competition with mate to make on first) (no directx) so far i have made the first level, programed some code to make my character move and pick up items but im having trouble with gravity. now so far all i've got is this

Dim locgravity As Point
If Not PicPlayer.Bounds.IntersectsWith(tile1(.Bounds) Then
locgravity = New Point(PicPlayer.Location.X, PicPlayer.Location.Y + 1)
PicPlayer.Location = locgravity
End If

this is run on a timer and each tick this happens ^^^

now so far this will only stop the character from falling out of the game if he is on tile1 but if i jump to tile2 he goes straight through it. and i want to know how to do multiple collisions because if i do nested ifs or more if statements below that it still will only collide with the first tile.

the answer is probably jumping out at me but i just cant seam to do it.

plx help /cry

Ideas To How To Incoperate Gravity
I am trying to think of a way that I would beable to incoperate gravity on a moving object. I have tryed a few things but cant not seem to get it to work.

I am looking for a basic approach to doing this.

if anyone had any ideas or how to go about doing this please help me out.

Gravity Physics To Mouse
how would i do this i wanna make a bunch of programs that make the mouse do stuff so far i have it circles around form bounces off side now i want it so that where ever the mouse is it drops and bounces but it varies with the height etc how would i do this

Projectiles - Multiple Sources Of Gravity...

I'm trying to create a program that utilizes some sort of projectile system. Currently, I can show the path of a projectile launched at a certain angle and power (both given by the user).

My problem - I don't want the projectile to be attracted to the bottom of the screen (Earth's gravity) anymore. Instead I want the program to 'somewhat' simulate a projectile's movement through space. I want to place an object on the screen (a circle/planet) that attracts the projectile to a certain degree (has a certain amount of gravity). Furthurmore, if this can be accomplished, I would like to have multiple objects on the screen, each having a different density/amount of pull on the object.

So...I want to be able to show the path of a projectile dealing with many sources of gravity, and I'm having difficulties accomplishing this...

Any help would be appreciated.

Gravity, Bounce Back Does Not Work Properly
Hi I have written a program which fallows basic laws of physics to create a bouncing box, or well that was my intention, however the box loses far to much energy after the first bounce. the code of intrest
VB Code:
Dim Time As Integer        Dim Other As IntegerDim Velocity As DoubleDim IVel As DoubleDim Acc As DoubleDim Distance As DoubleDim P As DoubleDim Bottom As DoubleDim Unit As DoubleDim Kenetic As DoubleDim LossK As DoubleDim Mass As DoubleDim Loss As DoubleDim ObjectH As DoubleDim InitV As Double Private Sub Gravityed()    Bottom = Me.Height    If (Shape1.Top + Shape1.Height) >= (Me.Height - 750) Or (bottomBetween = True And leftBetween = True) Or (bottomBetween = True And rightBetween = True) Then        Time = 0        P = 0        Kenetic = (0.5 * Mass) * (Velocity * Velocity) * Loss        IVel = Sqr(Kenetic)        Other = 1    Else        Time = Time + 1        Velocity = IVel + (Acc * Time)        Distance = Velocity * Time        P = P + Distance        Shape1.Top = Shape1.Top + (P / Unit)    End IfEnd SubPrivate Sub Gravity2()    If Velocity <= 0 Then        Time = 0        IVel = 10        Other = 0        InitV = 0    Else        Time = Time + 1        Velocity = IVel + ((-1 * Acc) * Time)        TxtVel.Text = Velocity        Distance = Velocity * Time        P = P + Distance        Shape1.Top = Shape1.Top - (P / Unit)    End IfEnd Sub Private Sub Gravity_Timer()    txtPosition.Text = P    If Other = 1 Then        Call Gravity2    Else        Call Gravityed    End If    Call CollisionDetEnd Sub

you can diregard the bottomLeft and BottomRight variables, they have to do with an object collision part of the program. I will include the Form file with this. any help would be greatly appreciated thanks!

&lt;b&gt;GRAVITY Program, Needs More Accurate Timing And Efficency
Hi I have a program that detects collision and has gravity, but it has some issues with that look of the shapes having weight, I will include the form along with this. Any help would be greatly appreciated.

Anti-Gravity Simulation (proton / Neutron Model)
Hi, I am trying to perfect this Qbasic code to draw the path of an electron moving past (in close proximity) a proton, suffice it to say that the electron should turn away from the large proton (because the have opposite charge), like anti-gravity would look.

The electron is modelled by the PARTICLE "e" and the proton by "p" in the following code. When you run this code in Qbasic, the trail of dots is the path of the electron from left to right, and the large dot is the stationary proton.

'Proton / Electron repulsion model


TYPE particle

DIM p AS particle
DIM e AS particle

'proton roughly in centre of screen...
p.x = 400
p.y = 200

CIRCLE (p.x, p.y), 10, 4
PAINT (p.x, p.y), 4

e.x = 0 'electrons always start at left edge of screen

e.y = 210 '!!! this line!!!

e.vx = 5 'speed from left to right
e.vy = 0 'no vertical speed

FOR i = 1 TO 200 'draws 200 steps in electron's path

dx = (p.x - e.x)
dy = (p.y - e.y)
dist = SQR((dx * dx) + (dy * dy))

'##### Horizontal symmetry problem occurs in this block

fx = (500 / ((dx * dx) + (dy * dy))) * SIN(ATN((dx / dy)))
fy = (500 / ((dx * dx) + (dy * dy))) * COS(ATN((dx / dy))) 'fy means Force in direction Y (vertically)

e.vx = e.vx + fx
e.vy = e.vy + fy

e.x = e.x + e.vx
e.y = e.y + e.vy


PSET (e.x, e.y), SQR((e.vx * e.vx) + (e.vy * e.vy)) MOD 15 'simply colours the dots according to the speed of the particle at that moment

All works well when the electron starts slightly below the proton on the screen, but if you start it above the proton, than the math goes wrong somewhere because the electron curves towards the proton when it should be a mirror image of the lower half of the screen.
To change the height of the starting point, alter the number on the line commented "'!!! this line !!!", but dont use 200 because you get a divide by 0 error. To go above the proton, try 180.

Any budding mathematicians out there wanna help me fix this?
I know its not VB but the guys in here are some of the best programmers I know. I'm stumped by this a bit!

Gravity: Moon Orbiting Earth Orbiting Sun
I'm trying to make a physics engine in which a moon orbits (circular) a planet, which in turn orbits a sun (also circular). Currently I have a planet orbiting a sun, and have absolutly no problems there. The planet has a force due to gravity of
a= Mass of Sun / (Distance between sun and planet) ^2

And its initial speed for circular orbit is
v = Sqr(Mass of sun / Distance)

(I didn't include G as its only a constant)
So using these I can make the planet orbit the sun. The problem is that when I try to make the moon orbit the planet, while at the same time the planet orbits the sun, I am unable to make the moon orbit in a circle, relative to the planet.
Originally, I thought it would just be the planets initial orbital velocity + moons orbital velocity for the planet. This didnt work.
Here's a diagram which should explain it further


O = Sun
o = Planet
. = moon
For Simplicities sake, lets say they all lie on the same line, so their initial direction is straight up, all orbiting in a anticlockwise direction.

Basically, my question is, how do I caculate the orbital velocity of the moon around the planet, relative to the sun?

Copyright 2005-08, All rights reserved