Quote: "I'm using your Box 2D so I can't use plains!"
I don't follow your reasoning there
You can use whatever you want with Box2D, even this:
sync on : sync rate 60
rem Set resolution to desktop resolution
set display mode desktop width(), desktop height(), 32, 1
autocam off
rem Link a body with a 3D object
type BodyObjectPair
BodyId as integer
ObjectId as integer
endtype
dim BodyObjectPairs() as BodyObjectPair
rem Create the physics world
global world as integer
rem Since 3D units can be any scale we want we'll make them the same as the Box2D units
b2SetScale 1, 180.0/3.1415926535, 1, 1
b2SetScreenTransform screen width()/2, screen height()/2, 20, 1
rem This is a top-down world, so there is effectively no gravity
world = b2CreateWorld(0.0, 0.0, 1)
rem Create an empty static object to attach friction joints to
global ground as integer
ground = b2CreateBody(world, b2BodyType_Static(), 0, 0)
rem Create the car
global car as integer
car = CreateBox(0, 0, 30, 1, 2, 1, 1, b2BodyType_Dynamic(), rgb(0, 255, 0))
position camera 0, -5, -15
rotate camera -30, 0, 0
for i = 0 to 100
CreateBox(rnd(400)*0.1-20.0, rnd(400)*0.1-20.0, rnd(360), (rnd(10)+1)*0.2, (rnd(10)+1)*0.2, (rnd(3)+1)*0.2, 1, b2BodyType_Dynamic(), rgb(255, 0, 0))
next i
for i = 0 to 10
CreateBox(rnd(400)*0.1-20.0, rnd(400)*0.1-20.0, rnd(360), (rnd(10)+1)*0.2, (rnd(10)+1)*0.2, (rnd(3)+1)*0.2, 1, b2BodyType_Static(), rgb(0, 255, 255))
next i
set light range 0, 1000
point light 0, 0, 50, 100
rem Main loop
do
text 0, 0, "FPS: " + str$(screen fps())
text 0, 40, "Arrow keys to move"
text 0, 60, "Space to turn sharply"
text 0, 80, "Enter to show debug info"
rem Control the car
ControlCar()
rem Update physics sim
b2StepWorld world, 1.0/60.0
rem Update 3D objects
UpdatePairs()
rem Make camera follow car
position camera b2GetBodyPositionX(car), b2GetBodyPositionY(car)-5, -15
if returnkey() then b2DrawWorld world, 1
sync
loop
global wheelAngle#
function ControlCar()
rem Find force of engine
f# = (upkey()-downkey())*20.0
rem Find location of front of car in world coordinates
fx# = b2GetBodyWorldPointX(car, 0, 1)
fy# = b2GetBodyWorldPointY(car, 0, 1)
rem Find forward and sideways velocity of car
vx# = b2GetBodyLocalVectorX(car, b2GetBodyLinearVelocityX(car), b2GetBodyLinearVelocityY(car))
vy# = b2GetBodyLocalVectorY(car, b2GetBodyLinearVelocityX(car), b2GetBodyLinearVelocityY(car))
rem Find speed of car to use lighter steering at high speeds
v# = sqrt(vx#*vx#+vy#*vy#)
rem If spacekey is pressed or speed is small, user doesn't want to use light steering
if spacekey() or v# < 1.0 then v# = 1.0
rem Find angle of wheels, and smooth it over time using curvevalue
wheelAngle# = curvevalue((leftkey()-rightkey())*100/v#, wheelAngle#, 10)
rem Find world direction of force to be applied
fdx# = b2GetBodyWorldVectorX(car, vx#*-4.0, f#)
fdy# = b2GetBodyWorldVectorY(car, vx#*-4.0, f#)
rem Apply the force
b2ApplyBodyForce car, fdx#, fdy#, fx#, fy#
rem Apply torque for steering
b2ApplyBodyTorque car, (b2GetBodyAngularVelocity(car)-wheelAngle#*vy#)*-0.01
endfunction
rem Update the positions of the 3D objects to match the 2D bodies
function UpdatePairs()
num = array count(BodyObjectPairs())
for i = 0 to num
bodyId = BodyObjectPairs(i).BodyId
objectId = BodyObjectPairs(i).ObjectId
position object objectId, b2GetBodyPositionX(bodyId), b2GetBodyPositionY(bodyId), object position z(objectId)
rotate object objectId, 0, 0, b2GetBodyAngle(bodyId)
next i
endfunction
function CreateBox(x#, y#, angle#, width#, height#, depth#, density#, bodyType, color)
rem Create the 3D object part
objectId = FindFreeObject()
make object box objectId, width#, height#, depth#
position object objectId, 0, 0, depth#*-0.5
color object objectId, color
rem Create the 2D body part
bodyId = b2CreateBody(world, bodyType, x#, y#, angle#)
rem Add box shape
shapeId = b2CreatePolygonShapeAsBox(width#, height#)
fixtureId = b2CreateFixture(bodyId, shapeId, density#, 0.2, 0.4)
b2DeleteShape shapeId
if bodyType = b2BodyType_Dynamic()
rem Simulate air resistance
b2SetBodyLinearDamping bodyId, 0.3
b2SetBodyAngularDamping bodyId, 0.05
rem Simulate friction
joint = b2CreateFrictionJoint(world, ground, bodyId, x#, y#)
b2SetFrictionJointMaxForce joint, 0.1
b2SetFrictionJointMaxTorque joint, 0.05
endif
array insert at bottom BodyObjectPairs()
id = array count(BodyObjectPairs())
BodyObjectPairs(id).BodyId = bodyId
BodyObjectPairs(id).ObjectId = objectId
endfunction bodyId
rem Search for an unused object ID
function FindFreeObject()
id = rnd(100)+1
while object exist(id)
inc id
endwhile
endfunction id
I'll see if I can make an example of additive blending
[b]
