Minulla on sattumoisin marcoderin autofysiikkakoodi tallennettuna koneelle. Voinkin tässä sen uppiakkin:
Code: Select all
Const WWIDTH = 5 // Renkaan leveys
Const WLENGTH = 15 // Renkaan pituus
Const WHEELBASE = 58 // Akseliväli
Const RAILGAUGE = 35 // Raideleveys
Const MAXSPEED = 7
Const MAXBSPEED = 3 // Auton maksimi peruutusnopeus
Const STEERSPEED# = 2.5 // Ohjauksen nopeus
Const STEERSPEED2# = 0.2 // Ohjauksen nopeus kun auto kulkee huippunopeutta
Const MAXSTEER = 35 // Maksimi ohjauskulma
Const ACCELERATION# = 0.02 // Kiihtyvyys
Const BACCELERATION# = 0.2 // Peruutuskiihtyvyys
Const SLOWDOWN# = 0.1 // Hidastuvuus
Const WHEELHOLD = 0.5//Renkaiden pito0
// Asetetaan vakiot joilla viitataan taulukon elementteihin
Const CAR_X = 1 // Auton position X
Const CAR_Y = 2 // Auton positio Y
Const CAR_ANGLE = 3 // Auton kulma
Const CAR_STEER = 4 // Ohjauskulma
Const CAR_SPEED = 5 // Nopeus
Const CAR_FIELDS = 5 // Taulukon kenttien lukumäärä
Dim CarTable#(0, CAR_FIELDS)
gNumCars = 0
Global gx1#, gy1#, gx2#, gy2#, gx3#, gy3#, gx4#, gy4#
// Luodaan auto
car1 = AddCar(0, 0, 0)
objCar = LoadObject("..\media\car.bmp", 180)
// Luodaan toinen auto
car2 = AddCar(0, 100, 0)
// Nurtsia taustalle
grass=MakeObjectFloor()
lawn=LoadImage("..\Media\grass.bmp")
PaintObject grass,lawn
DrawToWorld ON
Color 0, 0, 0
Repeat
CarPhysic(car1, UpKey(), DownKey(), LeftKey(), RightKey())
CarPhysic(car2, KeyDown(cbKeyW), KeyDown(cbKeyS), KeyDown(cbKeyA), KeyDown(cbKeyD))
PositionObject objCar, CarTable(car1, CAR_X), CarTable(car1, CAR_Y)
RotateObject objCar, -CarTable(car1, CAR_ANGLE)
CloneCameraPosition objCar
DrawGame
DrawCars()
Text 0, 60, "Speed: " + CarTable(1, CAR_SPEED)
DrawScreen
Forever
Function AddCar(x, y, angle)
gNumCars + 1
ClearArray OFF
ReDim CarTable(gNumCars, CAR_FIELDS)
CarTable(gNumCars, CAR_X) = x
CarTable(gNumCars, CAR_Y) = y
CarTable(gNumCars, CAR_ANGLE) = angle
CarTable(gNumCars, CAR_STEER) = 0
CarTable(gNumCars, CAR_SPEED) = 0
Return gNumCars
End Function
Function CarPhysic(carID, bAccelerate, bBackup, bLeft, bRight)
// Ohjauksen nopeus muuttuu nopeuden mukaan
p# = 100 / MAXSPEED * CarTable(carID, CAR_SPEED)
s# = STEERSPEED - (STEERSPEED - STEERSPEED2) / 100 * p
// Vasemmalle
If bLeft = True And bRight = False Then
If CarTable(carID, CAR_STEER) > 0 Then s = STEERSPEED * 2
CarTable(carID, CAR_STEER) = CarTable(carID, CAR_STEER) - s
CarTable(carID, CAR_STEER) = Max(CarTable(carID, CAR_STEER), -MAXSTEER)
EndIf
// Oikealle
If bLeft = False And bRight = True Then
If CarTable(carID, CAR_STEER) < 0 Then s = STEERSPEED * 2
CarTable(carID, CAR_STEER) = CarTable(carID, CAR_STEER) + s
CarTable(carID, CAR_STEER) = Min(CarTable(carID, CAR_STEER), MAXSTEER)
EndIf
// Oikaisu eli ei ohjata
If bLeft = False And bRight = False Then
If CarTable(carID, CAR_STEER) > 0 Then
CarTable(carID, CAR_STEER) = CarTable(carID, CAR_STEER) - STEERSPEED * 2
CarTable(carID, CAR_STEER) = Max(CarTable(carID, CAR_STEER), 0)
EndIf
If CarTable(carID, CAR_STEER) < 0 Then
CarTable(carID, CAR_STEER) = CarTable(carID, CAR_STEER) + STEERSPEED * 2
CarTable(carID, CAR_STEER) = Min(CarTable(carID, CAR_STEER), 0)
EndIf
EndIf
// Kiihdytys
If bAccelerate = True And bBackup = False Then
CarTable(carID, CAR_SPEED) = CarTable(carID, CAR_SPEED) + ACCELERATION
CarTable(carID, CAR_SPEED) = Min(CarTable(carID, CAR_SPEED), MAXSPEED)
EndIf
// Peruutus
If bAccelerate = False And bBackup = True Then
CarTable(carID, CAR_SPEED) = CarTable(carID, CAR_SPEED) - BACCELERATION
CarTable(carID, CAR_SPEED) = Max(CarTable(carID, CAR_SPEED), -MAXBSPEED)
EndIf
// Hidastus
If bAccelerate = False And bBackup = False Then
If CarTable(carID, CAR_SPEED) > 0 Then
CarTable(carID, CAR_SPEED) = CarTable(carID, CAR_SPEED) - SLOWDOWN
CarTable(carID, CAR_SPEED) = Max(CarTable(carID, CAR_SPEED), 0)
EndIf
If CarTable(carID, CAR_SPEED) < 0 Then
CarTable(carID, CAR_SPEED) = CarTable(carID, CAR_SPEED) + SLOWDOWN
CarTable(carID, CAR_SPEED) = Min(CarTable(carID, CAR_SPEED), 0)
EndIf
EndIf
a# = CarTable(carID, CAR_ANGLE) - 90
// Lasketaan etuakselin keskipiste
fax# = CarTable(carID, CAR_X) - (Sin(a) * WHEELBASE / 2)
fay# = CarTable(carID, CAR_Y) - (Cos(a) * WHEELBASE / 2)
// Lasketaan taka-akselin keskipiste
rax# = CarTable(carID, CAR_X) - (Sin(a - 180) * WHEELBASE / 2)
ray# = CarTable(carID, CAR_Y) - (Cos(a - 180) * WHEELBASE / 2)
// Siirretään etuakselia renkaiden osoittamaan suuntaan nopeuden verran
a# = a + CarTable(carID, CAR_STEER)
fax# = fax - (Sin(a) * CarTable(carID, CAR_SPEED))
fay# = fay - (Cos(a) * CarTable(carID, CAR_SPEED))
// Lasketaan auton uusi kulma
CarTable(carID, CAR_ANGLE) = GetAngle(rax, ray, fax, fay)
// Lasketaan auton uusi paikka
CarTable(carID, CAR_X) = fax + Sin(CarTable(carID, CAR_ANGLE)-90) * WHEELBASE / 2
CarTable(carID, CAR_Y) = fay + Cos(CarTable(carID, CAR_ANGLE)-90) * WHEELBASE / 2
End Function
Function DrawCars()
For i = 1 To gNumCars
DrawCar(i)
Next i
End Function
Function DrawCar(carID)
// Auton runko
DrawBox(CarTable(carID, CAR_X), CarTable(carID, CAR_Y), CWIDTH, CLENGTH, CarTable(carID, CAR_ANGLE))
// Laskenaan renkaiden paikat
CalcBoxPoints(CarTable(carID, CAR_X), CarTable(carID, CAR_Y), RAILGAUGE, WHEELBASE, CarTable(carID, CAR_ANGLE))
wx1# = gx1: wy1# = gy1
wx2# = gx2: wy2# = gy2
wx3# = gx3: wy3# = gy3
wx4# = gx4: wy4# = gy4
// Vasen eturengas
DrawBox(wx1, wy1, WWIDTH, WLENGTH, CarTable(carID, CAR_ANGLE) + CarTable(carID, CAR_STEER))
// Oikea eturengas
DrawBox(wx2, wy2, WWIDTH, WLENGTH, CarTable(carID, CAR_ANGLE) + CarTable(carID, CAR_STEER))
// Vasen takarengas
DrawBox(wx4, wy4, WWIDTH, WLENGTH, CarTable(carID, CAR_ANGLE))
// Oikea takarengas
DrawBox(wx3, wy3, WWIDTH, WLENGTH, CarTable(carID, CAR_ANGLE))
End Function
Function DrawBox(x#, y#, width#, length#, angle#)
CalcBoxPoints(x, y, width, length, angle)
Line gx1, gy1, gx2, gy2
Line gx2, gy2, gx3, gy3
Line gx3, gy3, gx4, gy4
Line gx4, gy4, gx1, gy1
End Function
Function CalcBoxPoints(x#, y#, width#, length#, angle#)
a# = angle - 90
lx# = x - (Sin(a + 90) * width / 2)
ly# = y - (Cos(a + 90) * width / 2)
gx1 = lx - (Sin(a) * length / 2)
gy1 = ly - (Cos(a) * length / 2)
gx2 = gx1 - (Sin(a - 90) * width)
gy2 = gy1 - (Cos(a - 90) * width)
gx3 = gx2 - (Sin(a - 180) * length)
gy3 = gy2 - (Cos(a - 180) * length)
gx4 = gx3 - (Sin(a - 270) * width)
gy4 = gy3 - (Cos(a - 270) * width)
End Function