Post by jon93 on Oct 21, 2022 22:47:04 GMT
Hello everyone,
I am very puzzled and need to have my sanity restored.
I am writing a trivial program in which a ball (represented by a pset point) bounces inside a box. (Planning to use it for more complicated things)
There is gravity (g) which pulls the ball down. (increasing y direction values )
There is a potentially infinite repetition of calculation of new y velocity (yvel) (increased by gravity) changing the y position, and x velocity (xvel) being constant.
When it hits the side it bounces in the other direction.
When it hits the bottom it bounces back up.
It should just go on forever bouncing around.
But for some reason the height of the bounce (y position value) gradually decreases. The y velocity (yvel) gradually decreases.
I cannot see why this happens.
Is it a result of a cumulative effect of rounding numbers when there are many iterations?
global Xmax, Ymax, Pi, Deg, Rad
global g, iters
global xpos, ypos, previousypos, previousypos, xvel, yvel, itersToDo
global previousxvel, previousyvel
global minxpos, maxxpos, minypos, maxypos, iterstodo
global xrange 'difference between x left of box and x right of box
global xshow , yshow
global slownum
global howoftentocls
global multiplier 'i used this to change the values of xpos etc that are being calculated
'then divide by it to change it into values that can be displayed
'it serves no purpose except to see if the problem with the program
'is associated with the size of the numbers being calculated
'are they for 0 to 2 or 0 to 100 to 100 etc
'as far as I can see the problme has nothing to do with the size of the number
ulx = 10 'upper left x value of box on screen
uly = 10
ww =1200
wh =600
WindowWidth = ww
WindowHeight = wh
UpperLeftX = ulx
UpperLeftY = uly
open "Graphic Title" for graphics_nsb_nf as #gr '<======================= title
#gr "setfocus"
#gr "trapclose [quit]"
#gr "when leftButtonUp lButtonUp"
#gr "when characterInput charIn"
#gr "down"
#gr "fill black"
'#gr "trapclose [quit]"
'wait
call back 255,0,255
call fore 255,0,0
b$ = ""
call StartVals
for iters = 1 to iterstodo
call Sloww 'slow things down so it can be seen
call SavePreviousXY
call MoveBall
call BounceSub
call pset xpos/multiplier, ypos/multiplier
'call circ xpos/distancemultipiernum, ypos/distancemultipiernum, 2
'sometimes need to clear display
if iters mod howoftentocls = 1 then
print #gr, "fill black"
end if
next
'============================== sets drawing
#gr "flush"
wait
[quit]
close #gr
end
'***********************************************
sub StartVals
multiplier = 1
slownum= 100 'slow things down
howoftentocls = 3000 'how often to clear the display
iterstodo = 19500
g = .03 * multiplier';gravity constant
'min and max x and y positions. This determines where the ball bounces.
minxpos = 0
maxxpos = 1150 * multiplier',1
minypos = 0
maxypos = 555 * multiplier'1
xpos = 20 * multiplier'0 .200
ypos = 20 * multiplier'0' .200
yvel = 0
xvel =.09 * multiplier
end sub
sub BounceSub
'bounce off walls and bottom
'bounce off left or right wall
if xpos <= minxpos and previousxpos > minxpos then
xvel = xvel * -1
else
if xpos >= maxxpos and previousxpos < maxxpos then
xvel = xvel * -1
end if
end if
'bounce off bottom
if ypos >= maxypos and previousypos < maxypos then
print "now in bounce sub iters",iters
print "previous xvel yvel", previousxvel, previousyvel
print "currenr xvel yvel", xvel, yvel
yvel = yvel * -1
end if
end sub
sub MoveBall
yvel= yvel + g
xpos= xpos + xvel
ypos= ypos + yvel
end sub
sub SavePreviousXY 'probably not actually needed
previousxvel = xvel
previousyvel = yvel
previousxpos = xpos
previousypos = ypos
end sub
sub Sloww' slow things down
for j = 1 to slownum
dummy=1
next
end sub
sub fore r, g, b
#gr "color ";r;" ";g;" ";b
end sub
sub back r, g, b 'backcolor is used for fills
#gr "backcolor ";r;" ";g;" ";b
end sub
sub pset x, y
#gr "set ";x;" ";y
end sub
sub circ x, y, radius
#gr "place ";x;" ";y;"; circle ";radius
end sub
'Need line: #gr "trapclose quit"
sub quit H$
close #gr '<=== this needs Global H$ = "gr"
end 'Thanks Facundo, close graphic wo error
end sub
I am very puzzled and need to have my sanity restored.
I am writing a trivial program in which a ball (represented by a pset point) bounces inside a box. (Planning to use it for more complicated things)
There is gravity (g) which pulls the ball down. (increasing y direction values )
There is a potentially infinite repetition of calculation of new y velocity (yvel) (increased by gravity) changing the y position, and x velocity (xvel) being constant.
When it hits the side it bounces in the other direction.
When it hits the bottom it bounces back up.
It should just go on forever bouncing around.
But for some reason the height of the bounce (y position value) gradually decreases. The y velocity (yvel) gradually decreases.
I cannot see why this happens.
Is it a result of a cumulative effect of rounding numbers when there are many iterations?
global Xmax, Ymax, Pi, Deg, Rad
global g, iters
global xpos, ypos, previousypos, previousypos, xvel, yvel, itersToDo
global previousxvel, previousyvel
global minxpos, maxxpos, minypos, maxypos, iterstodo
global xrange 'difference between x left of box and x right of box
global xshow , yshow
global slownum
global howoftentocls
global multiplier 'i used this to change the values of xpos etc that are being calculated
'then divide by it to change it into values that can be displayed
'it serves no purpose except to see if the problem with the program
'is associated with the size of the numbers being calculated
'are they for 0 to 2 or 0 to 100 to 100 etc
'as far as I can see the problme has nothing to do with the size of the number
ulx = 10 'upper left x value of box on screen
uly = 10
ww =1200
wh =600
WindowWidth = ww
WindowHeight = wh
UpperLeftX = ulx
UpperLeftY = uly
open "Graphic Title" for graphics_nsb_nf as #gr '<======================= title
#gr "setfocus"
#gr "trapclose [quit]"
#gr "when leftButtonUp lButtonUp"
#gr "when characterInput charIn"
#gr "down"
#gr "fill black"
'#gr "trapclose [quit]"
'wait
call back 255,0,255
call fore 255,0,0
b$ = ""
call StartVals
for iters = 1 to iterstodo
call Sloww 'slow things down so it can be seen
call SavePreviousXY
call MoveBall
call BounceSub
call pset xpos/multiplier, ypos/multiplier
'call circ xpos/distancemultipiernum, ypos/distancemultipiernum, 2
'sometimes need to clear display
if iters mod howoftentocls = 1 then
print #gr, "fill black"
end if
next
'============================== sets drawing
#gr "flush"
wait
[quit]
close #gr
end
'***********************************************
sub StartVals
multiplier = 1
slownum= 100 'slow things down
howoftentocls = 3000 'how often to clear the display
iterstodo = 19500
g = .03 * multiplier';gravity constant
'min and max x and y positions. This determines where the ball bounces.
minxpos = 0
maxxpos = 1150 * multiplier',1
minypos = 0
maxypos = 555 * multiplier'1
xpos = 20 * multiplier'0 .200
ypos = 20 * multiplier'0' .200
yvel = 0
xvel =.09 * multiplier
end sub
sub BounceSub
'bounce off walls and bottom
'bounce off left or right wall
if xpos <= minxpos and previousxpos > minxpos then
xvel = xvel * -1
else
if xpos >= maxxpos and previousxpos < maxxpos then
xvel = xvel * -1
end if
end if
'bounce off bottom
if ypos >= maxypos and previousypos < maxypos then
print "now in bounce sub iters",iters
print "previous xvel yvel", previousxvel, previousyvel
print "currenr xvel yvel", xvel, yvel
yvel = yvel * -1
end if
end sub
sub MoveBall
yvel= yvel + g
xpos= xpos + xvel
ypos= ypos + yvel
end sub
sub SavePreviousXY 'probably not actually needed
previousxvel = xvel
previousyvel = yvel
previousxpos = xpos
previousypos = ypos
end sub
sub Sloww' slow things down
for j = 1 to slownum
dummy=1
next
end sub
sub fore r, g, b
#gr "color ";r;" ";g;" ";b
end sub
sub back r, g, b 'backcolor is used for fills
#gr "backcolor ";r;" ";g;" ";b
end sub
sub pset x, y
#gr "set ";x;" ";y
end sub
sub circ x, y, radius
#gr "place ";x;" ";y;"; circle ";radius
end sub
'Need line: #gr "trapclose quit"
sub quit H$
close #gr '<=== this needs Global H$ = "gr"
end 'Thanks Facundo, close graphic wo error
end sub