Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Ponderline · Mar 14, 2018 at 05:39 PM · 2dcollisionphysicsphysics2dcast

Precise Collision Detection with Rigidbody2D.Cast

Greetings.

My question is if Rigidbody2D.Cast can be used for pixel-perfect collision checking. I recently came across this blog which discusses a method for collision detection in 2D games. https://zackbellgames.com/2014/10/28/understanding-collision-basics/

I desired to recreate this collision detection in unity by replacing place_meeting with a implementation of Rigidbody2D.Cast. Note that the pixel to unit size for all objects is 1:1. The following is my attempt.

However, I seem to be getting collisions both vertically and horizontally when the object is pressed against one or the other. In other words, the object counts as blocked on both axis when only blocked on one. I'm struggling to determine if this is a result of my own implementation error, or if Rigidbody2D.Cast simply cannot be used for pixel-perfect collision. Any help or advice would be welcome.

Thank you!

Physics Object

 public class PlatformerPhysicsObject : MonoBehaviour
 {
     //Modifiable Values
     public float gravityModifier = 1f;
 
     //Velocity and Position
     protected int vy = 0;
     protected int vx = 0;
     protected int x = 0;
     protected int y = 0;
 
     // Physics Body and Collider
     protected Collider2D col;
     protected Rigidbody2D body;
     protected ContactFilter2D contactFilter;
 
     void Awake()
     {
         body = GetComponent<Rigidbody2D>();
         col = GetComponent<Collider2D>();
         updatePosition((int)body.position.x, (int)body.position.y);
         SetupContactFilters();
     }
 
     protected virtual void ComputeVelocity()
     {
 
     }
     void applyGravity()
     {
         vy += -1;
     }
     void FixedUpdate()
     {
         applyGravity();
         ComputeVelocity();
         Movement();
 
     }
     void Movement()
     {
         for (int i = 0; i < Mathf.Abs(vy); i++)
         {
             if (!placeMeeting(x, y + (int)Mathf.Sign(vy)))
             {
                 y += (int)Mathf.Sign(vy);
                 updatePosition(x, y);
             }
             else
             {
                 vy = 0;
                 break;
             }
         }
         for (int i = 0; i < Mathf.Abs(vx); i++)
         {
             if (!placeMeeting(x + (int)Mathf.Sign(vx), y))
             {
                 x += (int)Mathf.Sign(vx);
                 updatePosition(x, y);
             }
             else
             {
                 vx = 0;
                 break;
             }
         }
 
     }
     void updatePosition(int xi, int yi)
     {
         Vector2 pos = new Vector2(xi,yi);
         body.position = pos;
     }
 
     bool placeMeeting(int xi, int yi)
     {
         RaycastHit2D[] result = new RaycastHit2D[8];
         Vector2 direction = new Vector2(xi,yi);
         int count = body.Cast(direction.normalized, contactFilter, result, 1);
         return (count > 0);
     }
 
     void SetupContactFilters()
     {
         contactFilter.useTriggers = false;
         contactFilter.SetLayerMask(Physics2D.GetLayerCollisionMask(gameObject.layer));
         contactFilter.useLayerMask = true;
     }
 }
 

Player

 public class PlatformerPlayerController : PlatformerPhysicsObject {
     public int maxSpeed = 7;
     public int jumpTakeOffSpeed = 100;
 
 
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void Update () {
         
     }
     protected override void ComputeVelocity()
     {
 
         vx = (int) Input.GetAxisRaw("Horizontal") * maxSpeed;
 
         if (Input.GetButtonDown("Jump"))
         {
             vy = jumpTakeOffSpeed;
         }
         else if (Input.GetButtonUp("Jump"))
         {
             if (vy > 0)
             {
                 vy = (int) ((float) vy * 0.5f);
             }
         }
     }
 }
 
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by DunderAxel · Apr 01, 2019 at 02:24 PM

@Ponderline - Did you figure out a solution for this issue?

Comment
Add comment · Share
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

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

225 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 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

Calculate initial velocity for projectile to reach point at a given angle and calculate angle to reach point at a given velocity. 1 Answer

How to rotate a GameObject with another GameObject while simulating gravity? 2 Answers

Why Does Object Keep Getting Faster? 1 Answer

Is there a way to lock velocity? 3 Answers

No collision detected with 2D rigidbody 1 Answer


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