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.

Author
Message
adsta
21
Years of Service
User Offline
Joined: 5th Feb 2003
Location:
Posted: 5th Feb 2003 07:59
Hi,
I am trying to make a 3d minigolf game using darkbasic. But I'm having problems at figuring out how to work out how to make the ball bounce off walls.
If anyone could help, I would greatly appreciate it.
Cheers
QuothTheRaven
21
Years of Service
User Offline
Joined: 2nd Oct 2002
Location: United States
Posted: 5th Feb 2003 14:34
when the ball hits a wall, find its forward velocity, reverse the angle, and move it along the bounce with a different velocity? There should be some variable that stores the ball speed.
freak
21
Years of Service
User Offline
Joined: 20th Jan 2003
Location:
Posted: 5th Feb 2003 16:21
isn't there a command to get the object speed? something like speed(objectnumber) or objectspeed(objectnumber)...

you could use some variable to store the 'bouncing factor' (varying from 0 to 1), then you can multiply the speed (stored when the ball hits the wall) with that variable

for other types of balls you can change that bouncing factor

trager
21
Years of Service
User Offline
Joined: 5th Feb 2003
Location: United Kingdom
Posted: 5th Feb 2003 19:45
I wrote this about a year ago trying to get realistic physics from balls.

To make the balls decelerate quicker then decrease the cof# value.

There is alot of code but its only the last half that handles the physics, hope it helps.

sync rate 40
sync on
autocam off

backdrop on
screenwidth = 320
halfwidth = screenwidth/2
screenheight = 220
halfheight = screenheight/2
maxspeed = 8
cof# =0.5
numballs=19

dim ball#(numballs,9)
x=0
y=1
xv=2
yv=3
mass=4
radius=5
oldx=6
oldy=7
grad = 8
c=9

randomize timer()


for lp = 0 to numballs
ball#(lp,x) = rnd(screenwidth)-halfwidth
ball#(lp,y) = rnd(screenheight)-halfheight
ball#(lp,xv) = rnd(maxspeed)
ball#(lp,yv) = rnd(maxspeed)
ball#(lp,mass) = 1
ball#(lp,radius) = 8
ball#(lp,oldx) = ball#(lp,x)
ball#(lp,oldy) = ball#(lp,y)
make object sphere lp+1, ball#(lp,radius)
color object lp+1, rgb(rnd(255),rnd(255),rnd(255))

next lp

move camera -200

do
color backdrop rgb(0,0,0)
mx = mousemovex()/16
my = 0-mousemovey()/16
for lp=0 to numballs

