x


Determining rotation for collision avoidance?

Hi i was wondering what the best way to determine which way a character should rotate when it its ray returns a hit.

I have thought about casting two rays so that the one least close to the collier determines the direction, but i cant figure out how to code the ray direction.

any help is very appreciated.

Thanks

Brian

alt text

        // C# code version of JS provided by  Motionreactor below
        RaycastHit hit;
        Ray ray = Ray(gameObject.transform.position, Vector3.forward);

        if (Physics.Raycast(ray, hit, 10))
        {
            Debug.Log(hit.distance);
            Debug.DrawLine(ray.origin, hit.point);
        }
more ▼

asked Feb 26 '10 at 03:48 PM

sacredgeometry gravatar image

sacredgeometry
203 11 15 26

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

You can cast a ray in a specific direction like this:

var hit : RaycastHit;
var ray : Ray = Ray(gameObject.transform.position, Vector3.forward);
if(Physics.Raycast(ray, hit, 10))
{
    Debug.Log(hit.distance);
    Debug.DrawLine(ray.origin, hit.point);
}

Vector3.forward could be any Vector direction you wish.

more ▼

answered Feb 26 '10 at 04:37 PM

Motionreactor gravatar image

Motionreactor
395 1 5 16

Sorry my fault, i've got snow blindness from staring at too much code.

Feb 26 '10 at 05:05 PM sacredgeometry

:) heh we just went through deleting comments at the same time... so yeh, no worries

Feb 26 '10 at 05:06 PM Motionreactor

Oh, btw.. I remembered this awesome reference on autonomous steering: http://www.red3d.com/cwr/steer/

Feb 26 '10 at 05:35 PM Motionreactor

Thank you so much for the link that will help lots, currently my little guy is getting stuck in corners :D... an easy fix but i think theres a lot worse to come.

Feb 26 '10 at 09:00 PM sacredgeometry

heh, yeh I've done a little bit of this kind of thing before and experienced the same thing. There's always problem cases like that, but that's half the fun of it, making it smarter.

Feb 27 '10 at 02:38 AM Motionreactor
(comments are locked)
10|3000 characters needed characters left

This steering behavior and others are implemented in UnitySteer, which is a port of Craig Reynolds' OpenSteer (the reference on autonomous steering that MotionReactor linked to is Reynolds' as well).

The function you'd be interested on is steerToAvoidObstacles, or maybe steerToAvoidNeighbors.

Once you begin getting into the fnier points of steering, such as a group of vehicles attempting to behave reasonably when encountering each other, you might want to look at Mikko Mononen's Project Cane, as well the other articles he has posted on his site on the topic of velocity obstacles.

To close, I don't actually recommend you use raycasting for every aspect of navigation. For static areas like walls, a navigation mesh and AngryAnt's Path is a better option.

more ▼

answered Feb 27 '10 at 08:22 AM

Ricardo gravatar image

Ricardo
5.2k 20 32 96

Nice. I didn't know UnitySteer existed, Awesome! Thanks.

Feb 27 '10 at 02:49 PM Motionreactor

I will read, download and explore those libraries as soon as i have the time, and as always any help is appreciated so thank you.

Peace

Brian

Feb 28 '10 at 01:07 AM sacredgeometry

Those links are outdated!! can you guys give a new link?

Aug 27 '12 at 05:20 PM Karsnen
(comments are locked)
10|3000 characters needed characters left

Having done a "bit" of this stuff over the years, I agree with Ricardo's assessment cautioning you against extensive use of ray casting for navigation. The corner problem that you are encountering is a perfect example where ray casting, and more generally, purely reactive steering becomes problematic. For example, in corners, as you are turning away from the corner you typically wind up moving closer to one side of the corner or the other. A purely reactive solution (e.g., turn in the direction of most space) will cause you to move back into the corner and cause the oscillation you are seeing.

As Ricardo mentions, Mikko Mononen's website has a superb set of links to the latest papers in the field, most of which are based on a concept known as velocity objects (and variants such as reciprocal velocity objects). The big idea of velocity objects is that, based on the relative motion of 2 objects, it divides velocity space into those velocities (speed and direction) that are collision-free over some time horizon, and those velocities that will result in a collision over that time horizon. When tied to a planner that provides, in effect, a preferred velocity (namely one that will get you to the goal position), you can arrive at the closest velocity to your preferred velocity that will still be collision-free over the time horizon. At the end of the day you need a robust local steering (collision-avoidance) system as well as a nav. planner of some sort.

more ▼

answered Feb 27 '10 at 05:58 PM

bruceb gravatar image

bruceb
174 6 7 12

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2492
x1528
x959
x804
x48

asked: Feb 26 '10 at 03:48 PM

Seen: 2488 times

Last Updated: Aug 27 '12 at 05:20 PM