ok so i just had a physics course at school and folllowing it i made this semi real box physics program.
it is quite simple, there is a box which you can shoot at and it will react to your shots.
press leftmouse to set a shooting location and then rightmouse to aim and shoot.
the code uses some vectormath and a quite usefull ( maybe not the fastest one ) function to determine if a point is inside a rotated box.
it requires Matrix utils and advanced2d plugins. both are optional so if you really dont want do download them you can replace the functions.
Rem Project: box
Rem Created: Thursday, February 09, 2012
Rem ***** Main Source File *****
sync on : sync rate 60
w = screen width() : h = screen height()
global bx#
bx# = w/2
global by#
by# = h/2
global bz# = 0
global br# = 80.0
global bm# = 2000.0
global dim bx#(4) :global dim by#(4) :global dim bz#(4)
for i= 1 to 4
bz#(i) = i*90-90
NEXT
buv = createvector()
bv = createvector()
bus# = 5
c = 255
a2setblendmode 2,2,1
do
mx# = mousex() : my# = mousey()
bx# = bx#+x vector2(bv) : by# = by#+y vector2(bv)
bz# = bz#+m#/100000.0
if bx# > w or bx# < 0 or by# > h or by# < 0 then bx# = w/2 : by# = h/2
for i = 1 to 4
bx#(i) = newxvalue(bx#,bz#+bz#(i),br#)
by#(i) = newzvalue(by#,bz#+bz#(i),br#)
NEXT
for i = 1 to 4
a2line bx#(i),by#(i),bx#(wrap(i-1,1,4)),by#(wrap(i-1,1,4)),rgb(30,180,140)
a2line bx#(i)+1,by#(i)+1,bx#(wrap(i-1,1,4))+1,by#(wrap(i-1,1,4))+1,rgb(30,180,140)
NEXT
a2filltriangle bx#(1),by#(1),bx#(2),by#(2),bx#(3),by#(3),rgb(0,20,40)
a2filltriangle bx#(1),by#(1),bx#(4),by#(4),bx#(3),by#(3),rgb(40,20,0)
inside = checkbox(mx#,my#)
a2circle bux#,buy#,3,rgb(10,bum#,100)
obux# = bux# : obuy# = buy#
bux# = bux#+x vector2(buv) : buy# = buy#+y vector2(buv)
binside = checkbox(bux#,buy#)
if binside = 1
buf# = bum#*bus# : bf# = buf#/bm#
lenvec(buv,bf#)
add vector2 bv,bv,buv
r# = linedist(obux#,obuy#,bux#,buy#,bx#,by#)
m# = m#+buf#*r#
bux# = 0 : buy# = 0 : set vector2 buv,0,0
endif
gosub _input:
a2dot bx#,by#,rgb(255,255,255)
sync
cls
LOOP
_input:
if upkey() then inc bz#
if mouseclick() = 1 and inside = 0
aimx1# = mx# : aimy1# = my#
ENDIF
a2dot aimx1#,aimy1#,rgb(10,255,100)
if mouseclick() = 2 and inside = 0 and aimx1# > 0
aimx2# = mx# : aimy2# = my#
a2line aimx1#,aimy1#,aimx2#,aimy2#,rgb(10,255,100)
ENDIF
if aimx2# > 0 and mouseclick() = 0
bux# = aimx1# : buy# = aimy1#
set vector2 buv,aimx2#-aimx1#,aimy2#-aimy1#
bum# = length vector2(buv)/2.0
lenvec(buv,bus#)
aimx2# = 0 : aimy2# = 0
ENDIF
return
function linedist(x1#,y1#,x2#,y2#,x0#,y0#)
top# = ((x2#-x1#)*(y1#-y0#)-(x1#-x0#)*(y2#-y1#))
bot# = dist(x1#,y1#,x2#,y2#)
dist# = top#/bot#
ENDFUNCTION dist#
function checkbox(x#,y#)
boxz# = atanfull(bx#(1)-bx#(2),by#(1)-by#(2))
rotz# = -boxz#
nbx1# = newxvalue(bx#,bz#+bz#(1)+rotz#,br#) : nby1# = newzvalue(by#,bz#+bz#(1)+rotz#,br#)
nbx2# = newxvalue(bx#,bz#+bz#(3)+rotz#,br#) : nby2# = newzvalue(by#,bz#+bz#(3)+rotz#,br#)
d# = dist(x#,y#,bx#,by#)
z# = atanfull(x#-bx#,y#-by#)
nx# = newxvalue(bx#,z#+rotz#,d#) : ny# = newzvalue(by#,z#+rotz#,d#)
if nx# > nbx2# and nx# < nbx1# and ny# > nby2# and ny# < nby1#
inside = 1
ENDIF
endfunction inside
function dist(x1#,y1#,x2#,y2#)
d# = sqrt((x1#-x2#)^2+(y1#-y2#)^2)
ENDFUNCTION d#
function lenvec(vec,len#)
normalize vector2 vec,vec
multiply vector2 vec,len#
ENDFUNCTION
function createvector()
num = find free vector()
null = make vector2(num)
ENDFUNCTION num