Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

2D All the way! / can any body show me an example of how to make a ball bounce like a screen saver

Author
Message
Crazy Programmer
AGK Developer
21
Years of Service
User Offline
Joined: 6th Sep 2004
Location: Lost in AGK
Posted: 8th Sep 2004 10:59
hey guys i need an example on how to make a ball bounce like a screen saver i was wanting to make my own screen saver it dosent even have to be a ball i just want to know how to make somthing bounce

Will
Van B
Moderator
23
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 8th Sep 2004 17:40 Edited at: 8th Sep 2004 17:41
Please don't double post and please don't create more than one user profile.

Your other thread has been locked.

Anyhoo...

If you imagine your ball as a projectile, a projectile need's to know what direction and how fast it is travelling, a ball is just the same and in 2D, you only have to worry about 2 axis, and on a screen, you only have to use the most basic collision. Say your ball uses the variables BallX# and BallY# to store it's location, and BallSX# and BallSY# to store it's movement speed.

This code...


Would set the variables you need, a random X and Y speed, variating between -1 and 1 which is perfect because it's already a sorta standard speed vector.

Now, you can move your ball by simply adding the X speed to the X position, and doing the same for the Y axis too...

Inc BallX#,BallSX#
Inc BallZ#,BallSY#

Collision now, for your example at least, is real straightforward. Say our ball is on a 16x16 bitmap, and you want to simply paste that image on the screen, you'd just paste at the BallX#,BallY# location, subtracting 8 on each axis to line the ball up with it's centre, but as it's bouncing on the screen, you can use the screen as your collision zone.

Like:



That would ensure that the ball stays in the collision box, 8 pixels inside the edges of the screen so that the ball can never leave the screen.

If you increase the BallYS# variable slightly every loop, you'd have nice smooth gravity as well. Once you get the code in, you'll see how easy it is to mess around with the code, best way to learn IMO.


Van-B


Muhahahahaha.
Flashing Blade
23
Years of Service
User Offline
Joined: 19th Oct 2002
Location: United Kingdom
Posted: 8th Sep 2004 17:48 Edited at: 8th Sep 2004 17:49
you need four variables:

x
y
xvelocity
yvelocity

set x to middle of screen
set y to middle of screen
set yvelocity to speed you want eg 3
ditto xvelocity

do

each loop add xvelocity to x and yvelocity to y
check if x<0 - if it is set x to 0 and reverse xvelocity (*-1)
check if x>screen width - if it is set x to screen width and reverse xvelocity.
ditto with yvelocity (<0 or >screen height)

paste image/position sprite/do text at x,y.

sync
loop


The word "Gullible" cannot be found in any English Dictionary.
Flashing Blade
23
Years of Service
User Offline
Joined: 19th Oct 2002
Location: United Kingdom
Posted: 8th Sep 2004 17:51
Ah Van B beat me.
Should learn to type faster or press refresh before I reply


The word "Gullible" cannot be found in any English Dictionary.
Van B
Moderator
23
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 8th Sep 2004 18:18
It's useful for someone to post the same principles though, gives Crazykid more to go on .


Van-B


Muhahahahaha.
Crazy Programmer
AGK Developer
21
Years of Service
User Offline
Joined: 6th Sep 2004
Location: Lost in AGK
Posted: 8th Sep 2004 20:59
thanks guys for the help

Will
Crazy Programmer
AGK Developer
21
Years of Service
User Offline
Joined: 6th Sep 2004
Location: Lost in AGK
Posted: 9th Sep 2004 10:56
what do yall mean by double post

Will
Old Wrinkly
23
Years of Service
User Offline
Joined: 14th Dec 2002
Location: Nth Devon UK
Posted: 16th Sep 2004 06:10
We know everyone has a different way of reaching the same point, this is the basics that old wrinkly uses, see code

OldWrinkly
Dot Merix
22
Years of Service
User Offline
Joined: 15th Oct 2003
Location: Canada
Posted: 16th Sep 2004 06:26
Double post.. I assume he means making more than one thread on the same topic.

- Merix

Login to post a reply

Server time is: 2026-06-11 17:34:15
Your offset time is: 2026-06-11 17:34:15