Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by nickyoso1 · Sep 06, 2012 at 03:01 PM · androidraycastaicarcorner

Car Ai - Calculate max corner speed

I have been working on a simple arcade racing ai, but the problem I have is cornering. Before each corner is a simple waypoint telling the car the angle of the corner. After receiving that info from the waypoint, the car will then start to turn. Problem with this is that the car doens't know if he has to accelerate or brake to clear the corner.

I was hoping there would be a way to make a circle based on the car's speed and turnrate, and use that circle to see if he would go off track or not. I'm sorry if I am being unclear about things, I'm not English so I don't know how to explain it very well.

edit: I have followed a tutorial that uses a waypoint system.. but that didn't work. I download the completed version of the tutorial and it had alot of bugs

edit2: See image below:

This is what I currently have. the car can now determine his trajectory with the following formula: r = v*t / 2*pi (radius = speed * time to turn full cirkel / 2*pi)

Now I just need to find a way to do a circular raycast.. The idea behind this is that if the raycast will hit rectangle 1 or 2, the car will know that it is going off track. I have tried Physics.OverlapSphere for this, but that function returns all colliders within that range, and I just want the colliders on the edge of the cirkel.

After having made this progress so-far, I don't want to use a waypoint system, because I think if I can make this work I could make the ai much more advanced than a waypoint system in terms of overtaking, and smarter use of braking and acceleration before/during corners.

alt text

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
2
Best Answer

Answer by Fattie · Sep 06, 2012 at 05:49 PM

Bonjour, your English is great

I really think you should try what I explain in the other post :)

But, the way to do what you're asking is fortunately very easy .. .just like this ...

alt text

just cast rays down, where the yellow Xs are .. you see ?

Simply start where the car is, and go forward (say 5 or 10 degrees at a time) around the imaginary circle.

You need only go say 100 degrees, and you are done, you can then drive.

Do not hesitate to ask any further questions !!!!!


You asked:

"However do I have to determine to position of the yellow Xs?"

Of course, it's only one line of code. Just use Mathf (see doco) for sine, cosine, etc.

An even better way to do it:

just make a "calculation" mechanism (ie, an empty game object) and sit it at the center of the circle.

Have a marker ("mmm") that sticks out the correct distance. Simply rotate this "calculation" object, by ten degrees at a time.

Just shoot from "mmm" - it saves you doing the high-school math!


You asked:

:The solution I just thought of was instantiating a gameobject at the center of the red circle and having a cube rotating around the gameobject with the radius as the distance to see if the cube would detect a collision. However if I could get your solution to work, then it would be better for the performance:"

You are truly thinking like a game programmer. Your life is set! :)

there are problems with that idea, it takes time! You have to swing it around!!!

Regarding the raycasts, you can do a few zillion of them all inside one frame you know?

Note too that you can be a smart-arse with a "hoop" that is on an ANGLE ... move it down, and the POINT of contact is like the exact first point if moving around, you know?

If you were going to do your "swinging invisible tester" .... do not forget too that .......

YOU COULD SIMPLY MOVE THE CAR !!! heh! it is everyday ion game engines that you take an invisible version of an actor, and you "move it ahead" to see what it can do. You know?

(For this, you must learn about the LAYERS system in Unity3D ... you know it? it's great, read up)

But these things are not relevant here....

I would perhaps, suggest, encourage you to do "ray casting along a possible path" as it is an extremely common technique, it is everyday.

(Do not forget, it may not be the red circle ... God knows, you may be trying a future path in a straight line, or all sorts of things ... ideally, you construct yourself a little OO object that will be useful to you generally !!!)

Note that you might have to try four or five attempted radii, each with a few raycasts .. it will take you maybe 30 raycasts .... you will do a kind of "binary search" through the space back and fore to find a nice path, I guess.

One small point: "it would be better for the performance".... not true, PjhysX is incredibly efficient AND it's always very hard IMO to guess what will be faster. Often it is counter intuitive. It could be that what we mention "swing a cat" is much quicker than what we mention "ten yellow shots" - it is really hard to say. Based on experience both are "absolutely no problem" -- just go right ahead and do not think one atom about performance at this point,

Hope it helps in some way

Sol one day someone will say my drawings are good, I still wait ... :-)


PS you know this right: on the track you have drawn....

You could PRECALCULATE, like today, now, the solution for say 10 entry positions across, at 5 angles each, at 5 speeds each, you generate a space of maybe 1000 solutions. (so for each one it tells you "brake this much" or whatever), store it in some data.

So, this is definitely an approach you see being used. At run time (drive time?) just look up the closest and use it.

of course, it is more fun to have a car that works itself :-)


trycircle.jpg (40.3 kB)
Comment
Add comment · Show 4 · 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
avatar image nickyoso1 · Sep 06, 2012 at 05:54 PM 0
Share

That is a great solution, However do I have to deter$$anonymous$$e to position of the yellow Xs?

The solution I just thought of was instantiating a gameobject at the center of the red circle and having a cube rotating around the gameobject with the radius as the distance to see if the cube would detect a collision.

However if I could get your solution to work, then it would be better for the performance

avatar image Fattie · Sep 06, 2012 at 06:14 PM 0
Share

I have added to my answer re: your comments ! Good night

avatar image nickyoso1 · Sep 06, 2012 at 07:39 PM 0
Share

I have tried doing it the following way: I create 2 cubes at the position of the yellow Xs and I do a raycast between them. Everything goes fine, except for the performance.. it drops from 1800 fps to 1 fps

avatar image Fattie · Sep 07, 2012 at 05:25 AM 0
Share

that seems weird! IF you wanted to do that, just have cubes standing by offscreen and move them in. Also, you don't need cubes for any reason at all to raycast, just raycast and see if you hit collider2. (You can cast from one point to another, you do not need cubes or anything)

But there is a tremendously easier way.

You just cast DOWN from OVERHEAD -- it's that simple. the first three yellow Xs will $$anonymous$$ISS collider2. and then the next two yellow Xs will HIT collider2.

very simple!

let's say the first X is at (in the xz plane) maybe 102.33,93.44.

assume your ground surface is near zero Y.

Just cast from (102.33, 2, 93.44) to (102.33, -2, 93.44).

You will thus find out if there is an obstacle (collider2) where the X is.

Do not cast from X to X. Just cast at the points X.

(If you feel you need more resolution, go for it .. make X every 5 degrees rather than 10 degrees.) Easy!

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

8 People are following this question.

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

Related Questions

AI create path, help. 1 Answer

How to implement traffic system efficiently in Unity game to run smoothly on mobile device? 1 Answer

Car AI Avoiding Obstacle 0 Answers

Have falling object exit from a collider after collision? 2 Answers

Raycast Car Physics 0 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