perspective
This commit is contained in:
parent
4a251fe5e2
commit
2acfe785db
|
@ -65,14 +65,18 @@ class Action
|
||||||
# log "Action.finished #{@name} #{@object?.actionFinished?}"
|
# log "Action.finished #{@name} #{@object?.actionFinished?}"
|
||||||
@object.actionFinished @
|
@object.actionFinished @
|
||||||
return if @deleted
|
return if @deleted
|
||||||
if @current == @getDuration() # if keepRest wasn't called -> reset start and current values
|
|
||||||
@reset()
|
@reset()
|
||||||
|
# if @current >= @getDuration() # if keepRest wasn't called -> reset start and current values
|
||||||
|
# @reset()
|
||||||
|
# else
|
||||||
|
# log 'keeping rest', @current
|
||||||
|
|
||||||
reset: () ->
|
reset: () ->
|
||||||
@start = 0
|
log "action.reset #{@name}"
|
||||||
|
@start = 0 # world time
|
||||||
@rest = 0
|
@rest = 0
|
||||||
@last = 0
|
@last = 0 # relative
|
||||||
@current = 0
|
@current = 0 # relative
|
||||||
|
|
||||||
takeRest: (action) ->
|
takeRest: (action) ->
|
||||||
@current = action.rest
|
@current = action.rest
|
||||||
|
@ -84,6 +88,8 @@ class Action
|
||||||
if @rest != 0
|
if @rest != 0
|
||||||
@current = @rest
|
@current = @rest
|
||||||
@rest = 0
|
@rest = 0
|
||||||
|
# @last = 0
|
||||||
|
# @current = 0
|
||||||
|
|
||||||
getRelativeTime: -> @current / @getDuration()
|
getRelativeTime: -> @current / @getDuration()
|
||||||
getRelativeDelta: -> (@current-@last) / @getDuration()
|
getRelativeDelta: -> (@current-@last) / @getDuration()
|
||||||
|
|
|
@ -155,8 +155,8 @@ class Bot extends Pushable
|
||||||
# 000 000 0000000 000 000 0000000 000 000
|
# 000 000 0000000 000 000 0000000 000 000
|
||||||
|
|
||||||
initAction: (action) ->
|
initAction: (action) ->
|
||||||
# log "initAction #{action.name}"
|
|
||||||
newPos = new Pos @position
|
newPos = new Pos @position
|
||||||
|
log "initAction #{action.name} pos", newPos
|
||||||
|
|
||||||
switch action.id
|
switch action.id
|
||||||
when Action.NOOP then return
|
when Action.NOOP then return
|
||||||
|
@ -176,8 +176,8 @@ class Bot extends Pushable
|
||||||
super action
|
super action
|
||||||
return
|
return
|
||||||
|
|
||||||
if not newPos.eql @position
|
if not newPos.eql new Pos @position
|
||||||
# log 'bot.initAction', newPos
|
log 'bot.initAction objectWillMoveToPos:', newPos
|
||||||
world.objectWillMoveToPos @, newPos, action.getDuration()
|
world.objectWillMoveToPos @, newPos, action.getDuration()
|
||||||
|
|
||||||
# 00000000 00000000 00000000 00000000 0000000 00000000 00 00
|
# 00000000 00000000 00000000 00000000 0000000 00000000 00 00
|
||||||
|
|
|
@ -12,10 +12,10 @@ class Pos
|
||||||
|
|
||||||
constructor: (x=0, y=0, z=0) ->
|
constructor: (x=0, y=0, z=0) ->
|
||||||
|
|
||||||
if (x instanceof Vector) or (x instanceof Pos)
|
if x.x? and x.y?
|
||||||
@x = Math.round x.x
|
@x = Math.round x.x
|
||||||
@y = Math.round x.y
|
@y = Math.round x.y
|
||||||
@z = Math.round x.z
|
@z = Math.round x.z ? 0
|
||||||
else if Array.isArray x
|
else if Array.isArray x
|
||||||
@x = Math.round x[0]
|
@x = Math.round x[0]
|
||||||
@y = Math.round x[1]
|
@y = Math.round x[1]
|
||||||
|
@ -24,11 +24,11 @@ class Pos
|
||||||
@x = Math.round x
|
@x = Math.round x
|
||||||
@y = Math.round y
|
@y = Math.round y
|
||||||
@z = Math.round z
|
@z = Math.round z
|
||||||
log "Pos #{x} #{y} #{z}", @
|
# log "Pos #{@x} #{@y} #{@z}"
|
||||||
if Number.isNaN @x
|
if Number.isNaN @x
|
||||||
throw new Error
|
throw new Error
|
||||||
|
|
||||||
vector: () -> new Vector x, y, z
|
vector: -> new Vector @x, @y, @z
|
||||||
minus: (p) -> new Pos @x-p.x, @y-p.y, @z-p.z
|
minus: (p) -> new Pos @x-p.x, @y-p.y, @z-p.z
|
||||||
plus: (p) -> new Pos @x+p.x, @y+p.y, @z+p.z
|
plus: (p) -> new Pos @x+p.x, @y+p.y, @z+p.z
|
||||||
mul: (f) -> new Pos @x*f, @y*f, @z*f
|
mul: (f) -> new Pos @x*f, @y*f, @z*f
|
||||||
|
@ -36,15 +36,15 @@ class Pos
|
||||||
eql: (p) -> @x==p.x and @y==p.y and @z==p.z
|
eql: (p) -> @x==p.x and @y==p.y and @z==p.z
|
||||||
|
|
||||||
add: (p) ->
|
add: (p) ->
|
||||||
@x = parseInt @x + p.x
|
@x = Math.round @x + p.x
|
||||||
@y = parseInt @y + p.y
|
@y = Math.round @y + p.y
|
||||||
@z = parseInt @z + p.z
|
@z = Math.round @z + p.z
|
||||||
@
|
@
|
||||||
|
|
||||||
sub: (p) ->
|
sub: (p) ->
|
||||||
@x = parseInt @x - p.x
|
@x = Math.round @x - p.x
|
||||||
@y = parseInt @y - p.y
|
@y = Math.round @y - p.y
|
||||||
@z = parseInt @z - p.z
|
@z = Math.round @z - p.z
|
||||||
@
|
@
|
||||||
|
|
||||||
module.exports = Pos
|
module.exports = Pos
|
||||||
|
|
|
@ -113,11 +113,15 @@ class Vector
|
||||||
@pointMappedToPlane: (point, planePos, planeNormal) ->
|
@pointMappedToPlane: (point, planePos, planeNormal) ->
|
||||||
point.minus(planeNormal).dot point.minus(planePos).dot(planeNormal)
|
point.minus(planeNormal).dot point.minus(planePos).dot(planeNormal)
|
||||||
|
|
||||||
@rayPlaneIntersectionFactor: (rayPos, rayDirection, planePos, planeNormal) ->
|
@rayPlaneIntersectionFactor: (rayPos, rayDir, planePos, planeNormal) ->
|
||||||
# ((planePos - rayPos) * planeNormal) / (rayDirection * planeNormal);
|
# ((planePos - rayPos) * planeNormal) / (rayDirection * planeNormal);
|
||||||
r = planePos.minus(rayPos).dot(planeNormal) / rayDirection.dot(planeNormal)
|
r = planePos.minus(rayPos).dot(planeNormal) / rayDir.dot(planeNormal)
|
||||||
# log 'rayPlaneIntersectionFactor', r
|
# log 'rayPlaneIntersectionFactor', r
|
||||||
if Number.isNaN r
|
if Number.isNaN r
|
||||||
|
log 'rayPos', rayPos
|
||||||
|
log 'rayDir', rayDir
|
||||||
|
log 'planePos', planePos
|
||||||
|
log 'planeNormal', planeNormal
|
||||||
throw new Error
|
throw new Error
|
||||||
r
|
r
|
||||||
|
|
||||||
|
|
|
@ -67,8 +67,9 @@ class Player extends Bot
|
||||||
|
|
||||||
updatePosition: () ->
|
updatePosition: () ->
|
||||||
if @move_action
|
if @move_action
|
||||||
relTime = (world.getTime() - @move_action.start) / @move_action.duration
|
relTime = (world.getTime()-@move_action.start) / @move_action.duration
|
||||||
if relTime <= 1.0
|
# log "updatePosition #{@move_action.id} #{relTime} #{@move_action.start} #{world.getTime()}"
|
||||||
|
if 0 <= relTime <= 1.0
|
||||||
switch @move_action.id
|
switch @move_action.id
|
||||||
when Action.FORWARD
|
when Action.FORWARD
|
||||||
@current_position = @position.plus @getDir().mul relTime
|
@current_position = @position.plus @getDir().mul relTime
|
||||||
|
@ -101,7 +102,7 @@ class Player extends Bot
|
||||||
else
|
else
|
||||||
# smooth camera rotation a little bit
|
# smooth camera rotation a little bit
|
||||||
lookDelta = (2.0 - @projection.getZVector().dot playerDir) * world.getSpeed() / 50.0
|
lookDelta = (2.0 - @projection.getZVector().dot playerDir) * world.getSpeed() / 50.0
|
||||||
newLookVector = @projection.getZVector().mul(1.0 - lookDelta).minus playerDir.mul lookDelta
|
newLookVector = @projection.getZVector().mul(1.0 - lookDelta).plus playerDir.mul lookDelta
|
||||||
newLookVector.normalize()
|
newLookVector.normalize()
|
||||||
@projection.setXVector playerUp.cross(newLookVector).normal()
|
@projection.setXVector playerUp.cross(newLookVector).normal()
|
||||||
@projection.setYVector playerUp
|
@projection.setYVector playerUp
|
||||||
|
@ -166,7 +167,6 @@ class Player extends Bot
|
||||||
@updatePosition()
|
@updatePosition()
|
||||||
|
|
||||||
playerPos = @current_position # desired look pos
|
playerPos = @current_position # desired look pos
|
||||||
# log 'getFollowProjection.current_position', @current_position
|
|
||||||
playerDir = @getCurrentDir()
|
playerDir = @getCurrentDir()
|
||||||
playerUp = @current_orientation.rotate new Vector(0,1,0).normal()
|
playerUp = @current_orientation.rotate new Vector(0,1,0).normal()
|
||||||
playerRight = playerDir.cross(playerUp).normal()
|
playerRight = playerDir.cross(playerUp).normal()
|
||||||
|
@ -177,6 +177,8 @@ class Player extends Bot
|
||||||
botToCamera = cameraPos.minus playerPos # vector from bot to current pos
|
botToCamera = cameraPos.minus playerPos # vector from bot to current pos
|
||||||
cameraBotDistance = botToCamera.length() # distance from camera to bot
|
cameraBotDistance = botToCamera.length() # distance from camera to bot
|
||||||
|
|
||||||
|
# log 'getFollowProjection 1', botToCamera, cameraPos, playerPos
|
||||||
|
|
||||||
if cameraBotDistance >= desiredDistance
|
if cameraBotDistance >= desiredDistance
|
||||||
difference = cameraBotDistance - desiredDistance
|
difference = cameraBotDistance - desiredDistance
|
||||||
delta = difference*difference/400.0 # weight for following speed
|
delta = difference*difference/400.0 # weight for following speed
|
||||||
|
@ -186,6 +188,8 @@ class Player extends Bot
|
||||||
delta = difference/20.0 # weight for negative following speed
|
delta = difference/20.0 # weight for negative following speed
|
||||||
cameraPos = cameraPos.mul(1.0-delta).plus (playerPos.plus botToCamera.normal().mul desiredDistance).mul delta
|
cameraPos = cameraPos.mul(1.0-delta).plus (playerPos.plus botToCamera.normal().mul desiredDistance).mul delta
|
||||||
|
|
||||||
|
# log 'getFollowProjection 2', botToCamera, cameraPos, playerPos
|
||||||
|
|
||||||
# ____________________________________________________ refining camera position
|
# ____________________________________________________ refining camera position
|
||||||
# second, rotate around bot
|
# second, rotate around bot
|
||||||
|
|
||||||
|
@ -202,8 +206,10 @@ class Player extends Bot
|
||||||
botToCamera = cameraPos.minus playerPos
|
botToCamera = cameraPos.minus playerPos
|
||||||
botToCameraNormal = botToCamera.normal()
|
botToCameraNormal = botToCamera.normal()
|
||||||
|
|
||||||
|
# log 'getFollowProjection 3', botToCamera, cameraPos, playerPos
|
||||||
|
|
||||||
rotFactor = 1.0
|
rotFactor = 1.0
|
||||||
# log "playerPos", playerPos
|
|
||||||
wall_distance = world.getWallDistanceForPos playerPos.plus botToCamera
|
wall_distance = world.getWallDistanceForPos playerPos.plus botToCamera
|
||||||
if wall_distance < 0.5
|
if wall_distance < 0.5
|
||||||
# ____________________________________________________ piercing walls
|
# ____________________________________________________ piercing walls
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
# 000 000 000 000 000 000 000 000 000 000 000 000
|
# 000 000 000 000 000 000 000 000 000 000 000 000
|
||||||
# 000 0000000 0000000 000 000 000 000 0000000 0000000 00000000
|
# 000 0000000 0000000 000 000 000 000 0000000 0000000 00000000
|
||||||
|
|
||||||
|
log = require '/Users/kodi/s/ko/js/tools/log'
|
||||||
Item = require './item'
|
Item = require './item'
|
||||||
Action = require './action'
|
Action = require './action'
|
||||||
Vector = require './lib/vector'
|
Vector = require './lib/vector'
|
||||||
|
@ -33,6 +34,7 @@ class Pushable extends Item
|
||||||
initAction: (action) ->
|
initAction: (action) ->
|
||||||
switch action.id
|
switch action.id
|
||||||
when Action.FALL
|
when Action.FALL
|
||||||
|
log 'fall!'
|
||||||
world.objectWillMoveToPos @, @position.plus(@direction), action.getDuration()
|
world.objectWillMoveToPos @, @position.plus(@direction), action.getDuration()
|
||||||
|
|
||||||
performAction: (action) ->
|
performAction: (action) ->
|
||||||
|
|
|
@ -384,7 +384,6 @@ class World extends Actor
|
||||||
|
|
||||||
isValidPos: (pos) ->
|
isValidPos: (pos) ->
|
||||||
p = new Pos pos
|
p = new Pos pos
|
||||||
log 'isValidPos', p
|
|
||||||
p.x >= 0 and p.x < @size.x and p.y >= 0 and p.y < @size.y and p.z >= 0 and p.z < @size.z
|
p.x >= 0 and p.x < @size.x and p.y >= 0 and p.y < @size.y and p.z >= 0 and p.z < @size.z
|
||||||
|
|
||||||
isInvalidPos: (pos) -> not @isValidPos pos
|
isInvalidPos: (pos) -> not @isValidPos pos
|
||||||
|
@ -810,7 +809,7 @@ class World extends Actor
|
||||||
o.step?(step) for o in @objects
|
o.step?(step) for o in @objects
|
||||||
|
|
||||||
switch @camera_mode
|
switch @camera_mode
|
||||||
when World.CAMERA_INSIDE then @projection = @player.getProjection()
|
when World.CAMERA_INSIDE then @projection = @player.getInsideProjection()
|
||||||
when World.CAMERA_BEHIND then @projection = @player.getBehindProjection()
|
when World.CAMERA_BEHIND then @projection = @player.getBehindProjection()
|
||||||
when World.CAMERA_FOLLOW then @projection = @player.getFollowProjection()
|
when World.CAMERA_FOLLOW then @projection = @player.getFollowProjection()
|
||||||
@projection.apply @camera
|
@projection.apply @camera
|
||||||
|
@ -925,7 +924,6 @@ class World extends Actor
|
||||||
f = Vector.rayPlaneIntersectionFactor pos, World.normals[w].neg(), planePos, World.normals[w]
|
f = Vector.rayPlaneIntersectionFactor pos, World.normals[w].neg(), planePos, World.normals[w]
|
||||||
if f < delta
|
if f < delta
|
||||||
insidePos.add World.normals[w].mul delta-f
|
insidePos.add World.normals[w].mul delta-f
|
||||||
# log 'getInsideWallPosWithDelta', insidePos
|
|
||||||
insidePos
|
insidePos
|
||||||
|
|
||||||
getWallDistanceForPos: (pos) -> # distance to the next wall (positive or negative)
|
getWallDistanceForPos: (pos) -> # distance to the next wall (positive or negative)
|
||||||
|
@ -934,19 +932,16 @@ class World extends Actor
|
||||||
planePos = new Vector -0.5, -0.5, -0.5
|
planePos = new Vector -0.5, -0.5, -0.5
|
||||||
if w >= 3 then planePos.add @size
|
if w >= 3 then planePos.add @size
|
||||||
f = Vector.rayPlaneIntersectionFactor pos, World.normals[w].neg(), planePos, World.normals[w]
|
f = Vector.rayPlaneIntersectionFactor pos, World.normals[w].neg(), planePos, World.normals[w]
|
||||||
# log "getWallDistanceForPos w #{w} min_f #{min_f} f #{f}"
|
|
||||||
min_f = absMin min_f, f
|
min_f = absMin min_f, f
|
||||||
# log "getWallDistanceForPos #{min_f}", pos
|
|
||||||
min_f
|
min_f
|
||||||
|
|
||||||
getWallDistanceForRay: (rayPos, rayDirection) -> # distance to the next wall in rayDirection
|
getWallDistanceForRay: (rayPos, rayDir) -> # distance to the next wall in rayDir
|
||||||
min_f = 10000
|
min_f = 10000
|
||||||
for w in [0..5]
|
for w in [0..5]
|
||||||
planePos = new Vector -0.5, -0.5, -0.5
|
planePos = new Vector -0.5, -0.5, -0.5
|
||||||
if w >= 3 then planePos.add @size
|
if w >= 3 then planePos.add @size
|
||||||
f = Vector.rayPlaneIntersectionFactor rayPos, rayDirection, planePos, World.normals[w]
|
f = Vector.rayPlaneIntersectionFactor rayPos, rayDir, planePos, World.normals[w]
|
||||||
min_f = f if f >= 0.0 and f < min_f
|
min_f = f if f >= 0.0 and f < min_f
|
||||||
# log "getWallDistanceForRay #{min_f}", rayDirection
|
|
||||||
min_f
|
min_f
|
||||||
|
|
||||||
displayLights: () ->
|
displayLights: () ->
|
||||||
|
|
Loading…
Reference in New Issue
Block a user