static float3 GetSimulatedPosition(float3 start, float3 end, float paraA, float paraB, float paraC, float t) { return(new Vector3( math.lerp(start.x, end.x, t), Parabola.Solve(paraA, paraB, paraC, t), math.lerp(start.z, end.z, t) )); }
public static Vector3 GetSimulatedPosition(Vector3 start, Vector3 end, float paraA, float paraB, float paraC, float t) { return(new Vector3( Mathf.LerpUnclamped(start.x, end.x, t), Parabola.Solve(paraA, paraB, paraC, t), Mathf.LerpUnclamped(start.z, end.z, t) )); }
public unsafe void Execute(Entity entity, int entityIndex, ref Translation cannonballPos, ref ArcState arc) { float t = ElapsedTime - arc.StartTime; if (t >= 0 && t < arc.Duration) { // Test for collisions with the player const float COLLISION_DISTANCE_SQ = (PLAYER_RADIUS + TankFireSystem.CANNONBALL_RADIUS) * (PLAYER_RADIUS + TankFireSystem.CANNONBALL_RADIUS); if (math.lengthsq(cannonballPos.Value - PlayerCenterPosition) < COLLISION_DISTANCE_SQ) { if (!EnableInvincibility) { // game over! add a NewGame entity to trigger the NewGameSystem to restart. Entity gameOverEntity = CommandBuffer.CreateEntity(entityIndex); CommandBuffer.AddComponent(entityIndex, gameOverEntity, new NewGameTag()); } } // Get the current height along the parabola & update cannonball pos float s = t / arc.Duration; cannonballPos.Value = new float3( math.lerp(arc.StartPos.x, arc.EndPos.x, s), Parabola.Solve(arc.Parabola.x, arc.Parabola.y, arc.Parabola.z, s), math.lerp(arc.StartPos.z, arc.EndPos.z, s) ); } else if (t >= arc.Duration) { // Cannonball has reached the end of its path; damage the block it hit // TODO(@cort): If the destination block has been damaged/lowered since the shot was fired, the shot will "land" in the air above the block. Original game does this too. int2 boxCoords = TerrainSystem.PositionToBlockCoords(cannonballPos.Value.x, cannonballPos.Value.z, TerrainSize); int boxIndex = TerrainSystem.BlockCoordsToIndex(boxCoords, TerrainSize); Entity boxEntity = BlockEntities[boxIndex]; int * blockHitsPtr = (int *)BlockHitCounts.GetUnsafePtr() + boxIndex; int newCount = Interlocked.Increment(ref *blockHitsPtr); if (newCount == 1) { CommandBuffer.AddComponent(entityIndex, boxEntity, new UpdateBlockTransformTag()); } // TODO(@cort): There is a (relatively benign) race condition here. There's no guarantee that the last job // to update the hit counts array will also write the last command that updates the BlockHeight component. // This means that if >1 cannonball hits a block in a single frame, some of the hits may not damage the block. // One solution would be commands that allow mutating component values rather than setting them to a value // known at record time. https://github.com/Unity-Technologies/dots/issues/632 float currentBoxHeight = BlockHeights[boxIndex]; CommandBuffer.SetComponent(entityIndex, boxEntity, new BlockHeight { Value = math.max(BLOCK_HEIGHT_MIN, currentBoxHeight - newCount * BoxHeightDamage) }); // destroy the cannonball CommandBuffer.DestroyEntity(entityIndex, entity); } }
/// <summary> /// Uses parabola to determine bounce position at time t. /// </summary> /// <param name="t">t in [0, 1].</param> private Vector3 bouncePosition(float t) { float y = Parabola.Solve(paraA, paraB, paraC, t); Vector3 startPos = TerrainArea.instance.LocalPositionFromBox(startBox.col, startBox.row); Vector3 endPos = TerrainArea.instance.LocalPositionFromBox(endBox.col, endBox.row); float x = Mathf.Lerp(startPos.x, endPos.x, t); float z = Mathf.Lerp(startPos.z, endPos.z, t); return(new Vector3(x, y, z)); }
public void Execute(Entity _, int index, [ReadOnly] ref LocalToWorld localToWorld, ref TankFireCountdown countdown, ref Rotation rotation) { // launching cannonball countdown.SecondsUntilFire -= DeltaTime; if (countdown.SecondsUntilFire <= 0) { countdown.SecondsUntilFire = math.fmod(countdown.SecondsUntilFire, FirePeriod) + FirePeriod; // start and end positions int2 playerBoxCoords = TerrainSystem.PositionToBlockCoords(PlayerPosition.x, PlayerPosition.z, TerrainSize); int playerBoxIndex = TerrainSystem.BlockCoordsToIndex(playerBoxCoords, TerrainSize); float playerBoxHeight = BlockHeights[playerBoxIndex]; float3 startPos = math.mul(localToWorld.Value, new float4(0, 0, 0, 1)).xyz; float3 endPos = TerrainSystem.BlockCoordsToPosition(playerBoxCoords, playerBoxHeight, TerrainSize); endPos.y += CANNONBALL_RADIUS + 0.01f; // extra 0.01 to avoid false positive collisions at endPos float distance = math.distance(endPos.xz, startPos.xz); float duration = distance / CANNONBALL_SPEED; if (duration < 0.0001f) { duration = 1.0f; } // binary searching to determine height of cannonball arc float low = math.max(startPos.y, endPos.y); float high = low * 2; float paraA, paraB, paraC; // look for height of arc that won't hit boxes while (true) { Parabola.Create(startPos.y, high, endPos.y, out paraA, out paraB, out paraC); if (!PathHitsTerrain(BlockBoundingBoxes, TerrainSize, startPos, endPos, paraA, paraB, paraC)) { // high enough break; } // not high enough. Double value low = high; high *= 2; // failsafe if (high > 9999) { return; // skip launch } } // do binary searches to narrow down while (high - low > CANNONBALL_PARABOLA_PRECISION) { float mid = (low + high) / 2; Parabola.Create(startPos.y, mid, endPos.y, out paraA, out paraB, out paraC); if (PathHitsTerrain(BlockBoundingBoxes, TerrainSize, startPos, endPos, paraA, paraB, paraC)) { // not high enough low = mid; } else { // too high high = mid; } } // launch with calculated height float height = (low + high) / 2; Parabola.Create(startPos.y, height, endPos.y, out paraA, out paraB, out paraC); Entity ballEntity = CommandBuffer.Instantiate(index, CannonballPrefab); CommandBuffer.SetComponent(index, ballEntity, new Translation { Value = startPos }); CommandBuffer.SetComponent(index, ballEntity, new ArcState { Parabola = new float3(paraA, paraB, paraC), StartTime = ElapsedTime, Duration = duration, StartPos = startPos, EndPos = endPos, }); // set cannon rotation { const float t = 0.01f; float altitudeAngle = -math.atan2( Parabola.Solve(paraA, paraB, paraC, t) - Parabola.Solve(paraA, paraB, paraC, 0.0f), t * distance); rotation.Value = quaternion.Euler(altitudeAngle, 0, 0); } } }
// Update is called once per frame void Update() { UpdateTranslation(); RotateTowardsPlayer(); time += Time.deltaTime; if (time >= Game.instance.tankLaunchPeriod) { // launching cannonball time -= Game.instance.tankLaunchPeriod; // start and end positions Vector3 start = transform.localPosition; Vector2Int playerBox = TerrainArea.instance.BoxFromLocalPosition(Player.instance.transform.localPosition); Vector3 end = TerrainArea.instance.LocalPositionFromBox(playerBox.x, playerBox.y, TerrainArea.instance.GetBox(playerBox.x, playerBox.y).top + Cannonball.RADIUS); float distance = (new Vector2(end.z - start.z, end.x - start.x)).magnitude; float duration = distance / Cannonball.SPEED; if (duration < .0001f) { duration = 1; } // binary searching to determine height of cannonball arc float low = Mathf.Max(start.y, end.y); float high = low * 2; float paraA, paraB, paraC; // look for height of arc that won't hit boxes while (true) { Parabola.Create(start.y, high, end.y, out paraA, out paraB, out paraC); if (!Cannonball.CheckBoxCollision(start, end, paraA, paraB, paraC)) { // high enough break; } // not high enough. Double value low = high; high *= 2; // failsafe if (high > 9999) { return; // skip launch } } // do binary searches to narrow down while (high - low > Game.instance.playerParabolaPrecision) { float mid = (low + high) / 2; Parabola.Create(start.y, mid, end.y, out paraA, out paraB, out paraC); if (Cannonball.CheckBoxCollision(start, end, paraA, paraB, paraC)) { // not high enough low = mid; } else { // too high high = mid; } } // launch with calculated height float height = (low + high) / 2; Cannonball cannonball = Cannonball.Create(transform.parent, start); cannonball.Launch(end, height, duration); // set cannon rotation SetCannonRotation(Mathf.Atan2(Parabola.Solve(paraA, paraB, paraC, .1f) - Parabola.Solve(paraA, paraB, paraC, 0), .1f) * Mathf.Rad2Deg); } }
public void Execute(ref Translation position, ref ArcState arc) { float t = ElapsedTime - arc.StartTime; if (t > arc.Duration) { // Compute the next arc info int2 playerCoords = TerrainSystem.PositionToBlockCoords(position.Value.x, position.Value.z, TerrainSize); int2 dir = math.clamp(MouseBoxCoords - playerCoords, new int2(-1, -1), new int2(1, 1)); int2 destCoords = playerCoords + dir; // If destination is outside the terrain bounds, don't move. if (destCoords.x < 0 || destCoords.x >= TerrainSize.x || destCoords.y < 0 || destCoords.y >= TerrainSize.y) { destCoords = playerCoords; } int playerBlockIndex = TerrainSystem.BlockCoordsToIndex(playerCoords, TerrainSize); int destBlockIndex = TerrainSystem.BlockCoordsToIndex(destCoords, TerrainSize); // If destination is occupied, don't move. if (IsBlockOccupied[destBlockIndex] != 0) { destCoords = playerCoords; destBlockIndex = playerBlockIndex; } float playerBlockHeight = BlockHeights[playerBlockIndex]; float destBlockHeight = BlockHeights[destBlockIndex]; float parabolaHeight = math.max(playerBlockHeight, destBlockHeight); if (dir.x != 0 && dir.y != 0) { // If jumping diagonally, the parabola height must take into account the directly // adjacent blocks as well. int2 adjacentCoordsX = playerCoords + new int2(dir.x, 0); if (adjacentCoordsX.x >= 0 && adjacentCoordsX.x < TerrainSize.x) { int adjacentIndexX = TerrainSystem.BlockCoordsToIndex(adjacentCoordsX, TerrainSize); float adjacentHeightX = BlockHeights[adjacentIndexX]; parabolaHeight = math.max(parabolaHeight, adjacentHeightX); } int2 adjacentCoordsZ = playerCoords + new int2(0, dir.y); if (adjacentCoordsZ.y >= 0 && adjacentCoordsZ.y < TerrainSize.y) { int adjacentIndexZ = TerrainSystem.BlockCoordsToIndex(adjacentCoordsZ, TerrainSize); float adjacentHeightZ = BlockHeights[adjacentIndexZ]; parabolaHeight = math.max(parabolaHeight, adjacentHeightZ); } } parabolaHeight += BOUNCE_HEIGHT; Parabola.Create(playerBlockHeight, parabolaHeight, destBlockHeight, out arc.Parabola.x, out arc.Parabola.y, out arc.Parabola.z); arc.StartPos = TerrainSystem.BlockCoordsToPosition(playerCoords, playerBlockHeight, TerrainSize); arc.EndPos = TerrainSystem.BlockCoordsToPosition(destCoords, destBlockHeight, TerrainSize); arc.StartTime = ElapsedTime; float distance = math.distance(playerCoords, destCoords); arc.Duration = math.max(1, distance) * BOUNCE_BASE_DURATION; } else { // Get the current height along the parabola float s = t / arc.Duration; position.Value = new float3( math.lerp(arc.StartPos.x, arc.EndPos.x, s), Parabola.Solve(arc.Parabola.x, arc.Parabola.y, arc.Parabola.z, s), math.lerp(arc.StartPos.z, arc.EndPos.z, s) ); } }