Added some head & rear brake light mods. Not as pretty as could be(the quick & dirty method, just for fun)
LoadVehicle.agc
#constant MOVE_FORWARD=1
#constant MOVE_REVERSE=-1
#constant MOVE_RIGHT=2
#constant MOVE_LEFT=3
#constant CAMERA_1ST=0
#constant CAMERA_3RD=1
#constant CAMERA_1ST_ALLIGNED=2 // so cam moves along with vehicle angle
type camera
style as integer
id as integer
dolly as integer
ease as float
endtype
type delay
amount as float
count as float
endtype
type hit
id as integer
contact as integer
pos as point
endtype
type wheel
pivot as integer
id as integer
fromSensor as integer
toSensor as integer
pos as point
radius as float
width as float
height as float
circumfrence as float
ray as integer
fromVect as integer
toVect as integer
shape as integer
hit as hit
endtype
type velocity
current as float
max as float
last as point
linearUnit as float
angularUnit as float
endtype
type vehicle
chassis as integer
hit as integer
size as point
wheels as wheel[]
forward as integer
reverse as integer
right as integer
left as integer
wheelAngle as float
image as integer
imagebrakes as integer /// added
lastPos as point
camera1st as camera
camera3rd as camera
camera1stalligned as camera
camera as camera
ground as integer
vectV as integer
vectU as integer
suspension as float
flying as integer
delay as delay
velocity as velocity
brakes as integer
// off as integer
endtype
//
// Stick together all the parts for the vehicle
//
// hit.obj The hitbox
// chassis.obj The body of the car
// fr.obj, fl.obj, rr.obj, rl.obj Wheels
// direction (created) Used to get the direction vector for the vehicle
// pivot (created for each wheel) For turning left/right so we can also rotate the wheel to roll
// sensor (created for each wheel) To detect if it is in contact with the ground and to simulate suspension
// cam1 The first person camera position
// dolly1 The first person dolly (Used by the camera to "LookAT")
// cam3 The 3rd person camera position
// dolly3 The 3rd person dolly
//
// Modeling notes
// The vehicle must alwasy point down the Z axis
//
// If you model the car so you have groups named "hit", "chassis", "fr", "fl", "rr", "rl", "cam1", "dolly1", "cam3", "dolly3"
// there is a function in MakeCar.agc called MakeCarFromModel(). It will split up your model into it's variuos pieces
// There is also a vehicle.obj model in the suv folder which demonstrates how to format your model
// The model is just a guideline. You could model a solid car, load it as hit.obj, forget about the wheels and
// chassis and it would work as long as it fits in the envelope of the hit.obj model (for physics purposes)
//
function LoadVehicle(folder as string)
vehicle as vehicle
vehicle.image = LoadImage(folder+"/vehicle.png")
vehicle.imagebrakes = LoadImage(folder+"/vehiclelights.png") ////// added
vehicle.hit = LoadObject(folder+"/hit.obj") // Load the hit box (This does all the work)
SetObjectColor(vehicle.hit, 0, 0xff, 0, 0xff)
SetObjectVisible(vehicle.hit, 0)
vehicle.chassis = LoadObject(folder+"/chassis.obj") // Load the chassis
SetObjectImage(vehicle.chassis, vehicle.image, 1)
SetObjectVisible(vehicle.chassis, 1)
vehicle.forward = CreateObjectPlane(0.5, 0.5) // Direction object so we can get a vector
SetObjectPosition(vehicle.forward, GetObjectX(vehicle.chassis), GetObjectY(vehicle.chassis), GetObjectZ(vehicle.chassis) + 1)
SetObjectVisible(vehicle.forward, 0)
FixObjectToObject(vehicle.forward, vehicle.chassis)
vehicle.reverse = CreateObjectPlane(0.5, 0.5) // reverse object so we can get a vector
SetObjectPosition(vehicle.reverse, GetObjectX(vehicle.chassis), GetObjectY(vehicle.chassis), GetObjectZ(vehicle.chassis) - 1)
SetObjectVisible(vehicle.reverse, 0)
FixObjectToObject(vehicle.reverse, vehicle.chassis)
////// Added...
remstart
Global rearlight
rearlight=CreateObjectBox ( 86,22,12)
FixObjectToObject(rearlight, vehicle.chassis )
SetObjectPosition(rearlight, 0,35,-91)
SetObjectRotation(rearlight, -8,0,0)
SetObjectColor(rearlight,255,255,255,1)
SetObjectTransparency(rearlight, 1)
SetObjectColorEmissive(rearlight,255,255,255)
CreatePointLight( 30,0,5,0, 250, 255, 255, 255)
SetPointLightMode(30,1)
SetPointLightPosition(30,GetObjectX(rearlight) ,GetObjectY(rearlight),GetObjectZ(rearlight) )
SetPointLightColor( 30, 0, 0, 0 ) /// turn off lights by default.
remend
Global rearlightLeft
rearlightLeft=CreateObjectBox ( 86,22,12)
FixObjectToObject(rearlightLeft, vehicle.chassis )
SetObjectPosition(rearlightLeft, -30,35,-91)
SetObjectRotation(rearlightLeft, -8,0,0)
SetObjectColor(rearlightLeft,255,255,255,1)
SetObjectTransparency(rearlightLeft, 1)
SetObjectColorEmissive(rearlightLeft,255,255,255)
CreatePointLight( 30,0,5,0, 110, 255, 255, 255)
SetPointLightMode(30,1)
SetPointLightPosition(30,GetObjectX(rearlightLeft) ,GetObjectY(rearlightLeft),GetObjectZ(rearlightLeft) )
SetPointLightColor( 30, 0, 0, 0 ) /// turn off lights by default.
Global rearlightRight
rearlightRight=CreateObjectBox ( 86,22,12)
FixObjectToObject(rearlightRight, vehicle.chassis )
SetObjectPosition(rearlightRight, 30,35,-91)
SetObjectRotation(rearlightRight, -8,0,0)
SetObjectColor(rearlightRight,255,255,255,1)
SetObjectTransparency(rearlightRight, 1)
SetObjectColorEmissive(rearlightRight,255,255,255)
CreatePointLight( 300,0,5,0, 110, 255, 255, 255)
SetPointLightMode(300,1)
SetPointLightPosition(300,GetObjectX(rearlightRight) ,GetObjectY(rearlightRight),GetObjectZ(rearlightRight) )
SetPointLightColor( 300, 0, 0, 0 ) /// turn off lights by default.
/////////////////////////
Global headlightLeft
headlightLeft=CreateObjectBox ( 20,12,5)
FixObjectToObject(headlightLeft, vehicle.chassis )
SetObjectPosition(headlightLeft, -20,10,65)
SetObjectRotation(headlightLeft, 0,0,0)
SetObjectColor(headlightLeft,255,255,255,200)
SetObjectTransparency(headlightLeft, 1)
SetObjectColorEmissive(headlightLeft,255,255,255)
Global lightConeLeft
lightConeLeft=CreateObjectCone(150,25,32)
FixObjectToObject(lightConeLeft, vehicle.chassis )
SetObjectRotation(lightConeLeft, -65,0,0)
SetObjectPosition(lightConeLeft, -20,-5,95)
SetObjectColor(lightConeLeft,255,255,255,200)
SetObjectTransparency(lightConeLeft, 1)
SetObjectColorEmissive(lightConeLeft,255,255,255)
Global lightLeft
lightLeft=CreateObjectSphere(25,32,32)
FixObjectToObject(lightLeft, lightConeLeft )
SetObjectRotation(lightLeft, -65,0,0)
SetObjectPosition(lightLeft, -5,-45,20)
SetObjectColor(lightLeft,255,255,255,200)
SetObjectTransparency(lightLeft, 1)
SetObjectColorEmissive(lightLeft,255,255,255)
SetObjectVisible(lightLeft,0)
CreatePointLight( 31,0,0,0, 300, 255, 255, 255)
SetPointLightMode(31,1)
SetPointLightPosition(31,GetObjectX(lightLeft) ,GetObjectY(lightLeft),GetObjectZ(lightLeft) )
SetPointLightColor( 31, 0, 0, 0 ) /// turn off lights by default.
////////////////////////
Global headlightRight
headlightRight=CreateObjectBox ( 20,12,5)
FixObjectToObject(headlightRight, vehicle.chassis )
SetObjectPosition(headlightRight, 20,10,65)
SetObjectRotation(headlightRight, 0,0,0)
SetObjectColor(headlightRight,255,255,255,200)
SetObjectTransparency(headlightRight, 1)
SetObjectColorEmissive(headlightRight,255,255,255)
Global lightConeRight
lightConeRight=CreateObjectCone(150,25,32)
FixObjectToObject(lightConeRight, vehicle.chassis )
SetObjectRotation(lightConeRight, -65,0,0)
SetObjectPosition(lightConeRight, 20,-5,95)
SetObjectColor(lightConeRight,255,255,255,200)
SetObjectTransparency(lightConeRight, 1)
SetObjectColorEmissive(lightConeRight,255,255,255)
Global lightRight
lightRight=CreateObjectSphere(25,32,32)
FixObjectToObject(lightRight, lightConeRight )
SetObjectRotation(lightRight, -65,0,0)
SetObjectPosition(lightRight, 5,-45,20)
SetObjectColor(lightRight,255,255,255,200)
SetObjectTransparency(lightRight, 1)
SetObjectColorEmissive(lightRight,255,255,255)
SetObjectVisible(lightRight,0)
CreatePointLight( 32,0,5,0, 300, 255, 255, 255)
SetPointLightMode(32,1)
SetPointLightPosition(32,GetObjectX(lightRight) ,GetObjectY(lightRight),GetObjectZ(lightRight) )
SetPointLightColor( 32, 0, 0, 0 ) /// turn off lights by default.
////////////////////////
vehicle.right = CreateObjectPlane(0.5, 0.5) // right object so we can get a vector
RotateObjectLocalY(vehicle.right, 5)
SetObjectVisible(vehicle.right, 0)
FixObjectToObject(vehicle.right, vehicle.chassis)
vehicle.left = CreateObjectPlane(0.5, 0.5) // left object so we can get a vector
RotateObjectLocalY(vehicle.left, -5)
SetObjectVisible(vehicle.left, 0)
FixObjectToObject(vehicle.left, vehicle.chassis)
vehicle.suspension = 0.4
LoadWheel(folder, "fr.obj", vehicle) // Wheels
LoadWheel(folder, "fl.obj", vehicle)
LoadWheel(folder, "rr.obj", vehicle)
LoadWheel(folder, "rl.obj", vehicle)
if GetFileExists(folder+"/cam1.obj") = TRUE
SetupCameras(folder, vehicle) // Attach dolly and cameras
else
SetupCamera(folder, vehicle)
endif
FixObjectToObject(vehicle.chassis, vehicle.hit)
vehicle.vectU = CreateVector3()
vehicle.vectV = CreateVector3()
GetDimensions(vehicle)
endfunction vehicle
//
// Create thr hitbox for the vehicle
//
function CreateVehicleShape(vehicle ref as vehicle)
w as wheel
pos as integer
rot as integer
size as integer
i as integer
p as point
pos = CreateVector3(0, 0, 0)
rot = CreateVector3(0, 0, 0)
size = CreateVector3(0, 0, 0)
SetObjectShapeCompound(vehicle.hit)
p = GetObjectPosition(vehicle.hit)
SetVector3(pos, p.x, p.y, p.z)
SetVector3(rot, 0, 0, 0)
p = GetObjectSize(vehicle.hit)
SetVector3(size, p.x * 0.5, p.y * 0.5, p.z * 0.5)
AddObjectShapeBox(vehicle.hit, pos, rot, size)
// Wheels as spheres
for i=0 to vehicle.wheels.length
w = vehicle.wheels[i]
p = GetObjectPosition(w.id)
SetVector3(pos, w.pos.x * 0.9, w.pos.y , w.pos.z)
AddObjectShapeSphere( vehicle.hit, pos, w.height)
vehicle.wheels[i] = w
next
DeleteVector3(pos)
DeleteVector3(rot)
DeleteVector3(size)
endfunction
function SetupCamera(folder as string, vehicle ref as vehicle)
p as point
vehicle.camera.dolly = CreateObjectPlane(0.5, 0.5)
SetObjectColor(vehicle.camera.dolly, 0xff, 0, 0, 0xff)
SetObjectVisible(vehicle.camera.dolly, 0)
vehicle.camera.id = LoadObject(folder+"/camera.obj")
SetObjectVisible(vehicle.camera.id, 0)
SetObjectColor(vehicle.camera.id, 0, 0xff, 0, 0xff)
p = GetObjectPosition(vehicle.camera.id)
SetObjectPosition(vehicle.camera.id, -p.x, -p.y, -p.z)
FixObjectPivot(vehicle.camera.id)
SetObjectPosition(vehicle.camera.id, p.x, p.y, p.z)
FixObjectToObject(vehicle.camera.id, vehicle.camera.dolly)
FixObjectToObject(vehicle.camera.dolly, vehicle.chassis)
vehicle.camera.ease = 0.2
SetCameraPosition(1, p.x, p.y, p.z)
endfunction
function SetupCameras(folder as string, vehicle ref as vehicle)
p as point
vehicle.camera1st.style=CAMERA_1ST // =0
vehicle.camera1st.ease = 0.2
vehicle.camera1st.dolly = LoadObject(folder+"/dolly1.obj")
SetObjectVisible(vehicle.camera1st.dolly, 0)
SetObjectColor(vehicle.camera1st.dolly, 0, 0xff, 0, 0xff)
p = GetObjectPosition(vehicle.camera1st.dolly)
SetObjectPosition(vehicle.camera1st.dolly, -p.x, -p.y, -p.z)
FixObjectPivot(vehicle.camera1st.dolly)
SetObjectPosition(vehicle.camera1st.dolly, p.x, p.y, p.z)
vehicle.camera1st.id = LoadObject(folder+"/cam1.obj")
SetObjectColor(vehicle.camera1st.id, 0xff, 0, 0, 0xff)
SetObjectVisible(vehicle.camera1st.id, 0)
p = GetObjectPosition(vehicle.camera1st.id)
SetObjectPosition(vehicle.camera1st.id, -p.x, -p.y, -p.z)
FixObjectPivot(vehicle.camera1st.id)
SetObjectPosition(vehicle.camera1st.id, p.x, p.y, p.z)
FixObjectToObject(vehicle.camera1st.id, vehicle.chassis)
FixObjectToObject(vehicle.camera1st.dolly, vehicle.chassis)
//////////////////////////////////////////////////////////////////////////////////
vehicle.camera3rd.style=CAMERA_3RD // =1
vehicle.camera3rd.ease = 0.2
vehicle.camera3rd.dolly = LoadObject(folder+"/dolly3.obj")
SetObjectColor(vehicle.camera3rd.dolly, 0xff, 0, 0, 0xff)
SetObjectVisible(vehicle.camera3rd.dolly, 0)
p = GetObjectPosition(vehicle.camera3rd.dolly)
SetObjectPosition(vehicle.camera3rd.dolly, -p.x, -p.y, -p.z)
FixObjectPivot(vehicle.camera3rd.dolly)
SetObjectPosition(vehicle.camera3rd.dolly, p.x, p.y, p.z)
vehicle.camera3rd.id = LoadObject(folder+"/cam3.obj")
SetObjectVisible(vehicle.camera3rd.id, 0)
SetObjectColor(vehicle.camera3rd.id, 0, 0xff, 0, 0xff)
p = GetObjectPosition(vehicle.camera3rd.id)
SetObjectPosition(vehicle.camera3rd.id, -p.x, -p.y, -p.z)
FixObjectPivot(vehicle.camera3rd.id)
SetObjectPosition(vehicle.camera3rd.id, p.x, p.y, p.z)
FixObjectToObject(vehicle.camera3rd.id, vehicle.camera3rd.dolly)
FixObjectToObject(vehicle.camera3rd.dolly, vehicle.chassis)
// set default to 3rd mode..
vehicle.camera = vehicle.camera3rd
//SetCameraPosition(1, p.x, p.y, p.z)
endfunction
function LoadWheel(folder as string, name as string, vehicle ref as vehicle)
w as wheel
h as float
w.id = LoadObject(folder+"/"+name) // Load the wheel
SetObjectImage(w.id, vehicle.image, 1)
w.pos = GetObjectPosition(w.id)
w.width = (GetObjectMeshSizeMaxX( w.id, 1 ) - GetObjectMeshSizeMinX( w.id, 1 ))
w.height = (GetObjectMeshSizeMaxY( w.id, 1 ) - GetObjectMeshSizeMinY( w.id, 1 )) // Diameter/Height
w.radius = w.height * 0.5
w.circumfrence = 2 * w.radius * PI // Radius
SetObjectVisible(w.id, 1)
SetObjectCollisionMode(w.id, 0)
SetObjectPosition(w.id, -w.pos.x, -w.pos.y, -w.pos.z) // Move it so it's centered on (0,0,0)
FixObjectPivot(w.id) // Make it's center (0,0,0)
w.pivot = CreateObjectPlane(6, 6) // Create pivot so it can turn left/right
RotateObjectLocalZ(w.pivot, 45)
FixObjectPivot(w.pivot)
SetObjectColor(w.pivot, 0, 0xff, 0, 0xff)
SetObjectVisible(w.pivot, 0)
SetObjectCollisionMode(w.pivot, 0)
w.fromSensor = CreateObjectPlane(4, 4) // Create a fromSensor just below the wheel
RotateObjectLocalZ(w.fromSensor, 45)
FixObjectPivot(w.fromSensor)
SetObjectPosition(w.fromSensor, 0, -w.radius * 0.95, 0)
SetObjectColor(w.fromSensor, 0xff, 0xff, 0, 0xff)
SetObjectVisible(w.fromSensor, 0)
SetObjectCollisionMode(w.fromSensor, 0)
w.toSensor = CreateObjectPlane(4, 4) // Create a toSensor just below the wheel
RotateObjectLocalZ(w.toSensor, 45)
FixObjectPivot(w.toSensor)
SetObjectPosition(w.toSensor, 0, -w.radius * 1.5, 0)
SetObjectColor(w.toSensor, 0xff, 0, 0, 0xff)
SetObjectVisible(w.toSensor, 0)
SetObjectCollisionMode(w.toSensor, 0)
w.hit.id = CreateObjectBox(2, 2, 2) // Create a sensor just below the wheel
// RotateObjectLocalZ(w.hit.id, 45)
FixObjectPivot(w.hit.id)
SetObjectPosition(w.hit.id, 0, -w.radius, 0)
SetObjectColor(w.hit.id, 0xff, 0xff, 0xff, 0xff)
SetObjectVisible(w.hit.id, 0)
SetObjectCollisionMode(w.hit.id, 0)
FixObjectToObject(w.fromSensor, w.pivot) // Stick them together
FixObjectToObject(w.toSensor, w.pivot)
FixObjectToObject(w.id, w.pivot)
FixObjectToObject(w.hit.id, w.pivot)
SetObjectPosition(w.pivot, w.pos.x, w.pos.y, w.pos.z) // Move The pivot back where the wheel was
FixObjectToObject(w.pivot, vehicle.chassis)
w.shape = CreateObjectSphere(w.height, 8, 8)
SetObjectPosition(w.shape, w.pos.x * 0.9, w.pos.y, w.pos.z)
SetObjectColor(w.shape, 0, 0xff, 0, 0xff)
FixObjectToObject(w.shape, vehicle.hit)
SetObjectVisible(w.shape, 0)
w.ray = Create3DPhysicsRay()
w.fromVect = CreateVector3()
w.toVect = CreateVector3()
vehicle.wheels.insert(w)
endfunction
function ToggleVisible(vehicle as vehicle)
v as integer
i as integer
w as wheel
if GetObjectVisible(vehicle.hit) = FALSE or GetObjectVisible(vehicle.chassis) = FALSE
v = TRUE
else
v = FALSE
endif
SetObjectVisible(vehicle.hit, v)
SetObjectVisible(vehicle.chassis, v)
for i=0 to vehicle.wheels.length
w = vehicle.wheels[i]
SetObjectVisible(w.shape, v)
SetObjectVisible(w.id, v)
SetObjectVisible(w.hit.id, abs(v - 1))
SetObjectVisible(w.fromSensor, abs(v - 1))
SetObjectVisible(w.toSensor, abs(v - 1))
SetObjectVisible(w.pivot, abs(v - 1))
next
endfunction
function ToggleHitShape(vehicle as vehicle)
v as integer
i as integer
if GetObjectVisible(vehicle.hit) = FALSE
v = TRUE
else
v = FALSE
endif
SetObjectVisible(vehicle.hit, v)
SetObjectVisible(vehicle.chassis, abs(v-1))
for i=0 to vehicle.wheels.length
SetObjectVisible(vehicle.wheels[i].shape, v)
SetObjectVisible(vehicle.wheels[i].id, abs(v - 1))
next
endfunction
function GetDimensions(vehicle ref as vehicle)
box as box
GetObjectDimensions(vehicle.hit, box)
GetObjectDimensions(vehicle.chassis, box)
GetObjectDimensions(vehicle.wheels[0].id, box)
GetObjectDimensions(vehicle.wheels[2].id, box)
vehicle.size.x = box.x.max - box.x.min
vehicle.size.y = box.y.max - box.y.min
vehicle.size.z = box.z.max - box.z.min
endfunction
function GetObjectPosition(id as integer)
s as point
s.x = GetObjectMeshSizeMinX( id, 1 ) + (( GetObjectMeshSizeMaxX( id, 1 ) - GetObjectMeshSizeMinX( id, 1 ) ) / 2 )
s.y = GetObjectMeshSizeMinY( id, 1 ) + (( GetObjectMeshSizemaxY( id, 1 ) - GetObjectMeshSizeMinY( id, 1 ) ) / 2 )
s.z = GetObjectMeshSizeMinZ( id, 1 ) + (( GetObjectMeshSizemaxZ( id, 1 ) - GetObjectMeshSizeMinZ( id, 1 ) ) / 2 )
endfunction s
function GetObjectSize(id as integer)
s as point
s.x = GetObjectMeshSizeMaxX( id, 1 ) - GetObjectMeshSizeMinX( id, 1 )
s.y = GetObjectMeshSizemaxY( id, 1 ) - GetObjectMeshSizeMinY( id, 1 )
s.z = GetObjectMeshSizemaxZ( id, 1 ) - GetObjectMeshSizeMinZ( id, 1 )
endfunction s
function GetObjectDimensions(id as integer, box ref as box)
s as point
if GetObjectMeshSizeMaxX( id, 1 ) > box.x.max
box.x.max = GetObjectMeshSizeMaxX( id, 1 )
endif
if GetObjectMeshSizemaxY( id, 1 ) > box.y.max
box.y.max = GetObjectMeshSizemaxY( id, 1 )
endif
if GetObjectMeshSizemaxZ( id, 1 ) > box.z.max
box.z.max = GetObjectMeshSizemaxZ( id, 1 )
endif
if GetObjectMeshSizeMinX( id, 1 ) > box.x.min
box.x.min = GetObjectMeshSizeMinX( id, 1 )
endif
if GetObjectMeshSizeMinY( id, 1 ) > box.y.min
box.y.min = GetObjectMeshSizeMinY( id, 1 )
endif
if GetObjectMeshSizeMinZ( id, 1 ) > box.z.min
box.z.min = GetObjectMeshSizeMinZ( id, 1 )
endif
endfunction s
MoveVehicle.agc
//
// Vehicle setup. Mostly for physics
//
function SetupVehiclePhysics(vehicle ref as vehicle, reset as integer)
a as point
y as float
i as integer
vehicle.velocity.max = 52.5 * SCALE_3D
vehicle.velocity.current = 0
vehicle.delay.amount = 0.300
vehicle.velocity.linearUnit = 3.75 * SCALE_3D
vehicle.velocity.angularUnit = 0.125 * SCALE_3D
SetObjectCastShadow(vehicle.chassis, 1) // Shadow stuff
SetObjectReceiveShadow(vehicle.chassis, 1)
for i=0 to vehicle.wheels.length
SetObjectCastShadow(vehicle.wheels[i].id, 1)
next
if reset = TRUE // Full reset at start of game
Delete3DPhysicsBody(vehicle.hit)
SetObjectRotation(vehicle.hit, 0, 0, 0)
SetObjectPosition(vehicle.hit, 0, vehicle.size.y, 0)
else
y = GetObject3DPhysicsLinearVelocityY(vehicle.hit) // In game reset (R key pressed)
Delete3DPhysicsBody(vehicle.hit)
if abs(y) > 200
SetObjectRotation(vehicle.hit, 0, 0, 0)
SetObjectPosition(vehicle.hit, 0, vehicle.size.y, 0)
endif
a.x = GetObjectAngleX(vehicle.hit)
a.y = GetObjectAngleY(vehicle.hit)
a.z = GetObjectAngleZ(vehicle.hit)
SetObjectPosition(vehicle.hit, GetObjectX(vehicle.hit), GetObjectY(vehicle.hit) + (vehicle.size.y * 2), GetObjectZ(vehicle.hit))
SetObjectRotation(vehicle.hit, 0, 0, 0)
RotateObjectLocalY(vehicle.hit, a.y)
endif
Create3DPhysicsDynamicBody(vehicle.hit) // Set out hitbox to be a convex hull
CreateVehicleShape(vehicle)
SetObject3DPhysicsCanSleep(vehicle.hit, 0)
SetObject3DPhysicsDamping( vehicle.hit , 0.2, 0.9 ) // Angular dampening. Works fantastic for steering
SetObject3DPhysicsMaxLinearVelocity( vehicle.hit, vehicle.velocity.max )
SetObject3DPhysicsCanSleep(vehicle.hit, 0)
endfunction
//
// This moves the vehicle
//
function MoveVehicle(vehicle ref as vehicle, env as environment)
t as point
i as integer
v as float
d as integer
turn as float
a as float
l as point
dot as float
if vehicle.delay.count > timer()
SetObjectColor(vehicle.chassis, 0, 0xff, 0xff, 0xff)
exitfunction
endif
SetObjectColor(vehicle.chassis, 0xff, 0xff, 0xff, 0xff)
turn = 0
if GetRawKeyState(39) = 1 // Turn right/left
turn = vehicle.velocity.angularUnit
endif
if GetRawKeyState(37) = 1
turn = -vehicle.velocity.angularUnit
endif
vehicle.wheelAngle = vehicle.wheelAngle * 0.8 // Dampen the wheel angle
for i=0 to vehicle.wheels.length // Check the wheels
inc d, CheckWheels(vehicle, i, env)
next
// If we have less than 3 wheels on the ground then we're in the air
// so turn everything off until we are in contact with the ground again
if vehicle.brakes = TRUE
SetObjectImage(vehicle.chassis, vehicle.imagebrakes, 1)
else
SetObjectImage(vehicle.chassis, vehicle.image, 1)
endif
if d < 3
if GetObject3DPhysicsLinearVelocityY(vehicle.hit) < -(25 * SCALE_3D) // Falling velocity too high, throttle it
a = DistancePoint(CreatePoint(GetObject3DPhysicsLinearVelocityX(vehicle.hit), GetObject3DPhysicsLinearVelocityY(vehicle.hit) * 0.999, GetObject3DPhysicsLinearVelocityZ(vehicle.hit)))
SetObject3DPhysicsLinearVelocity( vehicle.hit, GetObject3DPhysicsLinearVelocityX(vehicle.hit), GetObject3DPhysicsLinearVelocityY(vehicle.hit), GetObject3DPhysicsLinearVelocityZ(vehicle.hit), a * .9)
endif
if vehicle.ground = TRUE // Use colours to see if we are in the air or not
SetObjectColor(vehicle.chassis, 0xff, 0, 0, 0xff)
SetObjectColor(vehicle.hit, 0xff, 0, 0, 0xff)
endif
if vehicle.flying = FALSE // First time in the air damp down velocity
SetObject3DPhysicsDamping( vehicle.hit , 0.5, 0.99 )
endif
vehicle.flying = TRUE
exitfunction
endif
if vehicle.flying = TRUE // Just hit the ground, reset damping
SetObject3DPhysicsDamping( vehicle.hit , 0.2, 0.9 )
endif
vehicle.flying = FALSE
if vehicle.ground = TRUE
SetObjectColor(vehicle.hit, 0, 0xff, 0, 0xff)
SetObjectColor(vehicle.chassis, 0, 0xff, 0, 0xff) // Use colours to reort if we are in the air or not
endif
// Get the linear velocity
l = CreatePoint(GetObject3DPhysicsLinearVelocityX(vehicle.hit), GetObject3DPhysicsLinearVelocityY(vehicle.hit), GetObject3DPhysicsLinearVelocityZ(vehicle.hit))
// Get the angular velocity
a = DistancePoint(CreatePoint(GetObject3DPhysicsAngularVelocityX(vehicle.hit), GetObject3DPhysicsAngularVelocityY(vehicle.hit), GetObject3DPhysicsAngularVelocityZ(vehicle.hit)))
v = 0 // Acceleration
If lightsTurnedOn=1 /// added
SetPointLightColor( 30, 255, 100, 100 ) // rear Left
SetObjectColorEmissive(rearlightLeft,255,255,255)
SetPointLightColor( 300, 255, 100, 100 ) // rear Right
SetObjectColorEmissive(rearlightRight,255,255,255)
SetPointLightColor( 31, 255, 255, 255 ) // front left
SetPointLightColor( 32, 255, 255, 255 ) // front right
SetObjectColorEmissive(headlightLeft,255,255,255)
SetObjectVisible(lightConeLeft,1)
SetObjectColorEmissive(headlightRight,255,255,255)
SetObjectVisible(lightConeRight,1)
Else
SetPointLightColor( 30, 0, 0, 0 )
SetPointLightColor( 300, 0, 0, 0 )
SetPointLightColor( 31, 0, 0, 0 )
SetPointLightColor( 32, 0, 0, 0 )
SetObjectColorEmissive(headlightLeft,50,50,50)
SetObjectVisible(lightConeLeft,0)
SetObjectColorEmissive(headlightRight,50,50,50)
SetObjectVisible(lightConeRight,0)
EndIf
if GetRawKeyState(40) = 1 // down
v = -vehicle.velocity.linearUnit
SetObjectImage(vehicle.chassis, vehicle.imagebrakes, 1) ////// added
Elseif vehicle.brakes = TRUE ////// added
SetObjectImage(vehicle.chassis, vehicle.imagebrakes, 1) ////// added
Else ////// added (normal non-braking texture)
SetObjectImage(vehicle.chassis, vehicle.image, 1) ////// added
EndIf
if GetRawKeyState(38) = 1 // up
v = vehicle.velocity.linearUnit
endif
if vehicle.brakes = TRUE
v = v * 0.9
endif
//SetPointLightPosition(30,GetObjectX(rearlight) ,GetObjectY(rearlight),GetObjectZ(rearlight) )
if vehicle.velocity.current < 0
if abs( vehicle.velocity.current + v ) < ( vehicle.velocity.max * 0.5) // Reverse maximum is
inc vehicle.velocity.current, v
else
vehicle.velocity.current = ( vehicle.velocity.max * -0.5)
endif
else
if abs( vehicle.velocity.current + v ) < vehicle.velocity.max
inc vehicle.velocity.current, v
else
vehicle.velocity.current = vehicle.velocity.max * (vehicle.velocity.current / abs(vehicle.velocity.current))
endif
endif
t = GetVehicleDirectionGround(vehicle, MOVE_FORWARD) // Get the vector of the vehicle
if CheckWalls(vehicle, env, l) = TRUE
exitfunction
endif
SetVector3(vehicle.vectU, l.x, l.y, l.z) // Get the direction
SetVector3(vehicle.vectV, t.x, t.y, t.z)
dot = Round(GetVector3Dot(vehicle.vectU, vehicle.VectV))
if v <> 0
if vehicle.brakes = FALSE
SetObject3DPhysicsLinearVelocity( vehicle.hit, t.x, t.y, t.z, vehicle.velocity.current)
endif
else
if dot <> 0
vehicle.velocity.current = DistancePoint(l) * (dot / abs(dot))
SetObject3DPhysicsLinearVelocity( vehicle.hit, t.x, t.y, t.z, vehicle.velocity.current)
endif
endif
if abs(vehicle.wheelAngle) < 100 and vehicle.delay.count < timer() // turn the wheels
inc vehicle.wheelAngle, turn
SetObjectRotation(vehicle.wheels[0].pivot, 0, vehicle.wheelAngle, 0)
SetObjectRotation(vehicle.wheels[1].pivot, 0, vehicle.wheelAngle, 0)
endif
// Apply the turning force (Dampening is set in SetupVehicle())
// The reason we're checking velocity is because we want a
// tight turning circle when it's slow and a smooth one
// when it's fast
if turn <> 0 and abs(vehicle.velocity.current) > (0.7 * SCALE_3d)
if dot < 0
turn = -turn
endif
if dot <> 0
SetObject3DPhysicsAngularVelocity( vehicle.hit, 0, turn, 0, ((vehicle.velocity.max - vehicle.velocity.current) / (0.75 * scale_3d)) + (vehicle.velocity.current ) / (0.75 * scale_3d))
endif
endif
endfunction
//
// This checks for collision with walls
// If the collision is dramatic it turns everything off for vehicle.delay.amount seconds
// If the collision is just grazing the wall then ignore it
//
function CheckWalls(vehicle ref as vehicle, env as environment, vel as point)
o as integer
b as float
a as float
c as point
v as point
dot as float
r as integer = FALSE
if vehicle.delay.count < timer()
if GetObject3DPhysicsFirstContact( vehicle.hit )
repeat
o = GetObject3DPhysicsContactObjectB()
if o = env.walls
a = DistancePoint(vel)
b = DistancePoint(vehicle.velocity.last)
if a / b < 0.7 // Change in velocity is dramatic so we are not just grazing a wall.
r = TRUE
vehicle.velocity.current=0
vehicle.delay.count = timer() + vehicle.delay.amount // Turn everything off
endif
endif
until GetObject3DPhysicsNextContact() = FALSE
endif
endif
endfunction r
//
// This manages the wheels
// 1. It determines if the wheel is in contact with the ground
// 2. It manages the suspension (Moving the wheel up and down)
//
function CheckWheels(vehicle ref as vehicle, i as integer, env as environment)
l as line
h as point
d as float
m as point
c as integer
w as wheel
v as float
y as float
ease as float = 0.3
p as point
id as integer
w = vehicle.wheels[i]
//
// Manage the rolling of the wheel
//
if vehicle.velocity.current > 0
d = 1
else
d = -1
endif
v = Distance3D(CreatePoint(GetObjectX(vehicle.hit), GetObjectY(vehicle.hit), GetObjectZ(vehicle.hit)), vehicle.lastPos) * d
RotateObjectLocalX(w.id, (v / w.circumfrence) * 360)
//
// Determine if the wheel is touching the ground
// and manage the suspension
//
SetObjectColor(w.toSensor, 0xff, 0, 0, 0xff)
p = w.pos
l.f = GetObjectPos(w.fromSensor)
l.t = GetObjectPos(w.toSensor)
c = ObjectRayCast( env.ground, l.f.x, l.f.y, l.f.z, l.t.x, l.t.y, l.t.z )
w.hit.contact = FALSE
if c <> 0 // Sensor has hit the ground
id = GetObjectRayCastHitID( 0 )
if id <> env.ground
Message("Collision not with ground")
endif
w.hit.pos.x = GetObjectRayCastX(0) : w.hit.pos.y = GetObjectRayCastY(0) :w.hit.pos.z = GetObjectRayCastZ(0)
d = GetObjectRayCastDistance( 0 )
SetObjectPosition(w.id, 0, -d, 0)
SetObjectPosition(w.hit.id, 0, GetObjectY(w.fromSensor) - d, 0)
if d > w.radius // Not touching the ground
d = 0
else
w.hit.contact = TRUE
SetObjectColor(w.toSensor, 0, 0xff, 0, 0xff)
d = 1
endif
else // Not touching the ground
d = 0
l.f = GetObjectLocalPos(w.id)
l.t.y = -(w.radius * vehicle.suspension)
SetObjectPosition(w.id, 0, l.f.y + (l.t.y - l.f.y) * ease, 0)
endif
vehicle.wheels[i] = w
endfunction d
function GetVehicleDirectionAverage(vehicle as vehicle, dir as integer)
v1 as point
v2 as point
v3 as point
v1 = GetVehicleDirectionGround(vehicle, dir)
v2 = GetVehicleDirectionVehicle(vehicle, dir)
v3.x = (v1.x + v2.x) / 2
v3.y = (v1.y + v2.y) / 2
v3.z = (v1.z + v2.z) / 2
endfunction v3
function GetVehicleDirectionGround(vehicle as vehicle, dir as integer)
v as point
f as point
t as point
//
// Select right or left side wheels contact to calculate direction
//
if vehicle.wheels[0].hit.contact = TRUE and vehicle.wheels[2].hit.contact = TRUE
f = vehicle.wheels[2].hit.pos
t = vehicle.wheels[0].hit.pos
elseif vehicle.wheels[1].hit.contact = TRUE and vehicle.wheels[3].hit.contact = TRUE
f = vehicle.wheels[3].hit.pos
t = vehicle.wheels[1].hit.pos
else
Message("Catastrophic error : Wheels aren't touching")
endif
v.x = t.x - f.x : v.y = t.y - f.y : v.z = t.z - f.z
endfunction v
function GetVehicleDirectionVehicle(vehicle as vehicle, dir as integer)
v as point
c as point
d as point
c = GetObjectPos(vehicle.chassis)
select dir
case MOVE_FORWARD
d = GetObjectPos(vehicle.forward)
endcase
case MOVE_REVERSE
d = GetObjectPos(vehicle.reverse)
endcase
case MOVE_RIGHT
d = GetObjectPos(vehicle.right)
endcase
case MOVE_LEFT
d = GetObjectPos(vehicle.left)
endcase
endselect
v = CreatePoint(d.x - c.x, d.y - c.y, d.z - c.z )
endfunction v
main.agc
// Project: Arcade kar physics - Tech demo
// Created: 2018-11-30
// 1.7
// by blink0k
// Added: Vehicle + brake Lights & cube wall.
type line
f as point
t as point
endtype
type environment
ground as integer
walls as integer
sundirection as point
gravity as point
endtype
// If we change the car model then make new bits
// MakeCarFromModel("suv", "vehicle.obj")
// Setup the environment
// Ground
// Logo
// Miscellaneous stuff like sun, gravity etc
//
env as environment
env = SetupEnvironment()
//
// Load the vehicle
//
vehicle as vehicle
vehicle = LoadVehicle("suv")
SetupVehiclePhysics(vehicle, TRUE)
AddObjects(env.ground)
////////////////////////////////////////////////////////////////////////////////
// add some physics boxes..
Create_box_Wall(100, 5, 10, 1, -200, GetObjectY(env.ground), 2500)
////////////////////////////////////////////////////////////////////////////////
// add mirror..
// create texture -----------------------------------
global wht
global i
for i= 1 to 100
wht=makecolor(100,100,100) `mirror glass color
drawbox(150,150,150+i/150,150+i/150,wht,wht,wht,wht,1)
next i
global mir_texture
mir_texture=getimage(0,0,300,300)
global ima : ima = 0 // image object
// CreateRenderImage( imageID, width, height, format, mipmap ) | format - 0=RGBA (32bit), 1=Depth
ima=CreateRenderImage(256,256,0,1)
global mirror : mirror= createobjectbox(0.11*40,0.05*40,0) // mirror object
// SetObjectColor(ima, 255,255,255,100)
fixobjecttoobject(mirror, vehicle.chassis)
setobjectposition(mirror,0,55,20)
// Swap the mirror + image so actually behaves like a mirror.
SetObjectScale(mirror,-1,1,1)
SetObjectCullMode(mirror,2)
SetObjectLightMode(mirror,1) // Turns lighting on when drawing this object.
//////////////////////////////////////////////////////////////////////////////
// added streetlight..
Global lightsTurnedOn
lightsTurnedOn=0
Global StreetLightID // StreetLight..
StreetLightID=CreateObjectCylinder(240,8,480)
SetObjectPosition(StreetLightID,-300,-30,3500) : SetObjectRotation(StreetLightID,0,0,0) // ensure is at correct starting point.
//SetObjectPosition(centerpoleID,-400,20,500) : SetObjectRotation(centerpoleID,0,0,0) // ensure is at correct starting point.
SetObjectColor( StreetLightID ,0,0,0,255)
SetObjectCastShadow(StreetLightID,1)
Lightbulb as integer
Lightbulb=CreateObjectCapsule(40,70,1280)
SetObjectPosition(Lightbulb,-300,240-140,3500)
SetObjectColor(Lightbulb, 255,255,255,225)
SetObjectTransparency(Lightbulb,1)
SetObjectColorEmissive(Lightbulb,1255,1255,1255)
SetObjectCastShadow(Lightbulb,1)
Lightcap as integer
Lightcap= CreateObjectCone(20,80,1280)
SetObjectPosition(Lightcap,-300,280-150,3500)
SetObjectColor(Lightcap, 55,55,55,255)
SetObjectCastShadow(Lightcap,1)
CreatePointLight(33 , 0,5,0, 500, 1255, 1255, 1255)
SetPointLightMode(33,1)
SetPointLightPosition(33,-300,140,3500)
Create3DPhysicsStaticBody(StreetLightID)
SetObjectShapeBox(StreetLightID)
//////////////////////////////////////////////////////////////////////////////
// main
do
if GetRawKeyPressed(82) = 1 // R-Reset
if GetRawKeyState(16)
SetupVehiclePhysics(vehicle, FALSE)
else
SetupVehiclePhysics(vehicle, TRUE)
endif
endif
if GetRawKeyPressed(67) = 1 //C- Cam style
if vehicle.camera.style = CAMERA_1ST
vehicle.camera = vehicle.camera3rd
else
vehicle.camera = vehicle.camera1st
endif
endif
if GetRawKeyPressed(86) = 1 // H-Hit shape
ToggleVisible(vehicle)
endif
if GetRawKeyPressed(72) = 1 // H-Hit shape
ToggleHitShape(vehicle)
endif
if GetRawKeyPressed(71) = 1 // G-Ground contact
if vehicle.ground = FALSE
vehicle.ground = TRUE
else
SetObjectColor(vehicle.chassis, 0xff, 0xff, 0xff, 0xff)
vehicle.ground = FALSE
endif
endif
if GetRawKeyPressed(32) // SPACEBAR-Brakes
SetObject3DPhysicsDamping( vehicle.hit , 0.5, 0 )
endif
if GetRawKeyReleased(32)
SetObject3DPhysicsDamping( vehicle.hit , 0.2, 0.9 )
endif
if GetRawKeyState(32) = 1 // SPACEBAR-Brakes
vehicle.brakes = TRUE
else
vehicle.brakes = FALSE
endif
// camera
if GetRawKeyState(83) = 1 // S-Roll right or SHIFT+S-Sideways right
if GetRawKeyState(16)
MoveObjectLocalX(vehicle.camera.dolly , 5)
else
RotateObjectLocalY(vehicle.camera.dolly , -1)
endif
endif
if GetRawKeyState(65) // A-Roll left or SHIFT+A-Sidways left
if GetRawKeyState(16)
MoveObjectLocalX(vehicle.camera.dolly , -5)
else
RotateObjectLocalY(vehicle.camera.dolly , 1)
endif
endif
if GetRawKeyState(90) = 1 // Z-Roll down or SHIFT+Z-Zoom out
if GetRawKeyState(16)
MoveObjectLocalZ(vehicle.camera.id , -5)
else
RotateObjectLocalX(vehicle.camera.dolly , -1)
endif
endif
if GetRawKeyState(87) // W-Roll up or SHIFT+W-Zoom in
if GetRawKeyState(16)
MoveObjectLocalZ(vehicle.camera.id , 5)
else
RotateObjectLocalX(vehicle.camera.dolly , 1)
endif
endif
If GetRawKeyPressed(76) // L= toggle lights on/off.
lightsTurnedOn=1-lightsTurnedOn
If lightsTurnedOn=1
SetAmbientColor(12,12,12)
SetClearColor(12,12,12)
Else
SetAmbientColor(0x60, 0x60, 0x60)
SetSunColor(0x80, 0x80, 0x80)
SetClearColor(1,128,198)
EndIf
Endif
If GetRawKeyPressed(27) // Esc= exiy
End
Endif
MoveVehicle(vehicle, env) // Move the vehicle
// Save some stats
vehicle.velocity.last = CreatePoint(GetObject3DPhysicsLinearVelocityX(vehicle.hit), GetObject3DPhysicsLinearVelocityY(vehicle.hit), GetObject3DPhysicsLinearVelocityZ(vehicle.hit))
vehicle.lastPos = CreatePoint(GetObjectX(vehicle.hit), GetObjectY(vehicle.hit), GetObjectZ(vehicle.hit))
Step3DPhysicsWorld() // Physics step
// Move the camera
cam as point
cam = CreatePoint(GetCameraX(1), GetCameraY(1), GetCameraZ(1))
vcam as point
vcam = CreatePoint(GetObjectWorldX(vehicle.camera.id ), GetObjectWorldY(vehicle.camera.id ), GetObjectWorldZ(vehicle.camera.id ))
dolly as point
dolly = CreatePoint(GetObjectWorldX(vehicle.camera.dolly ), GetObjectWorldY(vehicle.camera.dolly ), GetObjectWorldZ(vehicle.camera.dolly ))
if vehicle.camera.style = CAMERA_3RD
// Set cam to 3rd person position..
SetCameraPosition(1, cam.x + ((vcam.x - cam.x) * vehicle.camera.ease), cam.y + ((vcam.y - cam.y) * vehicle.camera.ease), cam.z + ((vcam.z - cam.z) * vehicle.camera.ease))
else // if vehicle.camera.style = CAMERA_1ST , 1st person camera..
// *Mirror*
MoveCameraLocalZ(1, -80) // Move cam to rear of vehicle.
RotateCameraLocalY(1, -180.0) // Have cam look to the rear.
SetRenderToImage(ima,-1) // Copy the screen to image
ClearScreen()
Render()
SetRenderToScreen()
RotateCameraLocalY(1, 180.0) // Rotate back to front.
SetObjectLightMap(mirror,ima) // Combine the grabbed image with the white image on the mirror object .
//Set cam to (normal) 1st person position..
SetCameraPosition(1, vcam.x, vcam.y, vcam.z)
endif
SetCameraLookAt(1, dolly.x, dolly.y, dolly.z, 0)
// let the lights follow the vehicle's position..
SetPointLightPosition(30,GetObjectWorldX(rearlightLeft) ,GetObjectWorldY(rearlightLeft),GetObjectWorldZ(rearlightLeft) ) ////// added
SetPointLightPosition(300,GetObjectWorldX(rearlightRight) ,GetObjectWorldY(rearlightRight),GetObjectWorldZ(rearlightRight) ) ////// added
SetPointLightPosition(31,GetObjectWorldX(lightLeft) ,GetObjectWorldY(lightLeft),GetObjectWorldZ(lightLeft) ) ////// added
SetPointLightPosition(32,GetObjectWorldX(lightRight) ,GetObjectWorldY(lightRight),GetObjectWorldZ(lightRight) ) ////// added
// DrawSensors(vehicle) // Draw the sensors if debugging
Print( "Arrows-Move, R-Reset, H-Hit shape, G-Ground contact V-Sensors, L- Lights, SPACEBAR-Brakes Version:"+VERSION +" Mods added: Cubewall, brake- and vehicle lights.:")
Print( "AWSZ-Camera, C-Toggle camera: " + str(vehicle.camera.style) )
Print( "Velocity(last:current)="+str(DistancePoint(vehicle.velocity.last))+":"+str(vehicle.velocity.current)+" ["+str(vehicle.velocity.max)+"] FPS="+str(Round(screenFPS())))
Sync()
loop
function DrawSensors(vehicle as vehicle)
vf as point
vt as point
w as wheel
i as integer
for i=0 to vehicle.wheels.length
w = vehicle.wheels[i]
vf.x = GetScreenXFrom3D(GetObjectWorldX(w.fromSensor), GetObjectWorldY(w.fromSensor), GetObjectWorldZ(w.fromSensor))
vf.y = GetScreenYfrom3D(GetObjectWorldX(w.fromSensor), GetObjectWorldY(w.fromSensor), GetObjectWorldZ(w.fromSensor))
vt.x = GetScreenXFrom3D(GetObjectWorldX(w.toSensor), GetObjectWorldY(w.toSensor), GetObjectWorldZ(w.toSensor))
vt.y = GetScreenYFrom3D(GetObjectWorldX(w.toSensor), GetObjectWorldY(w.toSensor), GetObjectWorldZ(w.toSensor))
DrawLine(vf.x, vf.y, vt.x, vt.y, 0xffff00, 0xffff00)
next
endfunction
//
// Add Objects to crash into
//
function AddObjects(ground as integer)
o as integer
min as point
max as point
i as integer
s as integer = 250
c as integer
d as integer
colors as integer[7] = [0xFF0000, 0x00FF00, 0x0000ff, 0xffff00, 0x6666FF, 0xFF9900, 0x0098FF, 0x9900FF ]
min.x = GetObjectMeshSizeMinX( ground, 1 ) + (s * 2)
min.z = GetObjectMeshSizeMinZ( ground, 1 ) + (s * 2)
max.x = GetObjectMeshSizeMaxX( ground, 1 ) - (s * 2)
max.z = GetObjectMeshSizeMaxZ( ground, 1 ) - (s * 2)
for i=0 to 20
if random(1, 2) = 1
d = random(s -150, s)
o = CreateObjectBox(d, d, d)
SetObjectPosition(o, random2(min.x, max.x), d * 10, random2(min.z, max.z))
Create3DPhysicsDynamicBody(o)
SetObjectShapeBox(o)
else
d = random(s -150, s)
o = CreateObjectSphere(d, 8, 8)
SetObjectPosition(o, random2(min.x, max.x), d * 10, random2(min.z, max.z))
Create3DPhysicsDynamicBody(o)
SetObjectShapeSphere(o)
endif
c = colors[random(0, 7)]
SetObjectCastShadow(o, 1)
SetObject3DPhysicsMass(o, 0.1)
SetObjectColor(o, GetColorRed(c), GetColorGreen(c), GetColorBlue(c), 0xff)
SetObject3DPhysicsRestitution(o, 1)
next
endfunction
Function Create_box_Wall(CubeSize as integer, Rows as integer, Columns as integer, Spacing# as float, pos_x as integer, pos_y as integer, pos_z as integer)
o as integer
c as integer
colors as integer[7] = [0xFF0000, 0x00FF00, 0x0000ff, 0xffff00, 0x6666FF, 0xFF9900, 0x0098FF, 0x9900FF ]
cube_pos_x as integer
cube_pos_y as integer
cube_pos_z as integer
i as integer
j as integer
cube_pos_x=pos_x
cube_pos_y=pos_y
cube_pos_z=pos_z
For j= 1 to Rows
For i= 1 to Columns
o = CreateObjectBox(CubeSize, CubeSize, CubeSize)
SetObjectPosition(o,cube_pos_x, cube_pos_y, cube_pos_z)
cube_pos_x= cube_pos_x+CubeSize+Spacing# // move one cube sideways next time
Create3DPhysicsDynamicBody(o)
SetObjectShapeBox(o)
c = colors[random(0, 7)]
SetObjectCastShadow(o, 1)
SetObject3DPhysicsMass(o, 1.0)
SetObject3DPhysicsRestitution(o, .1)
SetObject3DPhysicsDamping(o, .2,.2)
SetObjectColor(o, GetColorRed(c), GetColorGreen(c), GetColorBlue(c), 220)
SetObjectTransparency(o,1)
Next
cube_pos_x=pos_x
cube_pos_y=cube_pos_y+(CubeSize)*j // move one cube upwards next time
Next
endfunction
//
// Setup everything basically
//
function SetupEnvironment()
check as integer
e as environment
// set window properties
SetWindowTitle( "Arcade kart physics - Tech demo" )
SetWindowSize( 1024, 768, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window
SetVirtualResolution( 1024, 768 ) // Display properties
SetOrientationAllowed( 1, 1, 1, 1 )
SetSyncRate( 30, 0 ) // 30fps
SetScissor( 0,0,0,0 )
UseNewDefaultFonts( 1 )
SetPrintSize(20)
SetCameraRange( 1, 1, 50000 )
e.sundirection = CreatePoint(0.2, -1, 0.2) /// Setup Sun and lighting
SetSunDirection(e.sundirection.x, e.sundirection.y, e.sundirection.z)
SetAmbientColor(0x60, 0x60, 0x60)
SetSunColor(0x80, 0x80, 0x80)
shadowmode as integer
shadowMode = 3 // start with cascade shadow mapping which gives the best quality
SetShadowMappingMode( shadowMode )
SetShadowSmoothing( 1 ) // random sampling
SetShadowMapSize( 2048, 2048 )
SetShadowRange( -1 ) // use the full camera range
SetShadowBias( 0.0012 ) // offset shadows slightly to avoid shadow artifacts
create3DPhysicsWorld(SCALE_3D)
e.gravity = CreatePoint(0, -40, 0)
Set3DPhysicsGravity(e.gravity.x, e.gravity.y, e.gravity.z)
check = LoadImage("check.png")
SetImageWrapU(check, 1)
SetImageWrapV(check, 1)
e.ground = LoadObject("ground.3ds")
SetObjectImage(e.ground, check, 1)
SetObjectMeshUVScale( e.ground, 1, 0, 10, 10)
SetObjectPosition(e.ground, 0, -50, 0)
Create3DPhysicsStaticBody(e.ground)
SetObjectShapeStaticPolygon(e.ground)
SetObjectReceiveShadow( e.ground, 1 )
e.walls = LoadObject("walls.3ds")
SetObjectImage(e.walls, check, 1)
SetObjectMeshUVScale( e.walls, 1, 0, 10, 10)
SetObjectPosition(e.walls, 0, -50, 0)
Create3DPhysicsStaticBody(e.walls)
SetObjectShapeStaticPolygon(e.walls)
// SetObject3DPhysicsRestitution(e.walls, 8)
SetObjectReceiveShadow( e.walls, 1 )
SetObjectColor(e.walls, 0x80, 0x80, 0x80, 0xff)
endfunction e
function CreatePoint(x as float, y as float, z as float)
p as point
p.x = x
p.y = y
p.z = z
endfunction p
function DistancePoint(t as point)
f as point
d as float
f = CreatePoint(0, 0, 0)
d = Distance3D(f, t)
endfunction d
function Distance3D(fp as point, tp as point)
dist as float
dist = sqrt((fp.x - tp.x)^2 + (fp.y - tp.y)^2 + (fp.z - tp.z)^2 )
endfunction dist
function GetObjectLocalPos(id as integer)
pos as point
pos.x = GetObjectX(id)
pos.y = GetObjectY(id)
pos.z = GetObjectZ(id)
endfunction pos
function GetObjectPos(id as integer)
pos as point
pos.x = GetObjectWorldX(id)
pos.y = GetObjectWorldY(id)
pos.z = GetObjectWorldZ(id)
endfunction pos
#include "Constants.agc"
#include "MakeCar.agc"
#include "LoadVehicle.agc"
#include "MoveVehicle.agc"