Dark Bit Factory & Gravity
PROGRAMMING => Other languages => Yabasic => Topic started by: bikemadness on January 22, 2007
-
These could be classed as exercises, answers to which you make up a question to.
See if can guess what is going to happen by looking at the code first.
...............
left/right to move, X to action.
open window 640,512
m=0
b=0
yb=200
y=200
ma=20
repeat
setdispbuf vm
vm=1-vm
setdrawbuf vm
clear window
setrgb 1,200,200,0
fill rectangle 0,0 to 640,512
c=peek("port1")
if and(c,128)>0 and m=0 m=-1
if and(c,32)>0 and m=0 m=1
if and(c,16384)>0 and y=200 go=1
if go=1 y=y-10
if y<99 y=99
if y=99 go=0
if go=0 y=y+2
if y>200 y=200
ma=ma+m
if ma<20 ma=20
if ma>600 ma=600
setrgb 1,100,100,100
text 10,20,"position="+str$(int(ma/20))
if m=0 text 130,20,"pointer=stop"
if m<>0 text 130,20,"pointer=go"
text 270,20,"bars="+str$((a-20)/20)
text 370,20,"fire height="+str$(yb-y)+"%"
if go=0 and y<200 text 550,20,"loading"
if go=1 text 550,20,"firing"
if y=200 text 550,20,"loaded"
setrgb 1,256,0,0
fill rectangle ma-8,205 to ma+8,215
setrgb 1,0,0,0
rectangle ma-8,205 to ma+8,215
for a=20 to 600 step 20
if ma=a then
m=0
setrgb 1,0,156,0
fill rectangle a-8,yb to a+8,y
endif
setrgb 1,0,0,0
rectangle a-8,100 to a+8,200
next a
until (1=0)
..................
this took me lots of versions.
open window 640,512
blocks=10
dim blx(blocks),bly(blocks),sqx(blocks),sqy(blocks)
for a=1 to blocks
blx(a)=20
bly(a)=20
sqx(a)=int(ran(5)+1)*120-40
sqy(a)=int(ran(4)+1)*120-40
next a
xb=400
yb=200
xm=2
ym=2
ra=15
repeat
setdispbuf vm
vm=1-vm
setdrawbuf vm
clear window
setrgb 1,200,200,0
fill rectangle 0,0 to 640,512
if sqx<0+blx sqx=0+blx
if sqx>640-blx sqx=640-blx
if sqy<0+bly sqy=0+bly
if sqy>510-bly sqy=510-bly
xb=xb+xm
yb=yb+ym
if xb<0+ra or xb>640-ra xm=-xm
if yb<0+ra or yb>510-ra ym=-ym
for a=1 to blocks
if xb>sqx(a)+blx(a)+ra and xb<sqx(a)+blx(a)+5+ra and yb>sqy(a)-bly(a)-ra and yb<sqy(a)+bly(a)+ra xm=-xm
if xb>sqx(a)-blx(a)-5-ra and xb<sqx(a)-blx(a)-ra and yb>sqy(a)-bly(a)-ra and yb<sqy(a)+bly(a)+ra xm=-xm
if yb>sqy(a)+bly(a)+ra and yb<sqy(a)+bly(a)+5+ra and xb>sqx(a)-blx(a)-ra and xb<sqx(a)+blx(a)+ra ym=-ym
if yb>sqy(a)-bly(a)-5-ra and yb<sqy(a)-bly(a)-ra and xb>sqx(a)-blx(a)-ra and xb<sqx(a)+blx(a)+ra ym=-ym
setrgb 1,0,256,0
fill rectangle sqx(a)-blx(a),sqy(a)-bly(a) to sqx(a)+blx(a),sqy(a)+bly(a)
next a
setrgb 1,200,0,0
fill rectangle xb-ra,yb-ra to xb+ra,yb+ra
setrgb 1,0,0,0
if xm=2 then
text xb-10,yb-5,"+"+str$(xm)
else
text xb-10,yb-5,str$(xm)
endif
if ym=2 then
text xb-10,yb+10,"+"+str$(ym)
else
text xb-10,yb+10,str$(ym)
endif
until (1=0)
Have a Yahappy day.
-
Nice second porgram! Very smart!
Glad to see you over here - haven't seen you much here.
Had no idea what the first one was lol! opps :-[
-
Thanks, just building up the courage to post at a new site.
Used to post over at (cough) ezboard (cough).
My PS2 memory cards are full of ideas, need to transfer them all to P.C easily.
Have a Yahappy day.
-
Hey bike madness I know what you goin through I tried to transfer all my old code from my memory card but I gave up almost 16MB of code and no way I was typin it all over again I just started anew.
I like the approach you took with the collision detection you were going for colliding with the edges as opposed to just hitting a box full stop. Takes me back to an old pool game I made. That kind of collision detection is extremely difficult to do in yabasic. You did it without even knowing the exact angle the sprite was travelling. I'm amazed. took me a long time to figure this same problem out and my solution involved alot of hypotenuse calculations and cosine / sine calcs to do it. Im looking foward to seeing more code from you.
-
You've reminded me of my attempt at colliding balls.
But I was getting too far ahead of myself, with my limited experience at the time.
So I started again with the basics and this is where I left off.
open window 640,512
ang1=pi/180
ang2=180/pi
ra=30
ball=6
dim dirs(ball),xs(ball),ys(ball),ssp(ball)
dim hitang(ball),hit(ball),reang(ball)
for a=1 to ball
dirs(a)=0
read xs(a),ys(a)
ssp(a)=0
next a
dirm=30
xm=500
ym=100
msp=5
repeat
setdispbuf vm
vm=1-vm
setdrawbuf vm
clear window
for a=1 to ball
xs(a)=xs(a)+cos(dirs(a)*ang1)*ssp(a)
ys(a)=ys(a)+sin(dirs(a)*ang1)*ssp(a)
circle xs(a),ys(a),ra
hitang(a)=atan(xs(a)-xm,ym-ys(a))*ang2+180
hit(a)=(abs(xs(a)-xm)^2+abs(ys(a)-ym)^2)^0.5
reang(a)=((dirs(a)+180)-dirm)*2+dirm-180
if hit(a)<ra*2 dirm=reang(a)
dirs(a)=hitang(a)-90
next a
xm=xm+cos(dirm*ang1)*msp
ym=ym+sin(dirm*ang1)*msp
circle xm,ym,ra
if xm<0+ra dirm=270-dirm+270
if xm>640-ra dirm=90-dirm+90
if ym<0+ra dirm=-dirm
if ym>510-ra dirm=180-dirm+180
if dirm>360 dirm=dirm-360
if dirm<0 dirm=dirm+360
until (1=0)
data 252,126,396,126,468,252,396,378,252,378,180,252
Have a Yahappy day
-
Changes in trajectory seems perfect to me. However the collisions are missing one final touch ...
Detecting the collision is one thing but there is one further step that you missed out on, calculating where the ball ends up after a collision if the collision takes place between frames.
Now there is no real need for you to perform this part you have bluffed it very well but this is because the ball is traveling at a relatively slow pace imagine the snooker table, first break the ball travels at really high speed' it hits the pack but because it has traveled so far between frames it actually strikes the pack somewhere near the back row rather than the closest point of the closest ball.
To see what I am talking about insert a wait state in the main loop to slow it down so you can observe what happens and increase mps (meters per second? ; ) I use this too) to a crazy speed like 500 or so the ball will simply pass through all the other balls. You got to stop the ball moving foward at the exact point of contact by using projection.
This is the kind of calculations that raycasting depends on and it does not require too much extra work but the end results speak for themselves. To project the ball note this -
sine = opposite / hypotenuse
cosine = adjacent / hypotenuse
(if you already know the angle then you can manipulate this to -)
opposite = targety - positiony
hypotenuse = opposite / sin(angle)
adjacent = cos(ang) * hypotenuse
or simply this -
opposite = targety - positiony
adjacent = opposite / sin(ang) * cos(ang)
this projects you towards the targets center point. if you calc the distance between projected position and target position a collision can be detected mid frame (hypotenuse < radius*2) you now have more accurate collision detections that can cope with any speed or angle. Then again you have no need for this step in the above code as it is not required. But it is worth taking note of nonetheless.
-
Whew! You know what's really crazy? You've just had your tea and I've just had my breakfast.
I must be the only one in NZ with a passing interest in programming. 99.9% of us just don't get it.
The previous program was good enough for me because I found I get lost in too much code.
But I am still fascinated by what can be done, even in basic, because I understand enough.
Thanks for caring.
Have Yahappy day.
-
Just like here too no one here codes I am the only one in my circle of friends, Thats why this place is great distance and time difference make no odds you can discuss things you are into with others who wont look blankly back at you like you have been speaking latin or somethin. I sent you a personal message a little while ago check it out man.