Quote: "I look forward to the Ai code if you post."
Its quite simple really it just places two block in front of the car
hides them but uses them to check if it collides with the ground
if one of the blocks isn't touching it means the ai cars have to turn
this code checks to see if the collisionblocks are touching the race track
function checkAICollision(objID as integer,objID2 as integer)
local width# as float
local height# as float
local depth# as float
local x# as float
local y# as float
local z# as float
local start_x# as float
local start_y# as float
local start_z# as float
local end_x# as float
local end_y# as float
local end_z# as float
local object_Hit as integer
//this checks for colliion between an object with any other object and returns its id
width#=(GetObjectMeshSizeMaxX(objID,1)-GetObjectMeshSizeMinX(objID,1))/2
height#=(GetObjectMeshSizeMaxX(objID,1)-GetObjectMeshSizeMinX(objID,1))
depth#=(GetObjectMeshSizeMaxX(objID,1)-GetObjectMeshSizeMinX(objID,1))/2
x#=getObjectWorldX(objID)
y#=GetObjectWorldY(objID)
z#=getObjectWorldZ(objID)
// calculate the start of the ray cast
start_x# = x#-width#
start_y# = y#+height#
start_z# = z#-depth#
// calculate the end of the ray cast
end_x# = x#+width#
end_y# = y#-height#
end_z# = z#+depth#
// determine which object has been hit
object_Hit = ObjectRayCast(objID2,start_x#,start_y#,start_z#,end_x#,end_y#,end_z#)
endfunction object_Hit
in the main.agc file i added this inbetween the do loop you will note I have an array of AI opponents
for carNum= 0 to AIVehicle.length
MoveAIVehicle(AIvehicle[carNum], env) // Move the AI vehicle
if checkAICollision(AIvehicle[carNum].hit,env.grass) then SetupVehiclePhysics(AIvehicle[carNum], TRUE,0,0)
next carNum
// 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))
for carNum= 0 to AIVehicle.length
AIvehicle[carNum].velocity.last = CreatePoint(GetObject3DPhysicsLinearVelocityX(AIvehicle[carNum].hit), GetObject3DPhysicsLinearVelocityY(AIvehicle[carNum].hit), GetObject3DPhysicsLinearVelocityZ(AIvehicle[carNum].hit))
AIvehicle[carNum].lastPos = CreatePoint(GetObjectX(AIvehicle[carNum].hit), GetObjectY(AIvehicle[carNum].hit), GetObjectZ(AIvehicle[carNum].hit))
next carNum
The AI Movement function
function MoveAIVehicle(AIvehicle 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 AIvehicle.delay.count > timer()
//SetObjectColor(vehicle.chassis, 0, 0xff, 0xff, 0xff)
exitfunction
endif
turn = 0
if checkAICollision(AIvehicle.collisionLeft,env.tracks[env.selectedTrack])=0 then turn=AIvehicle.velocity.angularUnit
if checkAICollision(AIvehicle.collisionRight,env.tracks[env.selectedTrack])=0 then turn=-AIvehicle.velocity.angularUnit
AIvehicle.wheelAngle = AIvehicle.wheelAngle * 0.8 // Dampen the wheel angle
for i=0 to AIvehicle.wheels.length // Check the wheels
inc d, CheckWheels(AIvehicle, i, env)
next
// print("Wheels="+str(d))
// If we have less than 3 wheels on the ground then we're in the air
if d < 3
if GetObject3DPhysicsLinearVelocityY(AIvehicle.hit) < -(25 * SCALE_3D) // Falling velocity too high, throttle it
a = DistancePoint(CreatePoint(GetObject3DPhysicsLinearVelocityX(AIvehicle.hit), GetObject3DPhysicsLinearVelocityY(AIvehicle.hit) * 0.999, GetObject3DPhysicsLinearVelocityZ(AIvehicle.hit)))
SetObject3DPhysicsLinearVelocity( AIvehicle.hit, GetObject3DPhysicsLinearVelocityX(AIvehicle.hit), GetObject3DPhysicsLinearVelocityY(AIvehicle.hit), GetObject3DPhysicsLinearVelocityZ(AIvehicle.hit), a * .9)
endif
if AIvehicle.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 AIvehicle.flying = FALSE // First time in the air damp down velocity
SetObject3DPhysicsDamping( AIvehicle.hit , 0.5, 0.99 )
endif
AIvehicle.flying = TRUE
exitfunction
endif
if AIvehicle.flying = TRUE // Just hit the ground, reset damping
SetObject3DPhysicsDamping( AIvehicle.hit , 0.2, 0.9 )
endif
AIvehicle.flying = FALSE
if AIvehicle.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(AIvehicle.hit), GetObject3DPhysicsLinearVelocityY(AIvehicle.hit), GetObject3DPhysicsLinearVelocityZ(AIvehicle.hit))
// Get the angular velocity
a = DistancePoint(CreatePoint(GetObject3DPhysicsAngularVelocityX(AIvehicle.hit), GetObject3DPhysicsAngularVelocityY(AIvehicle.hit), GetObject3DPhysicsAngularVelocityZ(AIvehicle.hit)))
v = 0 // Acceleration
// if GetRawKeyState(40) = 1
// v = -AIvehicle.velocity.linearUnit
// endif
// if GetRawKeyState(38) = 1
v = AIvehicle.velocity.linearUnit
// endif
// if vehicle.brakes = TRUE
// v = v * 0.9
// endif
if AIvehicle.velocity.current < 0
if abs( AIvehicle.velocity.current + v ) < ( AIvehicle.velocity.max * 0.5) // Reverse maximum is
inc AIvehicle.velocity.current, v
else
AIvehicle.velocity.current = ( AIvehicle.velocity.max * -0.5)
endif
else
if abs( AIvehicle.velocity.current + v ) < AIvehicle.velocity.max
inc AIvehicle.velocity.current, v
else
AIvehicle.velocity.current = AIvehicle.velocity.max * (AIvehicle.velocity.current / abs(AIvehicle.velocity.current))
endif
endif
t = GetVehicleDirectionGround(AIvehicle) // Get the vector of the vehicle
//if CheckWalls(vehicle, env, l) = TRUE
// exitfunction
//endif
SetVector3(AIvehicle.vectU, l.x, l.y, l.z) // Get the direction
SetVector3(AIvehicle.vectV, t.x, t.y, t.z)
dot = Round(GetVector3Dot(AIvehicle.vectU, AIvehicle.VectV))
if v <> 0
if AIvehicle.brakes = FALSE
SetObject3DPhysicsLinearVelocity( AIvehicle.hit, t.x, t.y, t.z, AIvehicle.velocity.current)
endif
else
if dot <> 0
AIvehicle.velocity.current = DistancePoint(l) * (dot / abs(dot))
SetObject3DPhysicsLinearVelocity( AIvehicle.hit, t.x, t.y, t.z, AIvehicle.velocity.current)
endif
endif
if abs(AIvehicle.wheelAngle) < 100 and AIvehicle.delay.count < timer() // turn the wheels
inc AIvehicle.wheelAngle, turn
SetObjectRotation(AIvehicle.wheels[0].pivot, 0, AIvehicle.wheelAngle, 0)
SetObjectRotation(AIvehicle.wheels[1].pivot, 0, AIvehicle.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(AIvehicle.velocity.current) > (1 * SCALE_3d)
if dot < 0
turn = -turn
endif
if dot <> 0
if AIvehicle.steering = STEERING_GLOBAL
l = GetVehicleTurn(AIvehicle, turn)
else
l.x = 0 : l.y = turn : l.z = 0
endif
SetObject3DPhysicsAngularVelocity( AIvehicle.hit, l.x, l.y, l.z, ((AIvehicle.velocity.max - AIvehicle.velocity.current) / (0.8 * scale_3d)) + (AIvehicle.velocity.current ) / (0.75 * scale_3d))
endif
endif
*/
if turn <> 0 and abs(AIvehicle.velocity.current) > 50 // Apply the turning force (Dampening is set in SetupVehicle()
SetObject3DPhysicsAngularVelocity( AIvehicle.hit, 0, turn, 0, ((AIvehicle.velocity.max - AIvehicle.velocity.current) / 40) + (AIvehicle.velocity.current * 1.2) / 25)
endif
endfunction
in the load vehicle i defined the blocks the ai.obj is just a square but I was having troube with slaing and raycasting so I made it an object
vehicle.collisionLeft=loadObject("AI.obj")
SetObjectPosition(vehicle.collisionLeft, GetObjectX(vehicle.chassis)-250, GetObjectY(vehicle.chassis)-20, GetObjectZ(vehicle.chassis) + 900)
SetObjectScale(vehicle.CollisionLeft,5,5,5)
SetObjectVisible(vehicle.collisionLeft,0)
FixObjectToObject(vehicle.collisionLeft,vehicle.chassis)
vehicle.collisionRight=loadObject("AI.obj")
SetObjectPosition(vehicle.collisionRight, GetObjectX(vehicle.chassis)+250, GetObjectY(vehicle.chassis)-20, GetObjectZ(vehicle.chassis) + 900)
SetObjectScale(vehicle.CollisionRight,5,5,5)
SetObjectVisible(vehicle.collisionRight,0)
FixObjectToObject(vehicle.collisionRight,vehicle.chassis)
the vehicle type has these added
////////////////////////////////////
mirror as integer
mirrorImg as integer
collisionLeft as integer
collisionRight as integer
imgLights as integer
imgBrake as integer
imgHazard as integer
imgLeft as integer
imgRight as integer
wheelImage as integer
/////////////////////////////////
Video removed for space
fubar