Search Unity

Arcade Kart Physics

Discussion in 'Scripting' started by Baskyn, Feb 22, 2013.

  1. Baskyn

    Baskyn

    Joined:
    Feb 17, 2013
    Posts:
    67
    I've been trying to find a solution to this for a few weeks with very few breaks. I'm trying to create a car with Mario kart like physics for the iOS. You can't flip over(you stay parallel to the road), you can only turn a certain amount, you can only turn/drift when you are holding a turn button, otherwise you're just driving straight, you can't over-drift and spin around, Hills don't have much affect on the kart, etc. I'm sure you have all seen Mario Kart physics.


    What I would like is a point in the right direction. I have found literally nothing on non-realistic car physics. The only examples and help with car I can find is realistic car, after realistic car. I was trying to modify the Unity Car tutorial but it was just way too realistic. Even if I modified it, it couldn't get to what I wanted it to be and it would waste too much memory with extra physics it doesn't need. I've heard of raycasts, but they seem to have the same physics as WheelCollider cars.

    Does anyone know how these physics are done, or where I can go to learn how to do it?

    Thanks! :D

    EDIT: In case this confuses anyone. No I do not want you to write the code, I would like some help writing it maybe, but that's it. What I would really like is to know HOW to do this, as I have no clue.
     
    Last edited: Feb 22, 2013
  2. fogman

    fogman

    Joined:
    Jan 21, 2009
    Posts:
    71
    Basically I would do a raycast from every corner towards the racetrack.
    Based on the distance to the hitpoint I would then set a vehicle height and the roll and tilt values of the vehicle.

    Here you can see an example, not done with Unity, but the basic principle is the same:
    http://www.indiegamedeveloper.org/?page_id=75〈=en
     
  3. Baskyn

    Baskyn

    Joined:
    Feb 17, 2013
    Posts:
    67
    Do you know of any examples projects or anywhere that I could look at for raycasts? I've read about some that used to be around here, but now people just use wheel colliders. The raycast examples that I'm talking about aren't available anymore. There was one by Forest Johnson, but it's gone and there was one made by the Unity team, but it's gone.

    Also, How would you go about setting roll and tilt values with a raycast? I don't really know much about raycasts myself.
     
    Last edited: Feb 22, 2013
  4. wccrawford

    wccrawford

    Joined:
    Sep 30, 2011
    Posts:
    2,039
    I think you're finding nothing on arcade racing because it's assumed to be easy, where realistic physics is a lot trickier.

    I think you should start from scratch. Don't use the physics engine. Don't use Wheel Colliders. Create a box with a rigibody and make it pivot under the back axle. Add controls to rotate around the pivot on horizontal input and move (translate) forward or backward on the vertical input axis.

    For the raycast, the idea is to shoot a ray down from the car to the terrain below. The hitinfo that it returns will contain the normal for the surface it hit. That will give you a direction that you need to tilt the car to make it look like it's on the surface of a hill.

    Explained here:

    http://answers.unity3d.com/questions/168097/orient-vehicle-to-ground-normal.html
     
  5. Baskyn

    Baskyn

    Joined:
    Feb 17, 2013
    Posts:
    67
    Thanks, I'll look at that stuff tomorrow and see what I can come up with.

    I have been making 2d games for years, and just recently got into 3d games. That's why I don't know much of raycasts. Even so, I can find info on easy things, but nothing on raycast vehicles or arcade vehicles.
     
  6. fogman

    fogman

    Joined:
    Jan 21, 2009
    Posts:
    71
    Yeah, that would be the first and easiest approach. It´s a good start.
    You won´t be able to solve curbs with it, for this you´ll need a raycast at each wheel midpoint.
     
  7. Baskyn

    Baskyn

    Joined:
    Feb 17, 2013
    Posts:
    67
    EDIT: I have been working with the alternate physics for the car tutorial for the ray casts, and I'm having an issue. All 4 wheels move to the center of the car and just flicker.

    Here's a picture of what's happening:



    Anyone know what is happening?
     
    Last edited: Feb 23, 2013
  8. Baskyn

    Baskyn

    Joined:
    Feb 17, 2013
    Posts:
    67
    Ok, so I fixed the wheels going to the center. I had to set them up like the Unity car. I had to have the mesh attached to another wheel object that held the code. I have yet another problem. The wheels don't do anything. They just go through the ground. I can hold forward and they spin, but they don't move the car or affect it.
     
  9. TheRaider

    TheRaider

    Joined:
    Dec 5, 2010
    Posts:
    2,250
    I agree with previous poster make your own.

    The box should be your primary controller and the wheels will be children. Then you would just write a script to make them spin, you wouldn't spin them realistically.

    Using the physics engine for this task really isn't the way to go.
     
  10. Baskyn

    Baskyn

    Joined:
    Feb 17, 2013
    Posts:
    67
    If you use a rigid body, aren't you already using the physics engine?
    I've never used raycasts, and the only vehicle raycast example is the unity car tutorial, which aims to be realistic. Could the unity car tutorial be stripped down, so it gets rid of the things I don't need?

    I fixed the wheels though, the rotation was off. it works now, except I can't drift.

    EDIT: Where would you have me start to learn a simple raycast wheel? I've been looking at the unitycar raycast wheel and it has a bunch of stuff I don't need and have no clue what is safe to get rid of.
     
    Last edited: Feb 23, 2013
  11. TheRaider

    TheRaider

    Joined:
    Dec 5, 2010
    Posts:
    2,250
    personally I wouldn't even use a rigidbody, i would use a character controller and then code the movement myself.

    I would keep control of speed in a variable (and decrease and increase manually using input on vertical axis).

    I would do a similar thing with the the turning if i desired.

    Then I would place a raycast at each wheel pointing down and use that to calculate the angle of the controller relative to ground.

    To handle the dift you just need to use the vector right and move in that direction as well as the normal forward (and perhaps angle the model a little).
     
    Last edited: Feb 23, 2013
  12. Baskyn

    Baskyn

    Joined:
    Feb 17, 2013
    Posts:
    67
    Can character controllers be used for AI cars? I will be needing them too.

    EDIT: How would you simulate a car ramping off ramps with a character collider if it doesn't have physics? Also, can you use addForce and addTorque with a character controller?

    sorry if I'm being naive, this is my first project so I've never used a character collider or anything for that matter ^_^
     
    Last edited: Feb 23, 2013
  13. TheRaider

    TheRaider

    Joined:
    Dec 5, 2010
    Posts:
    2,250
    You can certainly do AI, just your AI will be a bit different.

    jumping off ramps shouldn't be an issue since character controllers can have characters jumping.

    You can't use addForce or addTorque, but you can simulate them (which you want to do anyway since you don't want it to happen realistically.


    I used this method in a simulation i made of a rover moving over mars, but it was meant to move slowly unlike your system.

    http://www.youtube.com/watch?v=n3a3vLQHH5g <-- that is an early version of it.
     
  14. Baskyn

    Baskyn

    Joined:
    Feb 17, 2013
    Posts:
    67
    How much different would they be?

    How would you trigger a "Jump" if there are no physics?


    That's a cool project you have!

    I think I'm starting to get the concept. I just don't understand how to make a raycast wheel. Not even a simple one. Especially not with a character controller, because the Unity car example(the only raycast vehicle I can find) uses physics for it. Do you know any examples of what I need, so I can see how it works? After I get the wheel raycasts, I think I know the logic behind the rest and could probably implement it. I would more than likely need a bit of help.


    Btw, I really appreciate your help. I wouldn't be anywhere with this if it weren't for you guys here helping me out.

    EDIT: You HAVE to use a capsule collider with a character controller. That's not exactly what I need...
     
    Last edited: Feb 23, 2013
  15. TheRaider

    TheRaider

    Joined:
    Dec 5, 2010
    Posts:
    2,250
    http://answers.unity3d.com/questions/43792/how-to-jump-with-the-character-controller.html <--- how to make a character controller jump


    You can always make child objects with the right shaped colliders.

    You still use the physics raycast. Sorry if i was confusing.

    Here is the basic process i used. It won't work in isolation but this gives you the idea. I then measured the difference in angles between the hits to make it touch the ground. I don't know if it is the best method but worked for me!

    Code (csharp):
    1.  
    2.  
    3.         //---------raycasting in order to slope rover ----------------
    4.        
    5.         var up = transform.TransformDirection(Vector3.up);
    6.         var hit: RaycastHit;
    7.         // Bit shift the index of the layer (8) to get a bit mask
    8.         var layerMask = 1 << 8;
    9.         // This would cast rays only against colliders in layer 8.
    10.         var rayDistance =3.0;
    11.  
    12.         //--------------raycast 4 points for slope------------------   
    13.         if (Physics.Raycast(GameObject.Find("RayFront").transform.position, -up, hit, rayDistance, layerMask))
    14.             hitPoints[0] = hit.point;
    15.         if (Physics.Raycast(GameObject.Find("RayBack").transform.position, -up, hit,rayDistance, layerMask))
    16.             hitPoints[1] = hit.point;
    17.         if (Physics.Raycast(GameObject.Find("RayLeft").transform.position, -up, hit,rayDistance, layerMask))
    18.             hitPoints[2] = hit.point;
    19.         if (Physics.Raycast(GameObject.Find("RayRight").transform.position, -up, hit,rayDistance, layerMask))
    20.             hitPoints[3] = hit.point;
    21.  
    22.  
    23.  
    From memory with my rover I actually had to put code to make it stay on the ground cause if you turned the speed up too much you could launch off the hills (and outside the boundaries of the marsyard in the simulation lol).
     
    Last edited: Feb 23, 2013
  16. Baskyn

    Baskyn

    Joined:
    Feb 17, 2013
    Posts:
    67
    Yeah, I know how to make a character controller jump. The problem is telling it when to jump. How is it supposed to know to jump? What triggers it? In the physics engine it does it on its own.

    What's the physics raycast?

    So in your code What is a layerMask? Also, what are you supposed to do with the 4 points for a slope?


    Here is what I have in my head for the car. This version would use the physics engine, but very minimally.


    The red lines are the rays. The "wheels" wouldn't actually act like wheels. I don't want grip or slip or anything like that. They would be for smooth movement. The stabilizing ray would a long ray cast down from the center of the car. It would be the leveler for ramping and things to keep it from flipping. Basically it would keep you level with whatever ground you are on, instead of flipping everywhere.
    The driving code would be done separate and would consist of me controlling the way the car moves rather than just adding slip, RPM, gears and such. Those are unnecessary for my car and will only keep it from doing what I want.
     
  17. TheRaider

    TheRaider

    Joined:
    Dec 5, 2010
    Posts:
    2,250
    I haven't really done much on car mechanics and decieded that way wasn't for me so on that method I am not the right person to give advice, however if you get it working arcade style I would be interested to see :)

    In mariokart a some of the jumps are activated by triggers because they are like mega jumps.

    I believe with a character controller you just be able to run over a ramp and it be handled to a degree so long as you have gravity like that jump example. However the half pipe type jumps in mario kart I am not sure what would be the best way to handle and would be the spot where the kart would flip.
     
  18. Baskyn

    Baskyn

    Joined:
    Feb 17, 2013
    Posts:
    67
    I would imagine on the half pipes you would have some sort of invisible wall that acts as the "ground" for the stabilizer. The only issue I'm having so far is the raycast wheels. I still don't understand raycasts very well and have no clue how to implement them the way I need. I've found a bunch of stuff on raycasts, but they aren't helpful to me because they are for things like shooting.

    Using triggers for every jump wouldn't be optimal, since even little bumps can make you go in the air.
     
  19. Baskyn

    Baskyn

    Joined:
    Feb 17, 2013
    Posts:
    67
    Ok, so I'm attempting to make the car with just a box collider, and it slides around on it. It can move forward and backward, but it slowly rotates, probably due to no restrictions. Would switching the rotation and location constraints on and off help, or is there something else for this?

    I will be using a raycast eventually to keep the vehicle level with the ground. Hopefully at least.

    EDIT: Not sure this is going to work. I'm using transform which doesn't allow the use of physics apparently and you can go through walls. There's also loads of things that probably won't work with this. I'm gonna keep trying though.
     
    Last edited: Feb 24, 2013
  20. Baskyn

    Baskyn

    Joined:
    Feb 17, 2013
    Posts:
    67
    Ok, with some research I have decided the way I will be implementing this. I would like to know if my way of doing this will work.

    I will not be using wheel colliders or raycasts for wheels. it will be a simple box collider that I move around using code. I will use a state machine to tell what state you're in(driving, air, hurt, etc.). For keeping the car upright in midair, I'll be using either built in rotations or iTween animation to tell the car what to do in each state. I may use a raycast or a piece of code I have to keep you upright if it's needed, but I hope to not need to.

    Now I have a question for this. What would be the best way to move the Kart without using any of the addTorque or other physics based "pushing"? I tried transform.position, but it seems that it ignores collisions, and you go through walls. You also dont keep moving if you ramp off something and let go of the gas button. You just stop and fall where you're at. How would movePosition do for this? What do you think would be the best way to move it?
     
    Last edited: Feb 27, 2013
  21. Moix

    Moix

    Joined:
    Sep 6, 2012
    Posts:
    12
    Hey everyone. Sorry to resurrect an old thread.

    Hey OP, what was your final approach?

    I'm into the same problem. People here are suggesting to not use the Physics engine, but then this means doing a lot of stuff by hand (drifting, inertia, collisions, etc). Isn't there a way to use the Physics engine in a way that doesn't produce realistic results?
     
  22. FunghettoGio

    FunghettoGio

    Joined:
    Mar 13, 2014
    Posts:
    3
    I am also working on an arcade game similar to mario kart.

    I've ended up using rigidbody because it's very useful for collisions and also to manage all the basic physic stuff, but didn't use wheel colliders.

    I have implemented the steering by calculating the angular speed at which the car is supposed to rotate around its self and the relative deltaAngle. Then I calculate a rotation by deltaAngle along transform.up axis and apply the rotation to both the car transform and the rigidbody.velocity vector.

    Code (CSharp):
    1. Quaternion deltaAngle =
    2. Quaternion.AngleAxis (Mathf.Rad2Deg * AngularSpeed * Time.deltaTime, transform.up);
    3. transform.Rotate (deltaAngle.eulerAngles);
    4.          
    5. Vector3 v = rigidbody.velocity;
    6. v = deltaAngle * v;
    7. rigidbody.velocity = v;
    For drifting it's the same thing, i just have to calculate the angular speed in a different way and add an animation to the car so it looks rotated along the mid point of upper wheel axis.