Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by brancaccio · Apr 14, 2020 at 12:02 PM · physics2dframeratedeltatimephysics 2d

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);
             }
         }
         
 
     }
 }
 


Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

128 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges