Code Snippets > Collisions and Physics ( Created 13 September 2006 | Last Edited 13 September 2006)

 Simple bouncing by magicman | No VotesMake stuff bounce, what else. Written inBlitz This is quite simple, just makes somthing bounce, weather it be a ball, a blox, or a monkey. ENJOY!! ;This is a bit of code that makes a ball bounce, its pretty simple. ;I commented as well as i could. ENJOY!!! Graphics 800,600,16,2 SetBuffer BackBuffer() AutoMidHandle True ;set up the ball type Type ball Field x#,y#,xv#,yv# End Type ;create a variable for gravity wich will be applied to the ball Global gravity# = 0.08 ; start our main loop, isnt this fun!! While Not KeyDown(1) Cls ;If the user hits the left mouse button, a ball is created at the mouses position If MouseHit(1) b.ball = New ball b\x = MouseX() b\y = MouseY() b\xv=Rnd(-2,2) b\yv = 1 EndIf ;this is the part were you make the ball bounce For b.ball = Each ball If b\x =< 0 b\xv = -b\xv If b\x => 780 b\xv = -b\xv ;basicly, if the ball hits the bottem of the screen, its yv(y velocity) is turned negitve ;wich in turn, makes it bounce. The *.9 is so the ball comes to a stop(well, it never really stops but you get the idea) If b\y => 580 b\yv = -b\yv*.9 ;this keeps it from going off the screen If b\y > 580 b\y = b\y - 1 If b\y =< 0 b\yv = -b\yv Next ;this is the part were we draw an oval at the balls postion, if you change oval to rect you can ;have more of them on screen at once with out it slowing down, oval is a rather slow function ;its even slower when you make it non filled, remove the ; and see. For b.ball = Each ball Oval b\x,b\y,20,20;,0 ;here we move the ball according to its xv and yv(velocity) b\x = b\x + b\xv b\y = b\y + b\yv ;here we apply gravity to the ball by adding gravity to the balls yv b\yv = b\yv + gravity Next ;dealy a little for no real reason Delay 1 ;flip buffers, end main loop, and make it end with out that annoying error message Flip Wend -->

There are no comments for this entry. -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- (c) WidthPadding Industries 1987 681|0 -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=-
 Latest PostsCrash and Burn Dabz Fri 09:44 Chuck Berry - New Album Kuron Fri 03:46 Spinal's Search for Tech spinal Fri 03:30 PPAP Jayenkai Fri 01:22 OUYA vs Cortex Kuron Fri 00:54 GoPro Hero 4 Silver vs Nikon KeyMission 170 Kuron Fri 00:53 The Overpriced Tat Store rockford Thu 13:04 NoSex in England GfK Wed 15:22 Happy Birthday, Windows XP Jayenkai Wed 08:40 What Have You Done? - Oct 2016 GfK Wed 08:21 More

 Latest Items Blog : Spilling my Noodlessteve_ancell Mon 13:38 Link : Character generators! :DAfr0 Sun 07:58 Woot : Getting Noticed... for Gameboy Dev!Afr0 Sat 17:17 Dev-Diary : 1st Ever Wed Workshop Progresszzoom Thu 23:12 Showcase : Biomorph FractalsAndy_A Wed 12:43 Snippet : Fisheye Lens EffectJayenkai Wed 11:50 Snippet : 2D BallsJayenkai Wed 06:22 Showcase : Mining Map AI DemoStealth Tue 19:41 Showcase : Biotic Limit - Games For Better (2 Days) Editionrychan Mon 12:27 Dev-Diary : Intellivision Controller Adapterspinal Sun 11:33 Snippet : Contour Map from DataHoboBen Fri 18:14 Snippet : FPS Control (WASD+Mouse)Pakz Wed 22:30 Article : Concurrency in C#Afr0 Sun 12:28 News : Newsletter #281HoboBen Fri 04:18 News : Newsletter #280Jayenkai Sat 05:10More

Who's Online
Dabz
Fri, at 09:49
rockford
Fri, at 09:35
Jayenkai
Fri, at 09:04
Kuron
Fri, at 04:57
Pakz
Fri, at 04:07
spinal
Fri, at 03:30
GfK
Fri, at 01:42