Xtreme Visual Basic Talk

Xtreme Visual Basic Talk (http://www.xtremevbtalk.com/)
-   Tutors' Corner (http://www.xtremevbtalk.com/tutors-corner/)
-   -   GDI+ Backbuffers in VB.NET - Projectile Demo (http://www.xtremevbtalk.com/tutors-corner/160761-gdi-backbuffers-vb-net-projectile-demo.html)

Iceplug 04-14-2004 10:25 AM

GDI+ Backbuffers in VB.NET - Projectile Demo
1 Attachment(s)
For a more basic tutorial of GDI+ in VB.NET, go here:

Backbuffers - How to make and use them :)

What I will be talking about is how to use a backbuffer in your drawings for making smoother animation in .NET.
I'm sure you have used a Graphics object in order to draw to your form... it's important to note that a Graphics object does not hold the drawings that it makes... the drawings are held by whatever the Graphics object is aimed at... for example, if you draw a box with a Graphics object... the box is not held in the Graphics object....

If you were on a form and used:
GFX = Me.CreateGraphics()
The Graphics object is aimed at the form, and any drawings you make will be drawn to the display on the form.
GFX = Graphics.FromImage(Bmp)
The Graphics object is aimed at a bitmap, and drawings are made to the bitmap.
Since the bitmap is not immediately drawn to memory, you can actually do some other things with it... like save it to file... or draw it somewhere else.
In fact, this technique allows you to make a backbuffer for your image display (as I discovered last month)... a backbuffer is just an offscreen representation of what you want to draw to the screen.

In the example below, you will see the line:
GFX = Graphics.FromImage(BackBuffer)
Where BackBuffer is a bitmap object, and GFX is aimed at it... so that means we can draw on the image.

In order to get this image to the display, I'm sure you have heard of the Graphics.DrawImage() method for drawing images... and since we have drawn on the BackBuffer, if I wrote backbuffer as the image to draw, then our newly modified backbuffer image is drawn onto anything that the Graphics object can draw to (which is practically anything)...
so, to draw to a form... you would use:
GFXForm = Me.CreateGraphics()

to draw the finalized buffer to the screen.

Drawing to offscreen surface is considerably faster than drawing directly to the visible surface... and this will allow your GDI+ app to run faster than it would with a single Graphics aimed at the form. :)

Check out my demonstration below... which is heavily commented
(you can just add the form to one of your projects and show it to see how it works... or you can just open the solution included :p)

Iceplug 04-16-2004 10:27 AM

About the Projectiles:
I figured I will also explain how to make objects that fall due to gravity:

If you will remember from a physics book:
D = VT + 1/2 AT^2
V = AT
These two equations are the main equations that describe how things move when their acceleration (gravity) is constant in each dimension (velocity is how much distance changes, acceleration is how much velocity changes) (which is how much you'll add onto the current distance and velocity of the object):

I have rewritten them below with fully descriptive names:

Distancechange = CurrentVelocity * Timechange + 0.5 * CurrentAcceleration * Timechange * Timechange
Velocitychange = CurrentAcceleration * TimeChange

In many games (like my example above), you will have 2 dimensions... one represents the flat part of the earth, the other up and down (perpendicular to the earth surface). So, if you have two dimensions (X and Y), then you'll have two equations like the above

DistancechangeX = CurrentVelocityX * Timechange + 0.5 * CurrentAccelerationX * Timechange * Timechange
VelocitychangeX = CurrentAccelerationX * TimeChange

DistancechangeY = CurrentVelocityY * Timechange + 0.5 * CurrentAccelerationY * Timechange * Timechange
VelocitychangeY = CurrentAccelerationY * TimeChange

So, now you have a distance, velocity, and acceleration in the X direction and in the Y direction.
Usually, you will have the Y axis to represent the up and down direction.
Large values of Y are at the bottom of the form, closer to the land.

So, gravity will only act along the Y axis... therefore the CurrentAccelerationY can be replaced by a constant that you want for gravity... you do not have to put 9.81 or 32.1 here... those are accelerations for meters or feet per second^2... put any number there you want and play around with it until it looks good for you (generally, larger numbers will increase the hangtime of your particles...). Also, since larger numbers for Y lead to the bottom of the form, make gravity positive (negative values will make you 'fall' upwards :p)

Since all the gravity is in the Y direction, there is no gravity in the X direction (yay!) so therefore you can just do this:
DistancechangeX = CurrentVelocityX * Timechange
VelocitychangeX = 0
Since there's no velocity change in X direction, we don't need to do anything to it.

For the value of time, you can use whatever number you want... since you are probably using a timer or a game loop. If you wanted a time-based game loop, you have to use the TimeChange variables, but otherwise, you don't need them! (yay again) Just set them to one (which is just like removing them)

DistancechangeX = CurrentVelocityX
DistancechangeY = CurrentVelocityY + 0.5 * CurrentAccelerationY VelocitychangeY = CurrentAccelerationY
(Remember VelocitychangeX = 0)

Now, we have three very short equations!
Just modify these and put them in a timer or game loop, and they will make the object fall for you: :)
When I say modify: change currentvelocity to the current velocity of your object... the currentacceleration can be substituted by whatever you want for gravity...
for the changes, you will do something like this:
Obj.X = Obj.X + DistanceChangeX
Obj.Y = Obj.Y + DistanceChangeY
Obj.VelocY = Obj.VelocY + VelocityChangeY
(Note: no term for Obj.VelocX here, since VelocityChangeX = 0)


Obj.Y = Obj.Y + Obj.VelocY + 0.5 * Obj.Gravity
Obj.VelocY = Obj.VelocY + Obj.Gravity
Obj.X = Obj.X + Obj.VelocX

That's it.

Remember that if you want to throw an object up, you are throwing it towards the lower numbers for Y on the form... so make VelocY negative to make the object go up.
Also, you can divide by 2 instead of multiplying by 0.5:

Obj.Y = Obj.Y + Obj.VelocY + Obj.Gravity / 2
Obj.VelocY = Obj.VelocY + Obj.Gravity
Obj.X = Obj.X + Obj.VelocX

Iceplug 05-08-2004 01:58 PM

Movement in GDI+
2 Attachment(s)
And here are some more examples.

PackageA is an updated version of the projectile code.
(It didn't work on this one computer, so I updated it)
The moral of the story: avoid the paint and invalidate :).

Package2 is an example of moving with the Keys on the form.

Have fun.
If you have any problems, let me know by private messaging. :)

All times are GMT -6. The time now is 04:52 AM.

Powered by vBulletin® Version 3.8.9
Copyright ©2000 - 2018, vBulletin Solutions, Inc.
Search Engine Optimisation provided by DragonByte SEO v2.0.15 (Lite) - vBulletin Mods & Addons Copyright © 2018 DragonByte Technologies Ltd.
All site content is protected by the Digital Millenium Act of 1998. Copyright©2001-2011 MAS Media Inc. and Extreme Visual Basic Forum. All rights reserved.
You may not copy or reproduce any portion of this site without written consent.