movement stuff

This commit is contained in:
GoaLitiuM
2021-04-08 19:31:01 +03:00
parent ae8983a90e
commit 1234dd9393
11 changed files with 248 additions and 141 deletions

2
.gitignore vendored
View File

@@ -16,3 +16,5 @@ Output/*
.vscode/
Assets/
Source/obj/
.idea/*
*.DotSettings.user

View File

@@ -1,13 +1,14 @@
{
"ID": "3c7bc3854d42f9b1b0fea9ba0d7fa8e9",
"TypeName": "FlaxEditor.Content.Settings.GameSettings",
"EngineBuild": 6216,
"EngineBuild": 6217,
"Data": {
"ProductName": "Goake",
"CompanyName": "GoaLitiuM",
"FirstScene": "0733cc9b40d3d05366be64bbd9b59e21",
"NoSplashScreen": true,
"Time": "a55dc3c04da4ea3744b7f1994565beac",
"Physics": "4bd8a4cc460399b5f1975fbe0a668e3f",
"Input": "8ec53dba4c238bfbea1d62922e612a4d",
"Graphics": "f94d5aae457aeba67033a8a4ca753214",
"GameCooking": "af2e52554f7faed7b4937181dd22d166",

View File

@@ -31,12 +31,11 @@
"Name": "PlayerPrefab",
"Transform": {
"Translation": {
"X": -82.89323425292969,
"Y": 111.32740783691406,
"Z": 114.5750503540039
"X": -913.1759033203125,
"Y": 207.86880493164063,
"Z": 433.89312744140627
}
},
"Mass": 3784.5869140625,
"LinearDamping": 0.0,
"AngularDamping": 0.0,
"Constraints": 56,
@@ -82,10 +81,26 @@
"W": 0.7071068286895752
}
},
"IsTrigger": true,
"ContactOffset": 0.0,
"Material": "ffe0e84c49607480f67a3994a7fe11a8",
"Radius": 16.0,
"Height": 52.0
},
{
"ID": "9677298d46e5d5d7021f7cb607bd3023",
"TypeName": "FlaxEngine.BoxCollider",
"ParentID": "4ef1f38b4569142b55b0ff935525d47a",
"IsActive": false,
"Name": "BoxCollider",
"IsTrigger": true,
"Material": "ffe0e84c49607480f67a3994a7fe11a8",
"Size": {
"X": 32.0,
"Y": 84.0,
"Z": 32.0
}
},
{
"ID": "b5d566374477944e69bdc2b86249883b",
"TypeName": "FlaxEngine.UICanvas",
@@ -114,7 +129,7 @@
"TypeName": "Cabrito.ConsoleScript",
"ParentID": "b5d566374477944e69bdc2b86249883b",
"V": {
"ConsoleFont": "ec3a34464c0c59c40c52a29f469f161f",
"ConsoleFont": "43f32bec443158643f53699f07b2e09c",
"BackgroundColor": {
"R": 0.0,
"G": 0.0,
@@ -131,13 +146,13 @@
"Transform": {
"Translation": {
"X": 0.0,
"Y": 733.0,
"Y": 887.0,
"Z": 0.0
}
},
"Control": "FlaxEngine.GUI.Label",
"Data": {
"Text": "FPS: 14\nrFPS: 13\nCon: NaNms\nDirectX11\nGC memory: 15.89691MB",
"Text": "FPS: 120\nrFPS: 120\nCon: NaNms\nDirectX11\nGC memory: 8.103872MB",
"TextColor": {
"R": 1.0,
"G": 1.0,

View File

@@ -1,7 +1,7 @@
{
"ID": "8ec53dba4c238bfbea1d62922e612a4d",
"TypeName": "FlaxEditor.Content.Settings.InputSettings",
"EngineBuild": 6216,
"EngineBuild": 6217,
"Data": {
"ActionMappings": [
{
@@ -59,6 +59,14 @@
"MouseButton": 0,
"GamepadButton": 0,
"Gamepad": 0
},
{
"Name": "Jump",
"Mode": 0,
"Key": 32,
"MouseButton": 3,
"GamepadButton": 0,
"Gamepad": 0
}
],
"AxisMappings": [

View File

@@ -1,11 +1,11 @@
{
"ID": "4bd8a4cc460399b5f1975fbe0a668e3f",
"TypeName": "FlaxEditor.Content.Settings.PhysicsSettings",
"EngineBuild": 6216,
"EngineBuild": 6217,
"Data": {
"DefaultGravity": {
"X": 0.0,
"Y": -9810.0,
"Y": -800.0,
"Z": 0.0
},
"QueriesHitTriggers": true,

View File

@@ -1,7 +1,7 @@
{
"ID": "a55dc3c04da4ea3744b7f1994565beac",
"TypeName": "FlaxEditor.Content.Settings.TimeSettings",
"EngineBuild": 6216,
"EngineBuild": 6217,
"Data": {
"UpdateFPS": 120.0,
"PhysicsFPS": 60.0,

View File

@@ -97,9 +97,7 @@ namespace Cabrito
if (font == null)
return (int)Height;
int h = (int)Mathf.Round(LineSpacing * (font.Height / Platform.DpiScale) * Scale.Y);
Console.Print("height: " + LineSpacing.ToString());
return h;
return (int)Mathf.Round(LineSpacing * (font.Height / Platform.DpiScale) * Scale.Y);
}
private int GetHeightInLines()

View File

@@ -182,7 +182,6 @@ namespace Cabrito
}
Console.RegisterConsoleScript(this);
RefreshLayout();
#if false
//for (int i = 0; i < 10; i++)
@@ -263,6 +262,12 @@ namespace Cabrito
Console.OnOpen += () => { Screen.CursorVisible = true; Screen.CursorLock = CursorLockMode.None; };
Console.OnClose += () => { Screen.CursorVisible = false; Screen.CursorLock = CursorLockMode.Locked; };
// hide console by default, and close it instantly
Console.Close();
var rootlocation = rootControl.Control.Location;
rootlocation.Y = -rootControl.Control.Height;
rootControl.Control.Location = rootlocation;
}
private void OnSendLog(LogType level, string msg, FlaxEngine.Object obj, string stackTrace)
@@ -287,36 +292,6 @@ namespace Cabrito
Debug.Logger.LogHandler.SendExceptionLog -= OnSendExceptionLog;
}
public void RefreshLayout()
{
if (consoleBox == null || consoleInputBox == null)
return;
//consoleBox.SetAnchorPreset(AnchorPresets.StretchAll, true);
//consoleBox.AnchorMax = new Vector2(1.0f, ConsoleHeight);
Vector2 screenSize = Screen.Size;
Vector2 consoleSize = new Vector2(screenSize.X, screenSize.Y * ConsoleHeight);
//consoleBox.Height = consoleSize.Y - consoleBox.GetFontHeight();
//consoleInputBox.SetAnchorPreset(AnchorPresets.HorizontalStretchTop, true);
//consoleInputBox.Location = new Vector2(0, consoleBox.Bottom);
//Font fontRaw = consoleBox.Font.GetFont();
//consoleBox.Height -= fontRaw.Height;
/*float fontHeight = consoleBox.GetFontHeight();
Vector2 screenSize = Screen.Size;
Vector2 consoleSize = new Vector2(screenSize.X, screenSize.Y * ConsoleHeight);
consoleBox.Height = consoleSize.Y - fontHeight;
consoleInputBox.Location = new Vector2(0, consoleBox.Height);
consoleInputBox.Height = fontHeight;
consoleInputBox.ScrollToCaret();*/
}
private void OnConsoleInputEvent()
{
string currentInput = Input.InputText;
@@ -363,23 +338,10 @@ namespace Cabrito
Parent.As<UICanvas>().ReceivesEvents = false;
}
bool firstUpdate = true;
public override void OnUpdate()
{
base.OnUpdate();
// HACK: workaround for cursor not getting hidden in editor after entering play mode
if (firstUpdate)
{
firstUpdate = false;
Console.Close();
// hide console by default
var rootlocation = rootControl.Control.Location;
rootlocation.Y = -rootControl.Control.Height;
rootControl.Control.Location = rootlocation;
}
float targetY;
if (!Console.IsOpen)
targetY = -rootControl.Control.Height;

View File

@@ -1,4 +1,5 @@
using FlaxEngine;
using System.Collections.Generic;
using FlaxEngine;
using Cabrito;
using System.Diagnostics;
using System.Threading.Tasks;
@@ -45,115 +46,235 @@ namespace Game
viewRoll = initialEulerAngles.Z;
}
//Vector3 wishVelocity = Vector3.Zero;
Vector3 gravityVelocity = Vector3.Zero;
public override void OnFixedUpdate()
private RayCastHit[] TracePlayer(Vector3 start, Vector3 end)
{
var camera = rootActor.GetChild<Camera>();
var camTrans = camera.Transform;
var rootTrans = rootActor.Transform;
float inputH = InputManager.GetAxis("Horizontal");
float inputV = InputManager.GetAxis("Vertical");
var move = new Vector3(inputH, 0.0f, inputV);
var rigidBody = Actor.As<RigidBody>();
const float groundAccel = 30 * 100f;
//const float groundDamping = 30f * 100;
var moveDirection = rootTrans.TransformDirection(move);
Vector3 wishVelocity = Vector3.Zero;
if (!Console.IsOpen && !move.IsZero)
{
move = moveDirection.Normalized * MoveSpeed * Time.DeltaTime / (1.0f/Time.PhysicsFPS);
wishVelocity = move;
/*var groundVelocity = rigidBody.LinearVelocity;
groundVelocity.Y = 0f;
if (groundVelocity.Length > 320)
wishVelocity = wishVelocity.Normalized * 320;*/
//wishVelocity = move;
}
/*else if (!wishVelocity.IsZero)
{
moveDirection = wishVelocity.Normalized;
wishVelocity -= moveDirection * groundDamping * Time.DeltaTime;
if (Vector3.Dot(wishVelocity, moveDirection) < 0)
wishVelocity = Vector3.Zero;
}*/
//else if (!wishVelocity.IsZero)
// wishVelocity = new Vector3(0);
bool collided;
RayCastHit[] hitInfos;
var capsuleCollider = Actor.GetChild<CapsuleCollider>();
var boxCollider = Actor.GetChild<BoxCollider>();
PhysicsColliderActor collider = null;
Vector3 delta = end - start;
Vector3 velocity = wishVelocity;
if (capsuleCollider && capsuleCollider.IsActive)
{
collider = capsuleCollider;
bool collided = Physics.BoxCastAll(rigidBody.Position, new Vector3(capsuleCollider.Radius, (capsuleCollider.Height + capsuleCollider.Radius) / 2, capsuleCollider.Radius), Physics.Gravity.Normalized, out RayCastHit[] hitInfos, Quaternion.Identity, Physics.Gravity.Length * Time.DeltaTime, uint.MaxValue, false);
//start.Y -= capsuleCollider.Height / 2;
//start.Y -= capsuleCollider.Radius / 2;
const float minDistanceFromFloor = 0.1f;
/*collided = Physics.BoxCastAll(start,
new Vector3(capsuleCollider.Radius,
(capsuleCollider.Height + capsuleCollider.Radius) / 2,
capsuleCollider.Radius),
delta.Normalized, out hitInfos, Quaternion.Identity, delta.Length, uint.MaxValue,
false);*/
Vector3 floorNormal = Vector3.Zero;
Vector3 floorPosition = Vector3.Zero;
float floorDistance = 0.0f;
collided = Physics.CapsuleCastAll(start,
capsuleCollider.Radius, capsuleCollider.Height,
delta.Normalized, out hitInfos, capsuleCollider.Orientation, delta.Length, uint.MaxValue,
false);
}
else if (boxCollider && boxCollider.IsActive)
{
collider = boxCollider;
//start.Y += boxCollider.Size.Y / 2;
collided = Physics.BoxCastAll(start,
boxCollider.OrientedBox.Extents,
delta.Normalized, out hitInfos, boxCollider.Orientation, delta.Length, uint.MaxValue,
false);
}
else
return new RayCastHit[0];
if (collided)
{
List<RayCastHit> hitInfosFiltered = new List<RayCastHit>();
foreach (var hitInfo in hitInfos)
{
if (hitInfo.Collider == collider)
continue;
hitInfosFiltered.Add(hitInfo);
}
hitInfos = hitInfosFiltered.ToArray();
}
return hitInfos;
}
private bool SlideMove(Vector3 start, ref Vector3 velocity)
{
// PM_SlideMove
/*
endpos = pos + velocity
for i=0, i<4, i++
trace(pos, endpos)
if trace.dist >= 1
break;
blocked =
blocked_floor => trace.plane.normal[2] >= MIN_STEP_NORMAL
blocked_step => trace.plane.normal[2] == 0
blocked_other => .
*/
Vector3 startPos = start;
Vector3 endPos = startPos + (velocity * Time.DeltaTime);
Vector3 delta = endPos - startPos;
bool blocked = false;
RayCastHit[] hitInfos = TracePlayer(startPos, endPos);
bool collided = hitInfos.Length > 0;
float distance = 1.0f;
Vector3 normal = Vector3.Zero;
Vector3 point;
if (collided)
{
collided = false;
foreach (var hitInfo in hitInfos)
{
if (hitInfo.Collider == capsuleCollider)
continue;
//if (hitInfo.Distance < minDistanceFromFloor)
// continue;
float fraction = hitInfo.Distance / delta.Length;
collided = true;
floorNormal = hitInfo.Normal;
floorPosition = hitInfo.Point;
floorDistance = hitInfo.Distance;
//Console.Print("collided " + hitInfo.Distance.ToString());
if (fraction <= distance)
{
normal = hitInfo.Normal;
point = hitInfo.Point;
distance = fraction;
}
break;
//Console.Print("collided " + hitInfo.Distance.ToString());
//break;
}
}
if (collided)
velocity = Vector3.ProjectOnPlane(velocity, normal);
blocked = collided;
return blocked;
}
//Vector3 wishVelocity = Vector3.Zero;
Vector3 gravityVelocity = Vector3.Zero;
Vector3 currentVelocity = Vector3.Zero;
private bool onGround = false;
private const float friction = 4f;
private const float stopspeed = 100f;
private const float accelerationGround = 10f;
private const float jumpVelocity = 270f;
private bool jumped = false;
public override void OnFixedUpdate()
{
var rigidBody = Actor.As<RigidBody>();
var rootTrans = rootActor.Transform;
var inputDirection = new Vector3(InputManager.GetAxis("Horizontal"), 0.0f, InputManager.GetAxis("Vertical"));
var moveDirection = rootTrans.TransformDirection(inputDirection);
Vector3 wishVelocity = Vector3.Zero;
if (!Console.IsOpen && !inputDirection.IsZero)
{
//move = moveDirection.Normalized * MoveSpeed * Time.DeltaTime / (1.0f/Time.PhysicsFPS);
inputDirection = moveDirection.Normalized * MoveSpeed;
wishVelocity = inputDirection;
}
// onGround checks
RayCastHit[] hitInfosGround = TracePlayer(rigidBody.Position, rigidBody.Position + (Vector3.Down * 1f));
onGround = false;
foreach (var hitInfo in hitInfosGround)
{
var dot = Vector3.Dot(Vector3.Down, hitInfo.Normal);
if (dot < -0.7) //~45deg
{
onGround = true;
Vector3 newPos = rigidBody.Position;
newPos += Vector3.Down * (hitInfo.Distance - 0.01f);
//newPos.Y = hitInfo.Point.Y + 0.0001f;
//rigidBody.Position = newPos;
//currentVelocity.Y = 0.0f;
}
}
if (!collided)
gravityVelocity += Physics.Gravity * Time.DeltaTime;
// jump
if (onGround)
{
if (!jumped && InputManager.GetAction("Jump"))
{
//jumped = true;
currentVelocity += Vector3.Up * jumpVelocity;
}
else if (jumped) // jump released
jumped = false;
}
// ground friction
if (onGround)
{
float currentSpeed = currentVelocity.Length;
float control = currentSpeed < stopspeed ? stopspeed : currentSpeed;
var drop = control * friction * Time.DeltaTime;
float newspeed = currentSpeed - drop;
if (newspeed < 0)
newspeed = 0;
if (currentSpeed < 0.0001f)
currentVelocity *= 0;
else
{
gravityVelocity = Vector3.Zero;
// if (floorDistance < minDistanceFromFloor)
{
var pos = rigidBody.Position;
pos.Y = floorPosition.Y + (capsuleCollider.Height * 0.5f + capsuleCollider.Radius) + (minDistanceFromFloor * 0.5f);
rigidBody.Position = pos;
currentVelocity *= newspeed / currentSpeed;
}
/*if (floorDistance < minDistanceFromFloor)
// ground acceleration
{
Console.Print("grav ");
rigidBody.Position = new Vector3(rigidBody.Position.X, (floorPosition + (floorNormal * minDistanceFromFloor*0.9f)).Y, rigidBody.Position.Z);
}*/
//rigidBody.Position = floorPosition + (floorNormal * minDistanceFromFloor);
float currentSpeed = Vector3.Dot(currentVelocity, wishVelocity.Normalized);
float addSpeed = wishVelocity.Length - currentSpeed;
if (addSpeed > 0)
{
float accelSpeed = 12f * wishVelocity.Length * Time.DeltaTime;
if (accelSpeed > addSpeed)
accelSpeed = addSpeed;
currentVelocity += accelSpeed * wishVelocity.Normalized;
}
}
Vector3 newVelocity = velocity + gravityVelocity;
if (newVelocity != rigidBody.LinearVelocity)
rigidBody.LinearVelocity = newVelocity;
if (rigidBody.IsKinematic && !rigidBody.LinearVelocity.IsZero)
rigidBody.Position += rigidBody.LinearVelocity * Time.DeltaTime;
/*
- check gravity
- get normal for slope
moveVelocity = wishVelocity
LinearVelocity = moveVelocity + _jumpVelocity + _gravityVelocity
*/
if (!onGround)
currentVelocity += Physics.Gravity * Time.DeltaTime;
bool blocked2 = SlideMove(rigidBody.Position, ref currentVelocity);
if (currentVelocity.Length > 0.0f)
rigidBody.Position += currentVelocity * Time.DeltaTime;
}
public override void OnUpdate()