Wow, good stuff chafari.
Was already impressed by the
mirror stuff and took me a little while to understand what was going on here, for didn't know the commands, but it's really making clever usage of the lightmap command to introduce multi-texturing / filtering. It really works nicely for the car indeed, so put it on there:
[EDIT: added code]
main.agc (from:
Very simple kart physics)
// Project: Arcade kar physics - Tech demo
// Created: 2018-11-30
// 1.7
// by blink0k
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.
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(27)
End
Endif
foldend
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)
// DrawSensors(vehicle) // Draw the sensors if debugging
Print( "Arrows-Move, R-Reset, H-Hit shape, G-Ground contact V-Sensors, , SPACEBAR-Brakes Version:"+VERSION )
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
remstart
function mirror_flip_rotated(img as integer)
local x
local y
local memblockid
local memblocktoid
local size
x=0
y=0
memblocktoid=0
memblockid=0
size=0
memblockid = CreateMemblockFromImage (img)
imgwidth = GetMemblockInt(memblockid, 0)
imgheight = GetMemblockInt(memblockid, 4)
size = GetMemblockSize(memblockid)
memblocktoid = CreateMemblock(size)
SetMemblockInt(memblocktoid, 0, imgwidth)
SetMemblockInt(memblocktoid, 4, imgheight+1)
SetMemblockInt(memblocktoid, 8, 32)
x=imgwidth *imgheight * 4
offset=12
for x=((imgwidth*imgheight)-1)*4 to 12 step -4
r=GetMemblockByte(memblockid, offset)
g=GetMemblockByte(memblockid, offset+1)
b=GetMemblockByte(memblockid, offset + 2)
a=GetMemblockByte(memblockid, offset + 3)
setMemblockByte(memblocktoid, x,r)
setMemblockByte(memblocktoid, x + 1,g)
setMemblockByte(memblocktoid, x + 2,b)
setMemblockByte(memblocktoid, x + 3,a)
inc offset,4
next
deleteimage (img)
img = CreateImageFromMemblock(memblocktoid)
DeleteMemblock(memblocktoid)
endfunction img
remend
#include "Constants.agc"
#include "MakeCar.agc"
#include "LoadVehicle.agc"
#include "MoveVehicle.agc"