Hi all,
I was struggling with this all night and I finally came up with a solution for rotating an object (like a car) to fit the deformations of a landscape (particularly an .x file).
Here is the code. No media necessary.
Rem Project: Freight Runner Ver 2
Rem Created: 10/25/2005 12:32:14 AM
Rem ***** Main Source File *****
sync on : sync rate 30
make object cube 1, 100
make object cube 2, 50
make object cone 100, 100
scale object 100,1000, 125, 1000
position object 100, 500,50,500
make matrix 1,1000,1000,10,10
move camera -300
do
controlVehicle(1,10,5)
setFakeVehicle(1,2,100)
gravityForObject(1,2)
gravityForObject(2,2)
objectCollisionHandler(1, 100, 50, 50, 10)
objectCollisionHandler(2, 100, 50, 50, 10)
groundDeformation(1, 2)
setZeroPoint(1,0)
setZeroPoint(2,0)
text 0,0, str$(object angle x(1))
text 0,15,str$(object angle x(2))
text 0,100, str$(object position x(1))
text 0,115, str$(object position x(2))
`resetObject(1,0)
if spacekey() = 1 THEN xrotate object 1,0
sync
loop
function resetObject(objNum,decVal)
`RESET VEHICLE X ROTATION
objAngX# = object angle x(objNum)
if objAngX# > 0 THEN xrotate object objNum, objAngX#+decVal
if objAngX# < 0 THEN xrotate object objNum, objAngX#-decVal
endfunction
function gravityForObject(objectNum, gravityValue)
posX# = object position x(objectNum)
posY# = object position y(objectNum)
posZ# = object position z(objectNum)
position object objectNum, posX#, posY#-gravityValue, posZ#
endfunction
function setZeroPoint(objNum,pointVal)
`won't let an object fall below zero on the y axis
if object position y(objNum) < 0 THEN position object objNum, object position x(objNum), pointVal, object Position z(objNum)
endfunction
function setFakeVehicle(objNum, fakeVehNum, speed)
vehicleNumAngX# = object angle x(objNum)
vehicleNumAngY# = object angle y(objNum)
vehicleNumAngZ# = object angle z(objNum)
rotate object fakeVehNum, vehicleNumAngX#, vehicleNumAngY#, vehicleNumAngZ#
vehicleNumPosX# = object position x(objNum)
vehicleNumPosY# = object position y(objNum)
vehicleNumPosZ# = object position z(objNum)
position object fakeVehNum, vehicleNumPosX#, vehicleNumPosY#, vehicleNumPosZ#
move object fakeVehNum, speed
endfunction
function controlVehicle(objNum,speed,angSpeed)
if upkey() = 1 THEN move object objNum, speed
if downkey() = 1 THEN move object objNum, speed*-1
yAngle# = object angle y(objNum)
if rightkey() = 1 THEN yrotate object objNum, yAngle# + angSpeed
if leftkey() = 1 THEN yrotate object objNum, yAngle# - angSpeed
endfunction
function groundDeformation(vehicleNum, fakeVehNum)
`PROCEED WITH REST OF FUNCTION
fakeVehNumX# = object position x(fakeVehNum)
fakeVehNumY# = object position y(fakeVehNum)
fakeVehNumZ# = object position z(fakeVehNum)
`vehicleNumAngX# = object angle x(vehicleNum)
vehicleNumAngY# = object angle y(vehicleNum)
vehicleNumAngZ# = object angle z(vehicleNum)
point object vehicleNum, fakeVehNumX#, fakeVehNumY#, fakeVehNumZ#
`newAngPosX# = object angle x(i)
newAngPosX# = object angle x(vehicleNum)
`newAngPosZ# = object angle z(i)
rotate object vehicleNum, newAngPosX#,vehicleNumAngY#,vehicleNumAngZ#
endfunction
function objectCollisionHandler(objectNum, mapNum, radius#, floor#, ceiling#)
`Step 1: collect current position
positionX# = object position x(objectNum)
positionY# = object position y(objectNum)
positionZ# = object position z(objectNum)
`Step 2: Check the Vectors
vectorRight# = INTERSECT OBJECT(mapNum, positionX#, positionY#, positionZ#, positionX#+radius#, positionY#, positionZ#)
vectorLeft# = INTERSECT OBJECT(mapNum, positionX#, positionY#, positionZ#, positionX#-radius#, positionY#, positionZ#)
vectorForward# = INTERSECT OBJECT(mapNum, positionX#, positionY#, positionZ#, positionX#, positionY#, positionZ#+radius#)
vectorBackward# = INTERSECT OBJECT(mapNum, positionX#, positionY#, positionZ#, positionX#, positionY#, positionZ#-radius#)
vectorForwardRight# = INTERSECT OBJECT(mapNum, positionX#, positionY#, positionZ#, positionX#+radius#, positionY#, positionZ#+radius#)
vectorForwardLeft# = INTERSECT OBJECT(mapNum, positionX#, positionY#, positionZ#, positionX#-radius#, positionY#, positionZ#+radius#)
vectorBackwardRight# = INTERSECT OBJECT(mapNum, positionX#, positionY#, positionZ#, positionX#+radius#, positionY#, positionZ#-radius#)
vectorBackwardLeft# = INTERSECT OBJECT(mapNum, positionX#, positionY#, positionZ#, positionX#-radius#, positionY#, positionZ#-radius#)
vectorUp# = INTERSECT OBJECT(mapNum, positionX#, positionY#, positionZ#, positionX#, positionY#+ceiling#, positionZ#)
vectorDown# = INTERSECT OBJECT(mapNum, positionX#, positionY#, positionZ#, positionX#, positionY#-floor#, positionZ#)
`Step 2 and a half: Check these values for zero and re-assign values if necessary
if vectorRight# = 0 THEN vectorRight# = radius#
if vectorLeft# = 0 THEN vectorLeft# = radius#
if vectorForward# = 0 THEN vectorForward# = radius#
if vectorBackward# = 0 THEN vectorBackward# = radius#
if vectorForwardRight# = 0 THEN vectorForwardRight# = radius#
if vectorForwardLeft# = 0 THEN vectorForwardLeft# = radius#
if vectorBackwardRight# = 0 THEN vectorBackwardRight# = radius#
if vectorBackwardLeft# = 0 THEN vectorBackwardLeft# = radius#
if vectorUp# = 0 THEN vectorUp# = ceiling#
if vectorDown# = 0 THEN vectorDown# = floor#
`Step 3: Get info from the vector data
if vectorRight# <= radius# THEN additionValueX# = (radius# - vectorRight#)
if vectorLeft# <= radius# THEN additionValueXTwo# = radius# - vectorLeft#
if vectorForward# <= radius# THEN additionValueZ# = (radius# - vectorForward#)
if vectorBackward# <= radius# THEN additionValueZTwo# = radius# - vectorBackward#
if vectorForwardRight# = 0 THEN additionValueForwardRight# = radius# - vectorForwardRight#
if vectorForwardLeft# = 0 THEN additionValueForwardLeft# = radius# - vectorForwardLeft#
if vectorBackwardRight# = 0 THEN additionValueBackwardRight# = radius# - vectorBackwardRight#
if vectorBackwardLeft# = 0 THEN additionValueBackwardLeft# = radius# - vectorBackwardLeft#
if vectorUp# <= ceiling# THEN additionValueY# = (ceiling# - vectorUp#)
if vectorDown# <= floor# THEN additionValueYTwo# = floor# - vectorDown#
`Step 3 and a half: Re-assign for the closest vector
if additionValueX# > additionValueXTwo# THEN finalValueX# = additionValueX#*-1 ELSE finalValueX# = additionValueXTwo#
if additionValueY# > additionValueYTwo# THEN finalValueY# = additionValueY#*-1 ELSE finalValueY# = additionValueYTwo#
if additionValueZ# > additionValueZTwo# THEN finalValueZ# = additionValueZ#*-1 ELSE finalValueZ# = additionValueZTwo#
if additionValueForwardRight# > additionValueBackwardLeft#
finalValueDiagOne# = additionValueForwardRight#*-1
addOnX# = finalValueDiagOne#/2
addOnZ# = finalValueDiagOne#/2
ELSE
finalValueDiagOne# = additionValueBackwardLeft#
addOnX# = finalValueDiagOne#/2
addOnZ# = finalValueDiagOne#/2
endif
if additionValueForwardLeft# > additionValueBackwardRight#
finalValueDiagTwo# = additionValueForwardLeft#
addOnX# = finalValueDiagTwo#/2
addOnZ# = (finalValueDiagTwo#/2)*-1
ELSE
finalValueDiagTwo# = additionValueBackwardRight#
addOnX# = (finalValueDiagOne#/2)*-1
addOnZ# = finalValueDiagOne#/2
endif
`Step 4: Apply info from the vector data to the object's position
posX# = object position x(objectNum)
posY# = object position y(objectNum)
posZ# = object position z(objectNum)
position object objectNum, posX#+finalValueX#+addOnX#, posY#+finalValueY#, posZ#+finalValueZ#+addOnZ#
endfunction
I hope it works for you to. I'm going to be using it in my next game: Freight Runner II.
Thanks
go to my website to download cool new games:
www.fishbrosentertainment.com