- Home /
deltaTime doesn't work with custom gravity (ECS)
Hi, I'm making a 2d sandbox game, and I'm trying to make my own physics using ECS, but I have a problem with gravity: every frame I move all the entities that have a custom collider attached by an acceleration value * Vector2.down on the y axle, and I multiply that value for my deltaTime (I tried to use Time.deltaTime, but it doesn't work either) but it still remains frame-dependent.
Here's a video
Here's my code (I've put it on hastebin too if you prefere):
using Unity.Entities;
using UnityEngine;
public class CustomCollider : MonoBehaviour
{
[Tooltip("The box collider vertical offset from the center")]
public float verticalSize = 0;
[Tooltip("The box collider horizontal offset from the center")]
public float horizontalSize = 0;
public bool occupiedRight = false;
public bool occupiedLeft = false;
public bool occupiedUp = false;
public bool occupiedBottom = false;
public bool physicsActive = true;
public bool gravity = true;
public float mass = 1;
public float acceleration = 0;
}
class CustomPhysicEngine : ComponentSystem
{
public float gravity = 1f;
public float maxAcceleration = 300f;
float deltaTime = 0;
struct Components
{
public CustomCollider collider;
public Transform transform;
}
protected override void OnUpdate()
{
deltaTime = (Time.realtimeSinceStartup - deltaTime) / 10;
foreach (var e in GetEntities<Components>())
{
if (e.collider.physicsActive)
{
CheckCollisions(e);
if (e.collider.gravity) ApplyGravity(e);
}
}
}
void CheckCollisions(Components entity)
{
float x = entity.transform.position.x;
float y = entity.transform.position.y;
float verticalColliderSize = entity.collider.verticalSize;
float horizontalColliderSize = entity.collider.horizontalSize;
CustomCollider collider = entity.collider;
entity.collider.occupiedBottom = false;
entity.collider.occupiedUp = false;
entity.collider.occupiedLeft = false;
entity.collider.occupiedRight = false;
//This checks if one of the block's sides have a block next to them
for (float pos = x - horizontalColliderSize; pos <= x + horizontalColliderSize && !collider.occupiedBottom; pos++)
{
collider.occupiedBottom = WorldManager.CheckForBlock(new Vector2(pos, y - verticalColliderSize));
}
for (float pos = x - horizontalColliderSize; pos < x + horizontalColliderSize && !collider.occupiedUp; pos++)
{
collider.occupiedUp = WorldManager.CheckForBlock(new Vector2(pos, y + verticalColliderSize));
}
for (float pos = y - horizontalColliderSize + .1f; pos < y + verticalColliderSize && !collider.occupiedRight; pos++)
{
collider.occupiedRight = WorldManager.CheckForBlock(new Vector2(x + horizontalColliderSize, pos));
}
for (float pos = y - horizontalColliderSize +.1f; pos < y + verticalColliderSize && !collider.occupiedLeft; pos++)
{
collider.occupiedLeft = WorldManager.CheckForBlock(new Vector2(x - horizontalColliderSize, pos));
}
}
void ApplyGravity(Components e)
{
if (e.collider.occupiedBottom == false)
{
float mass = e.collider.mass;
e.transform.position += Vector3.down * e.collider.acceleration * deltaTime;
if (e.collider.acceleration < maxAcceleration * mass)
e.collider.acceleration += gravity * mass;
CheckCollisions(e);
AdjustPosition(e);
}
else
{
AdjustPosition(e);
e.collider.acceleration = 0;
}
}
void AdjustPosition(Components e)
{
// Adjust bottom
float y = (e.transform.position.y - e.collider.verticalSize + .1f);
float x = e.transform.position.x;
for (float x_ = x - e.collider.horizontalSize + .1f; x_ <= x + e.collider.horizontalSize - .1f; x_ += e.collider.horizontalSize)
{
while (WorldManager.CheckForBlock(new Vector2(x_, y)))
{
e.transform.position += new Vector3(0, .1f);
y = (e.transform.position.y - e.collider.verticalSize + .1f);
}
}
// Adjust left side
x = (e.transform.position.x - e.collider.horizontalSize + .1f);
y = e.transform.position.y;
for (float y_ = y - e.collider.verticalSize + .1f; y_ <= y + e.collider.verticalSize - .1f; y_ += e.collider.horizontalSize)
{
while (WorldManager.CheckForBlock(new Vector2(x, y_)))
{
e.transform.position += new Vector3(.1f, 0);
x = (e.transform.position.x - e.collider.horizontalSize + .1f);
}
}
// Adjust right side
x = (e.transform.position.x + e.collider.horizontalSize - .1f);
y = e.transform.position.y;
for (float y_ = y - e.collider.verticalSize + .1f; y_ <= y + e.collider.verticalSize - .1f; y_ += e.collider.horizontalSize)
{
while (WorldManager.CheckForBlock(new Vector2(x, y_)))
{
e.transform.position -= new Vector3(.2f, 0);
x = (e.transform.position.x - e.collider.horizontalSize + .1f);
}
}
}
}
Your answer
Follow this Question
Related Questions
Odd character dash discrepancies. Time.deltaTime? 2 Answers
I can't get framerate independence to work... 1 Answer
Problem with Fixed Timestep 0 Answers
Framerate Independent Firing Rate (delta time?) 2 Answers
How to make Two colliders, don't collide, but still be able of interact with each others 2 Answers