ball#(lp,oldx) = ball#(lp,x)
ball#(lp,oldy) = ball#(lp,y)
if abs(mx) > abs(ball#(lp,xv)) then ball#(lp,xv) = ball#(lp,xv) + mx
if abs(my) > abs(ball#(lp,yv)) then ball#(lp,yv) = ball#(lp,yv) + my
ball#(lp,x) = ball#(lp,x) + ball#(lp,xv)
ball#(lp,y) = ball#(lp,y) + ball#(lp,yv)

if ball#(lp,x) >= halfwidth then ball#(lp,xv) = 0-ball#(lp,xv): ball#(lp,x) = halfwidth
if ball#(lp,x) <= 0-halfwidth then ball#(lp,xv) = 0-ball#(lp,xv): ball#(lp,x) = 0-halfwidth
if ball#(lp,y) >= halfheight then ball#(lp,yv) = 0-ball#(lp,yv): ball#(lp,y) = halfheight
if ball#(lp,y) <= 0-halfheight then ball#(lp,yv) = 0-ball#(lp,yv): ball#(lp,y) = 0-halfheight
position object lp+1, ball#(lp,x), ball#(lp,y), 0
next lp

for ball_a = 0 to numballs
for ball_b = 0 to numballs
if ball_a <> ball_b
gosub collision
endif
next ball_b
next ball_a

sync
loop
end

collision:

nabx# = (ball#(ball_b,x) - ball#(ball_a,x))
naby# = (ball#(ball_b,y) - ball#(ball_a,y))
length# = sqrt(nabx#*nabx# + naby#*naby#)

if length# <= (ball#(ball_a,radius)/2 + ball#(ball_b, radius)/2)
if length#=0 then length# = sqrt(1)
rem find the collision point on the 2 vectors doesnot consider velocity

naby#=naby#/length#
nabx#=nabx#/length#

rem get perp. vector
rem does reversing taby# as well produce the opposite vector?
rem it sure does provided the x's and y's match unlike perp.
tabx# = 0-naby#
taby# = nabx#

rem compute initial velocities
vain# = ball#(ball_a,xv)*nabx# + ball#(ball_a,yv)*naby#
vbin# = ball#(ball_b,xv)*nabx# + ball#(ball_b,yv)*naby#

rem compute velocities after collision

ma# = ball#(ball_a,mass)
mb# = ball#(ball_b,mass)

vafn# = (mb#*vbin#*(cof#+1) + vain*(ma# - cof# * mb#)) / (ma# + mb#)
vbfn# = (ma#*vain#*(cof#+1) - vbin*(ma# - cof# * mb#)) / (ma# + mb#)

vaft# =ball#(ball_a,xv)*tabx# + ball#(ball_a,yv)*taby#
vbft# =ball#(ball_b,xv)*tabx# + ball#(ball_b,yv)*taby#


rem translate final velocity into coord system
rem sotre final spped
ball#(ball_a,xv) =vafn#*nabx# + vaft#*tabx#
ball#(ball_a,yv) =vafn#*naby# + vaft#*taby#

ball#(ball_b,xv) =vbfn#*nabx# + vbft#*tabx#
ball#(ball_b,yv) =vbfn#*naby# + vbft#*taby#

rem update coords
ball#(ball_a,x) = ball#(ball_a,x) + ball#(ball_a,xv)
ball#(ball_a,y) = ball#(ball_a,y) + ball#(ball_a,yv)

ball#(ball_b,x) = ball#(ball_b,x) + ball#(ball_b,xv)
ball#(ball_b,y) = ball#(ball_b,y) + ball#(ball_b,yv)
endif

return

trager
21
Years of Service
User Offline
Joined: 5th Feb 2003
Location: United Kingdom
Posted: 5th Feb 2003 19:58
There are a couple of problems with the above code but basically if you keep the speed of the ball fairly slow you shouldnt have to worry about it; otherwise the bals will pass through each other.

Im trying to find my intersection of line segments code whcih is a little simpler so dont panic if this seems too much.

freak
21
Years of Service
User Offline
Joined: 20th Jan 2003
Location:
Posted: 5th Feb 2003 20:06
wow, cool!!!!!!!
seems like theyre influenced by each others gravity, when they turn around eachother...

Kangaroo2
21
Years of Service
User Offline
Joined: 26th Sep 2002
Location: United Kingdom
Posted: 6th Feb 2003 00:19
Aww there was me thinking some1 had made a thread in tribut to my game "bounce" lol Anyway this sounds cool, keep it up

Coming Soon! Kangaroo2 Studio... wait and quiver with anticipation! lol
samjones@kangaroo2.com - http://www.kangaroo2.com - If the apocalypse comes, email me
trager
21
Years of Service
User Offline
Joined: 5th Feb 2003
Location: United Kingdom
Posted: 6th Feb 2003 11:16
Yup gravity is simulated, they spin with each other because the collision testing is shoddy in this demo but still it looks quite cool.

To correct it you need to consider the velocity and size of the objects as well as time in the collision test and as you can imagine this is quite complex.

If you have only one moving object, or only one moving at any speed, then it gets a lot easier.

Im still looking for my collision test code.

freak
21
Years of Service
User Offline
Joined: 20th Jan 2003
Location:
Posted: 6th Feb 2003 12:58
Yup gravity is simulated, they spin with each other because the collision testing is shoddy in this demo but still it looks quite cool.

yeah but I meant the gravity field of each sphere... (and I thouhgt they spinned around eachother because they came too close in eachtother's gravitation field)... Lol it's rather a coincidal effect but it is cool indeed

trager
21
Years of Service
User Offline
Joined: 5th Feb 2003
Location: United Kingdom
Posted: 6th Feb 2003 13:04
That is kinda whats happening, its more obvious when the balls have different masses.

trager
21
Years of Service
User Offline
Joined: 5th Feb 2003
Location: United Kingdom
Posted: 6th Feb 2003 15:31
I just found half of my line segment collision code and have just finished it off.

I'm afraid its rather poorly remarked and I have a tendancy of using my own code rather than DB's functions so if you get too lost in it give me a yell and I'll document it for ya.

basically this code will detect any collision between ball and wall REGARDLESS OF SPEED in 2D space. Although the collision test does look complex it probably still runs faster than a single sqrt() function call.



trager
21
Years of Service
User Offline
Joined: 5th Feb 2003
Location: United Kingdom
Posted: 6th Feb 2003 15:33
Just noticed this version has a single sqrt function call, oh well so much for that claim.

Login to post a reply

Server time is: 2024-05-18 17:37:38
Your offset time is: 2024-05-18 17:37:38