Here's a box with a lid - hit space to open the box.
#constant player_object = 100
#constant bullet_velocity = 1000
type TBullet
id
start
endtype
global nObjectIndex = 0
global nBulletIndex = 1000
global fspeed as float = 5.0
global fturn as float = 0.03
global oldcamx as float
global oldcamy as float
global camx as float
global camy as float
randomize timer()
phy start
autocam off
phy set gravity 0, -9.8, 0
sync on
sync rate 60
backdrop off
set display mode 1024, 768, 32
set camera range 0.5, 35000
hide mouse
set point light 0, 100, 200, -100
rem ground
ground = GetNextIndex()
make object plain ground, 30000, 30000
color object ground, rgb(20, 120, 20)
xrotate object ground, -90
phy make rigid body static box ground
rem box with lid
id1 = GetNextIndex()
make object box id1, 60, 100, 2
position object id1, 0, 50, -31
color object id1, rgb(255, 0, 0)
` set shadow shading on id1
id2 = GetNextIndex()
make object box id2, 60, 100, 2
position object id2, 0, 50, 31
color object id2, rgb(255, 0, 0)
` set shadow shading on id2
id3 = GetNextIndex()
make object box id3, 2, 100, 60
position object id3, -29, 50, 0
color object id3, rgb(255, 0, 0)
` set shadow shading on id3
id4 = GetNextIndex()
make object box id4, 2, 100, 60
position object id4, 29, 50, 0
color object id4, rgb(255, 0, 0)
` set shadow shading on id4
lid = GetNextIndex()
make object box lid, 60, 2, 64
position object lid, 0, 101, 0
color object lid, rgb(255, 0, 0)
` set shadow shading on lid
phy make rigid body dynamic box id1
phy make rigid body dynamic box id2
phy make rigid body dynamic box id3
phy make rigid body dynamic box id4
phy make rigid body dynamic box lid
remstart
phy set rigid body mass id1, 10
phy set rigid body mass id2, 10
phy set rigid body mass id3, 10
phy set rigid body mass id4, 10
phy set rigid body mass lid, 15
remend
phy make fixed joint 1, id1, id2
phy make fixed joint 2, id2, id3
phy make fixed joint 3, id3, id4
phy make fixed joint 4, id4, id1
phy make revolute joint 5, id2, lid
phy set revolute joint motor 5, 1, 1, 1
phy set revolute joint global axis 5, 0, 1, 0, 0
phy set revolute joint global anchor 5, 0, 0, 100, 31
phy build revolute joint 5
rem Player controller
make object box player_object, 10, 100, 10
phy make box character controller player_object, 10, 101, -200, 10, 100, 10, 1, 16, 45
hide object player_object
rem Light inside
light_object = GetNextIndex()
make light light_object
set point light light_object, 0, 600, 0
value = make vector3( bullet_velocity )
start = 0
dim bullets() as TBullet
empty array bullets()
do
now = timer()
text 10, 20, "Press space to open lid, left click to fire bullets"
text 10, 40, "FPS: " + STR$(screen fps())
text 10, 60, "Camera: " + STR$(object position x(player_object), 2) + " " + STR$(object position y(player_object), 2) + " " + STR$(object position z(player_object), 2)
text 10, 80, "Bullets: " + str$(array count(bullets()))
array index to top bullets()
while array index valid(bullets())
if now - bullets().start > 10000
phy delete rigid body bullets().id
delete object bullets().id
array delete element bullets()
else
next array index bullets()
endif
endwhile
position mouse 512, 384
position camera object position x(player_object), object position y(player_object) + 20, object position z(player_object)
rotate camera object angle x(player_object), object angle y(player_object), object angle z(player_object)
oldcamx = camx
oldcamy = camy
camx = wrapvalue( camx + mousemovey() * 0.4 )
camy = wrapvalue( camy + mousemovex() * 0.4 )
xrotate camera 0, curveangle( camx, oldcamx, 24 )
yrotate object player_object, curveangle( camy, oldcamy, 24 )
key = false
if upkey()
key = true
phy move character controller player_object, 200.0
endif
if downkey()
key = true
phy move character controller player_object, -200.0
endif
if leftkey()
key = 1
camy = wrapvalue( camy - 90 )
yrotate object player_object, camy
phy move character controller player_object, 200.0
camy = wrapvalue( camy + 90 )
yrotate object player_object, camy
endif
if rightkey()
key = 1
camy = wrapvalue( camy + 90 )
yrotate object player_object, camy
phy move character controller player_object, 200.0
camy = wrapvalue( camy - 90 )
yrotate object player_object, camy
endif
if spacekey()
phy set rigid body angular velocity lid, 1, 0, 0
endif
if key = false
phy move character controller player_object, 0.0
endif
if mouseclick() = 1
if now - start > 300
x# = object position x(player_object)
y# = object position y(player_object) + 20
z# = object position z(player_object)
bullet = GetBulletIndex()
make object sphere bullet, 10
color object bullet, rgb(64 + rnd(127), 64 + rnd(127), 64 + rnd(127))
position object bullet, x#, y#, z#
rotate object bullet, camera angle x(), camera angle y(), camera angle z()
move object bullet, 20
x# = object position x(bullet) - x#
y# = object position y(bullet) - y#
z# = object position z(bullet) - z#
set vector3 bullet_velocity, x#, y#, z#
normalize vector3 bullet_velocity, bullet_velocity
x# = x vector3(bullet_velocity)
y# = y vector3(bullet_velocity)
z# = z vector3(bullet_velocity)
phy make rigid body dynamic sphere bullet
phy set rigid body mass bullet, 100
phy set rigid body linear velocity bullet, x# * 1000, y# * 1000, z# * 1000
array insert at bottom bullets()
bullets().id = bullet
bullets().start = now
start = now
endif
endif
phy update
sync
loop
end
function GetNextIndex()
inc nObjectIndex
endfunction nObjectIndex
function GetBulletIndex()
inc nBulletIndex
if nBulletIndex > 1100 then nBulletIndex = 1000
endfunction nBulletIndex
"Reality Bites"