GDI+ Backbuffers in VB.NET - Projectile Demo
GDI+ Backbuffers in VB.NET - Projectile Demo
GDI+ Backbuffers in VB.NET - Projectile Demo
GDI+ Backbuffers in VB.NET - Projectile Demo
GDI+ Backbuffers in VB.NET - Projectile Demo
GDI+ Backbuffers in VB.NET - Projectile Demo GDI+ Backbuffers in VB.NET - Projectile Demo GDI+ Backbuffers in VB.NET - Projectile Demo GDI+ Backbuffers in VB.NET - Projectile Demo GDI+ Backbuffers in VB.NET - Projectile Demo GDI+ Backbuffers in VB.NET - Projectile Demo GDI+ Backbuffers in VB.NET - Projectile Demo GDI+ Backbuffers in VB.NET - Projectile Demo
GDI+ Backbuffers in VB.NET - Projectile Demo GDI+ Backbuffers in VB.NET - Projectile Demo
GDI+ Backbuffers in VB.NET - Projectile Demo
Go Back  Xtreme Visual Basic Talk > > > > GDI+ Backbuffers in VB.NET - Projectile Demo


Reply
 
Thread Tools Display Modes
  #1  
Old 04-14-2004, 11:25 AM
Iceplug's Avatar
IceplugGDI+ Backbuffers in VB.NET - Projectile Demo Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: Iowa, USA
Posts: 16,583
Smile GDI+ Backbuffers in VB.NET - Projectile Demo


For a more basic tutorial of GDI+ in VB.NET, go here:
GDI+ - What's That?

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.
Also:
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()
GFXForm.DrawImage(BackBuffer)

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 )
Attached Files
File Type: zip Package.zip (10.9 KB, 507 views)
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
  #2  
Old 04-16-2004, 11:27 AM
Iceplug's Avatar
IceplugGDI+ Backbuffers in VB.NET - Projectile Demo Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: Iowa, USA
Posts: 16,583
Smile 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 )

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)

Code:
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:
Code:
Obj.Y = Obj.Y + Obj.VelocY + Obj.Gravity / 2
Obj.VelocY = Obj.VelocY + Obj.Gravity
Obj.X = Obj.X + Obj.VelocX
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
  #3  
Old 05-08-2004, 02:58 PM
Iceplug's Avatar
IceplugGDI+ Backbuffers in VB.NET - Projectile Demo Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: Iowa, USA
Posts: 16,583
Red face Movement in GDI+

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.
Attached Files
File Type: zip PackageA.zip (11.5 KB, 467 views)
File Type: zip Package2.zip (10.8 KB, 353 views)
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing variables and controls using WinForms and VB.NET Bucky Tutors' Corner 0 02-24-2004 04:45 PM
Open VB.NET 2002 project within VB.NET 2003 geo1st487 .NET General 2 01-10-2004 05:13 PM
VB.Net Office Integration FAQ Mike Rosenblum .NET Office Automation 2 12-19-2003 01:17 PM
A VB6 to VB.NET conversion sample Helmar General 0 11-14-2001 06:27 PM

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
GDI+ Backbuffers in VB.NET - Projectile Demo
GDI+ Backbuffers in VB.NET - Projectile Demo
GDI+ Backbuffers in VB.NET - Projectile Demo GDI+ Backbuffers in VB.NET - Projectile Demo
GDI+ Backbuffers in VB.NET - Projectile Demo
GDI+ Backbuffers in VB.NET - Projectile Demo
GDI+ Backbuffers in VB.NET - Projectile Demo GDI+ Backbuffers in VB.NET - Projectile Demo GDI+ Backbuffers in VB.NET - Projectile Demo GDI+ Backbuffers in VB.NET - Projectile Demo GDI+ Backbuffers in VB.NET - Projectile Demo GDI+ Backbuffers in VB.NET - Projectile Demo GDI+ Backbuffers in VB.NET - Projectile Demo
GDI+ Backbuffers in VB.NET - Projectile Demo
GDI+ Backbuffers in VB.NET - Projectile Demo
 
GDI+ Backbuffers in VB.NET - Projectile Demo
GDI+ Backbuffers in VB.NET - Projectile Demo
 
-->