To all,
(DarkBasic Classic)
I meant for this post to be within the DarkBasic Classic forum.
Spidyfan made me to think on physics, and so, once again, I have been experimenting at coding a projectiled bouncing ball. The code in the snippet below works fine. However, it was created upon my own simple logic, so I am wondering of a more efficient, and a truly correct to physics method to create this effect.
I have simply used a constant speed, which is one unit per loop, but have made the rate of change in direction to increase after each bounce. Therefore, the change in the angle is what makes the ball to bounce. I am trying to find a method other than using a 'gravity' variable and changing the ball's position on the y axis with it. I am trying to use a vector, I suppose, to create the projectile movement.
set display mode 800,600,32
sync on
sync rate 60
`make textures
ink rgb(255,255,255),0
for t = 1 to 13 step 4
line 1,t,13,t
next t
get image 1,0,0,13,13
cls
`make objects
make object sphere 1,5
texture object 1,1
make object plain 2,500,10
texture object 2,1
scale object texture 2,0.10,0.50
xrotate object 2,90
xrotate camera 15
x# = 230
y# = 2.5
angle# = 135
anglechangerate# = 0.70
direction$ = "left"
speed# = 1
quitbounce = 0
repeat
`reset program upon press of space bar
if spacekey() = 1
x# = 230
y# = 2.5
angle# = 135
anglechangerate# = 0.70
speed# = 1
direction$ = "left"
quitbounce = 0
endif
`if ball collides with ground
if y# < 2.5
`if ball's bounces are on very short intervals;the rate of the change in direction
if anglechangerate# > 14
`make ball stop bouncing
quitbounce = 1
`reposition ball to above surface
y# = 2.5
`reset its angle to move in direction it is
if direction$ = "left" then angle# = 180
`if ball's bounces are on long intervals
else
lastangle# = angle#
`reset ball positionto above surface
y# = 2.5
`reset angle to opposite direction
angle# = wrapvalue(angle# - 90)
`make the ball's bounce a bit shorter
inc anglechangerate#,0.70
endif
endif
`if ball is not able to bounce, and its moving speed is greater than 0, then decrement its moving speed
if quitbounce = 1 and speed# > 0 then dec speed#,0.005
`if speed is slower than 0.01 units per loop then stop ball
if quitbounce = 1 and speed# < 0.01 then speed# = 0
`if ball is able to bounce then increment vector angle
if quitbounce = 0 then angle# = wrapvalue(angle# + anglechangerate#)
`make ball rotate;decrement rotation speed with speed#;ball will graduallyt stop rotating
rotangle# = wrapvalue(rotangle# + (5 * speed#))
`get new coordinates for ball
x# = x# + cos(angle#) * speed#
y# = y# + sin(angle#) * speed#
position object 1,x#,y#,0
zrotate object 1,rotangle#
position camera x#,20,-50
text 0,0,"ball's angle of direction:" + str$(angle#)
text 0,15,"ball's angle of direction upon last collision:" + str$(lastangle#)
text 0,30,"rate of change in angle of direction:" + str$(anglechangerate#)
text 0,45,"ball's current angle of rotation:" + str$(rotangle#)
center text 400,580,"press space bar to reset ball"
sync
until mouseclick() = 1
end
+NanoBrain+