Search Unity

Best way to simulate a cannon with ricochet

Discussion in 'Scripting' started by raycosantana, Sep 17, 2014.

  1. raycosantana

    raycosantana

    Joined:
    Dec 23, 2012
    Posts:
    319
    Hi

    I want to make a game where you fire a cannon and the cannon ball ricochets from wall to wall, I tried using physics with addrelativeforce and a bouncy physics material but sometimes the cannon balls go through the walls because of the high speed, what is the best way to achieve this?
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Have you checked the collisionDetectionModes? My guess is that setting these correctly will solve the problem.
     
  3. raycosantana

    raycosantana

    Joined:
    Dec 23, 2012
    Posts:
    319
    Yes, I did try them all before posting, changing the cannon bullet to Continuous dynamic worked a little better but still 30% of the cannon balls go through walls, that's unacceptable. I guess my best bet is not using physics at all.
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    OK, just to be sure: did you also change the wall? (It should be set to Continuous.) If set properly, there shouldn't be anything balls through the walls — if there are, there is something more screwy. The physics engine can handle this.

    (Of course, sure, it's quite possible to lob a cannon ball and make it bounce of walls "manually," but it's a lot more work, and shouldn't be necessary.)
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    Try putting invisible box colliders "behind" the wall planes you want.

    It takes a lot more speed for the cannonball to pass through a thick box collider.

    See attached picture. The more complex your geometry, the more of a hassle this becomes, to mimic the collision with invisible thick boxes.

    Kurt
     

    Attached Files:

  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    If you use Continuous + ContinuousDynamic, there is no need for thick colliders.

    --Eric
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    Wow, nice one Eric... I tested it with the rigidbody set to continuous dynamic collision and you are correct, thin colliders work just fine!

    Thanks for ejumicating me!

    Kurt
     
  8. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    JoeStrout mentioned it first. ;) Keep in mind that continuous is slower, so it should only be used when absolutely necessary.

    --Eric
     
  9. raycosantana

    raycosantana

    Joined:
    Dec 23, 2012
    Posts:
    319
    I tried that but the balls are too fast and they get through the walls sometimes, I'm using raycast and Vector3.reflect to make them bounce which works great but when translating this method to 2D it doesn't work, regular raycast doesn't detect 2D colliders and Raycast2D doesn't work the same way.

    For example when doing Physics2D.Raycast you cant use out raycastHit it wants a vector2 instead so I cant get the normal from the RaycastHit to use Vector3.reflect().

    Physics.Raycast wants a Ray and a RaycastHit.
    Physics2D.Raycast wants two Vector2s instead of a Ray2D and a RaycastHit2D, why is that?.o_O
     
  10. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I've never seen an object get through a collider if it's using Continuous collision detection, no matter how fast it goes. I'm pretty sure it's not actually possible, since the physics simulation no longer simply translates the object in discrete steps. If you're using 3D physics, you need to make the obstacles Continuous and use ContinuousDynamic for the moving object.

    --Eric
     
  11. raycosantana

    raycosantana

    Joined:
    Dec 23, 2012
    Posts:
    319
    Yes I tried that before making this thread, I made the obstacles Continuous and the cannon balls continuous dynamic but the cannon balls go through the walls anyways, not all of them, but a high percentage of them; I'm working in 2D now, the 3D stuff was for prototyping so I would like help with Raycast2D if possible otherwise I will just add 3D colliders to the sprites and call it a day :D

    This is the code Im using for the cannon balls, right now this works for 3D but when I tried to convert it to 2D physics2D.Raycast() doesn't seem to work like Physics.Raycast() like I said in my previous post.

    Code (CSharp):
    1. public class projectile : MonoBehaviour {
    2.     public float Speed;
    3.     // Use this for initialization
    4.     void Start () {
    5.    
    6.     }
    7.  
    8.     // Update is called once per frame
    9.     void Update () {
    10.    
    11.         gameObject.transform.Translate(Vector3.right* Speed * Time.deltaTime);
    12.         Ray ray = new Ray (transform.position,transform.right);
    13.         RaycastHit hit;
    14.         if (Physics.Raycast(ray,out hit,Speed * Time.deltaTime + 0.5f)){
    15.             Vector3 reflectDirection = Vector3.Reflect(ray.direction,hit.normal);
    16.             float rot = Mathf.Atan2(reflectDirection.y, reflectDirection.x) * Mathf.Rad2Deg;
    17.             transform.eulerAngles =(new Vector3 (0,0,rot));
    18.         }
    19.     }
    20. }
    thanks for your time
     
    Last edited: Sep 20, 2014
  12. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Well, if your walls are made of straight lines and your cannon balls are circles, you could always throw out the Unity physics and just do it yourself. Collisions between moving circles and straight lines is fairly straightforward... you just extend your walls (in math, not in how they're drawn) by the radius of the circles, and then you've conceptually reduced the cannon ball down to a point (with zero radius). Now you see how far the cannon ball is going to go this frame, and from where it is to there defines a line segment, and you do line-line intersection tests with the (extended) walls.

    (But like Eric, I'm a bit skeptical, and would love to see a simple demo of ContinuousDynamic objects passing through Continuous obstacles.)