// chuyển từ Box sang Broadphase dùng để khoanh vùng đối tượng public Box BoxToBroadPhase(Box b) { return(SweptAABB.GetBroadPhaseBox(b)); }
void Update() { _collided = false; _withColliderIndex = -1; if (debug_positions.Count > 1) { for (int i = 1; i < debug_positions.Count; i++) { Debug.DrawLine(debug_positions[i - 1], debug_positions[i], Color.yellow); } } var input = _inputMaster.Player.Movement.ReadValue <Vector2>(); var action = (short)_inputMaster.Player.MainAction.ReadValue <float>(); float3 position = transform.position; //var direction = new float2(input.x, input.y); if (input.sqrMagnitude < 0.01) { return; } //Debug.Log($"Input {input}"); //var vx = (input.x * Time.deltaTime * speed) * Time.realtimeSinceStartup; //var vy = (input.y * Time.deltaTime * speed) * Time.realtimeSinceStartup; var vx = input.x * Time.deltaTime * speed; var vy = input.y * Time.deltaTime * speed; var box = new Box(position.x - size * 0.5f, position.z - size * 0.5f, size, size, vx, vy); //var box = new Box(position.x, position.z, size, size, vx, vy ); float moveX = 0.0f; float moveY = 0.0f; float normalX = 0.0f; float normalY = 0.0f; float collisionTime = 1.0f; Vector2 velocity = Vector2.zero; for (var index = 0; index < colliders.Count; index++) { var collider = colliders[index]; if (collider.Type != ColliderType.Box) { continue; } var broadPhaseBox = SweptAABB.GetBroadPhaseBox(box); var block = collider.ToBox(); if (SweptAABB.CheckAABB(broadPhaseBox, block, ref normalX, ref normalY)) { _collided = true; _withColliderIndex = index; collisionTime = SweptAABB.Swept(box, block, ref normalX, ref normalY); // box.x += box.vx * collisiontime; // box.y += box.vy * collisiontime; Debug.Log($"Collided {collisionTime} {normalX} {normalY}"); break; } } var newPosition = Vector3.zero; if (_collided) { var remainingtime = 1.0f - collisionTime; if (collisionResolveType == PhysicsCollisionResolveType.Push) { float magnitude = math.sqrt((box.vx * box.vx + box.vy * box.vy)) * remainingtime; float dotprod = box.vx * normalY + box.vy * normalX; if (dotprod > 0.0f) { dotprod = 1.0f; } else if (dotprod < 0.0f) { dotprod = -1.0f; } box.vx = dotprod * normalY * magnitude; box.vy = dotprod * normalX * magnitude; newPosition = new Vector3(position.x + (box.vx), 0, position.z + (box.vy)); } else if (collisionResolveType == PhysicsCollisionResolveType.Slide) { float dotprod = (box.vx * normalY + box.vy * normalX) * remainingtime; box.vx = dotprod * normalY; box.vy = dotprod * normalX; newPosition = new Vector3(position.x + (box.vx), 0, position.z + (box.vy)); } } else { newPosition = new Vector3(position.x + (box.vx * collisionTime), 0, position.z + (box.vy * collisionTime)); } transform.position = newPosition; debug_positions.Add(newPosition); }