Windows Lua build instructions.
This commit is contained in:
@@ -103,7 +103,7 @@ end
|
||||
local function tileCollision( entity )
|
||||
local vPos = entity.pos + entity.vel -- Future pos with current vel.
|
||||
local vRect = util.tableClone( entity.colRect )
|
||||
local tinySlit = 0.001 -- Tiny slit between collisionRect and tile to prevent getting stuck on all seams.
|
||||
local tinyGap = 0.001 -- Tiny slit between collisionRect and tile to prevent getting stuck on all seams.
|
||||
|
||||
-- Move test rect to predicted position.
|
||||
vRect[1] = vPos.x - vRect[3] / 2
|
||||
@@ -121,14 +121,14 @@ local function tileCollision( entity )
|
||||
if isTileWall( Vec2:new( tileRect[3], y ) ) then
|
||||
-- Use new_x to push out of tile.
|
||||
local new_x = tileRect[3] * TILE_SIZE - ( entity.colRect[1] + entity.colRect[3] )
|
||||
entity.vel.x = new_x - tinySlit
|
||||
entity.vel.x = new_x - tinyGap
|
||||
|
||||
break
|
||||
end
|
||||
elseif entity.vel.x < 0 then
|
||||
if isTileWall( Vec2:new( tileRect[1], y ) ) then
|
||||
local new_x = ( tileRect[1] * TILE_SIZE + TILE_SIZE ) - entity.colRect[1]
|
||||
entity.vel.x = new_x + tinySlit, 0
|
||||
entity.vel.x = new_x + tinyGap, 0
|
||||
|
||||
break
|
||||
end
|
||||
@@ -151,7 +151,7 @@ local function tileCollision( entity )
|
||||
if isTileWall( Vec2:new( x, tileRect[4] ) ) then
|
||||
local new_y = tileRect[4] * TILE_SIZE - ( entity.colRect[2] + entity.colRect[4] )
|
||||
-- math.max prevents bounce when hitting right on the corner.
|
||||
entity.vel.y = math.max( new_y - tinySlit, 0 )
|
||||
entity.vel.y = math.max( new_y - tinyGap, 0 )
|
||||
player.onFloor = true
|
||||
|
||||
break
|
||||
@@ -159,7 +159,7 @@ local function tileCollision( entity )
|
||||
elseif entity.vel.y < 0 then
|
||||
if isTileWall( Vec2:new( x, tileRect[2] ) ) then
|
||||
local new_y = ( tileRect[2] * TILE_SIZE + TILE_SIZE ) - entity.colRect[2]
|
||||
entity.vel.y = new_y + tinySlit
|
||||
entity.vel.y = new_y + tinyGap
|
||||
|
||||
break
|
||||
end
|
||||
|
||||
122
examples/point_triangle_collision/main.lua
Normal file
122
examples/point_triangle_collision/main.lua
Normal file
@@ -0,0 +1,122 @@
|
||||
package.path = package.path..";"..RL_GetBasePath().."../resources/lib/?.lua"
|
||||
|
||||
util = require "utillib"
|
||||
Vec2 = require "vector2"
|
||||
Vec3 = require "vector3"
|
||||
|
||||
local MOVE_SPEED = 0.5
|
||||
|
||||
local monitor = 0
|
||||
local camera = -1
|
||||
local texture = -1
|
||||
local tri = {
|
||||
a = Vec3:new( 0, 0, 0 ),
|
||||
-- a = Vec3:new( 0, 1, 0 ),
|
||||
b = Vec3:new( 0, 0, 2 ),
|
||||
c = Vec3:new( 1, 0, 0 ),
|
||||
normal = Vec3:new( 0, 0, 0 ),
|
||||
}
|
||||
local point = {
|
||||
pos = Vec3:new( 0.2, 0.3, 0.2 ),
|
||||
radius = 0.05,
|
||||
lineLen = 0.5,
|
||||
color = RED,
|
||||
}
|
||||
|
||||
local debugText = ""
|
||||
|
||||
local function calcNormal( tri )
|
||||
tri.normal = Vec3:new( RL_Vector3Normalize( RL_Vector3CrossProduct( tri.b - tri.a, tri.c - tri.a ) ) )
|
||||
end
|
||||
|
||||
function init()
|
||||
local mPos = RL_GetMonitorPosition( monitor )
|
||||
local mSize = RL_GetMonitorSize( monitor )
|
||||
local winSize = { 1920, 1080 }
|
||||
|
||||
RL_SetWindowState( FLAG_WINDOW_RESIZABLE )
|
||||
RL_SetWindowPosition( { mPos[1] + mSize[1] / 2 - winSize[1] / 2, mPos[2] + mSize[2] / 2 - winSize[2] / 2 } )
|
||||
RL_SetWindowSize( winSize )
|
||||
camera = RL_CreateCamera3D()
|
||||
RL_SetCamera3DPosition( camera, { 0, 1, 2 } )
|
||||
RL_SetCamera3DTarget( camera, { 0, 0, 0 } )
|
||||
RL_SetCamera3DUp( camera, { 0, 2, 0 } )
|
||||
RL_SetCamera3DMode( camera, CAMERA_FREE )
|
||||
|
||||
calcNormal( tri )
|
||||
end
|
||||
|
||||
local function checkCollisionPointTriangle( p, a, b, c, n )
|
||||
local result = Vec3:new( 0, 0, 0 )
|
||||
|
||||
local v0 = Vec3:new( b.x - a.x, b.y - a.y, b.z - a.z ) -- Vector3Subtract( b, a )
|
||||
local v1 = Vec3:new( c.x - a.x, c.y - a.y, c.z - a.z ) -- Vector3Subtract( c, a )
|
||||
local v2 = Vec3:new( p.x - a.x, p.y - a.y, p.z - a.z ) -- Vector3Subtract( p, a )
|
||||
local d00 = v0.x * v0.x + v0.y * v0.y + v0.z * v0.z -- Vector3DotProduct( v0, v0 )
|
||||
local d01 = v0.x * v1.x + v0.y * v1.y + v0.z * v1.z -- Vector3DotProduct( v0, v1 )
|
||||
local d11 = v1.x * v1.x + v1.y * v1.y + v1.z * v1.z -- Vector3DotProduct( v1, v1 )
|
||||
local d20 = v2.x * v0.x + v2.y * v0.y + v2.z * v0.z -- Vector3DotProduct( v2, v0 )
|
||||
local d21 = v2.x * v1.x + v2.y * v1.y + v2.z * v1.z -- Vector3DotProduct( v2, v1 )
|
||||
|
||||
local denom = d00 * d11 - d01 * d01
|
||||
local distance = v2.x * n.x + v2.y * n.y + v2.z * n.z -- Vector3DotProduct( v2, n )
|
||||
|
||||
debugText = debugText.."distance "..distance.."\n"
|
||||
debugText = debugText.."v0 "..v0.x..", "..v0.y..", "..v0.z.."\n"
|
||||
debugText = debugText.."v1 "..v1.x..", "..v1.y..", "..v1.z.."\n"
|
||||
debugText = debugText.."v2 "..v2.x..", "..v2.y..", "..v2.z.."\n"
|
||||
|
||||
result.y = ( d11 * d20 - d01 * d21) / denom
|
||||
result.z = ( d00 * d21 - d01 * d20) / denom
|
||||
result.x = 1.0 - ( result.z + result.y )
|
||||
|
||||
debugText = debugText.."result "..result.x..", "..result.y..", "..result.z.."\n"
|
||||
|
||||
return 0.0 < result.x and 0.0 < result.y and 0.0 < result.z and distance < 0.0, distance
|
||||
end
|
||||
|
||||
function process( delta )
|
||||
debugText = ""
|
||||
|
||||
if RL_IsKeyDown( string.byte( "D" ) ) then
|
||||
point.pos.x = point.pos.x + MOVE_SPEED * delta
|
||||
elseif RL_IsKeyDown( string.byte( "A" ) ) then
|
||||
point.pos.x = point.pos.x - MOVE_SPEED * delta
|
||||
end
|
||||
if RL_IsKeyDown( string.byte( "S" ) ) then
|
||||
point.pos.z = point.pos.z + MOVE_SPEED * delta
|
||||
elseif RL_IsKeyDown( string.byte( "W" ) ) then
|
||||
point.pos.z = point.pos.z - MOVE_SPEED * delta
|
||||
end
|
||||
if RL_IsKeyDown( string.byte( "R" ) ) then
|
||||
point.pos.y = point.pos.y + MOVE_SPEED * delta
|
||||
elseif RL_IsKeyDown( string.byte( "F" ) ) then
|
||||
point.pos.y = point.pos.y - MOVE_SPEED * delta
|
||||
end
|
||||
|
||||
if checkCollisionPointTriangle( point.pos, tri.a, tri.b, tri.c, tri.normal ) then
|
||||
point.color = RED
|
||||
else
|
||||
point.color = GREEN
|
||||
end
|
||||
end
|
||||
|
||||
function draw()
|
||||
RL_ClearBackground( { 100, 150, 100 } )
|
||||
RL_UpdateCamera3D( camera )
|
||||
|
||||
RL_BeginMode3D( camera )
|
||||
RL_DrawGrid( 8, 1 )
|
||||
RL_DrawTriangle3D( tri.a, tri.b, tri.c, { 200, 100, 100 } )
|
||||
|
||||
RL_DrawLine3D( { point.pos.x - point.lineLen, point.pos.y, point.pos.z },
|
||||
{ point.pos.x + point.lineLen, point.pos.y, point.pos.z }, BLUE )
|
||||
RL_DrawLine3D( { point.pos.x, point.pos.y - point.lineLen, point.pos.z },
|
||||
{ point.pos.x, point.pos.y + point.lineLen, point.pos.z }, BLUE )
|
||||
RL_DrawLine3D( { point.pos.x, point.pos.y, point.pos.z - point.lineLen },
|
||||
{ point.pos.x, point.pos.y, point.pos.z + point.lineLen }, BLUE )
|
||||
RL_DrawSphereWires( point.pos, point.radius, 3, 8, point.color )
|
||||
RL_EndMode3D()
|
||||
|
||||
RL_DrawText( 0, debugText, { 10, 10 }, 30, 4, WHITE )
|
||||
end
|
||||
@@ -34,6 +34,7 @@ local function reset()
|
||||
-- Set ball to center.
|
||||
ball.pos = { winSize[1] / 2, winSize[2] / 2 }
|
||||
-- Short for if math random result 1, set BALL_SPEED otherwise set -BALL_SPEED.
|
||||
-- Could be replaced by normal if statement for easier readability.
|
||||
ball.vel[1] = math.random( 0, 1 ) == 1 and BALL_SPEED or -BALL_SPEED
|
||||
-- Start slow.
|
||||
ball.vel[2] = 0
|
||||
|
||||
@@ -40,6 +40,7 @@ local function reset()
|
||||
-- Set ball to center.
|
||||
ball.pos:set( winSize.x / 2, winSize.y / 2 )
|
||||
-- Short for if math random result 1, set BALL_SPEED otherwise set -BALL_SPEED.
|
||||
-- Could be replaced by normal if statement for easier readability.
|
||||
ball.vel.x = math.random( 0, 1 ) == 1 and BALL_SPEED or -BALL_SPEED
|
||||
-- Start slow.
|
||||
ball.vel.y = 0
|
||||
@@ -123,7 +124,7 @@ function draw()
|
||||
-- Draw ball. Ball position will be the center in drawCircle.
|
||||
RL_DrawCircle( ball.pos, ball.radius, WHITE )
|
||||
|
||||
-- Draw scire
|
||||
-- Draw score.
|
||||
RL_DrawText( 0, playerLeft.score, { 50, 10 }, 40, 2, WHITE )
|
||||
local rightTextSize = Vec2:new( RL_MeasureText( 0, playerRight.score, 40, 2 ) )
|
||||
RL_DrawText( 0, playerRight.score, { winSize.x - 50 - rightTextSize.x, 10 }, 40, 2, WHITE )
|
||||
|
||||
Reference in New Issue
Block a user