News Forums RAIN General Discussion and Troubleshooting Avoid collision with other players/AI

This topic contains 24 replies, has 5 voices, and was last updated by  Mad_Mark 10 months, 2 weeks ago.

Viewing 10 posts - 16 through 25 (of 25 total)
  • Author
    Posts
  • #36878

    mandi
    Participant

    I think there is some kind of avoidance going on in my project, but as if something was nullifying it, it seems. Avoid vectors are being computed, but characters do not seem to move accordingly. I tried changing to different motor types, but the behaviour seems the same between all of them. A working example might help me try to work this out.

    #36879

    mandi
    Participant

    Multiplying the avoid vectors by 100 does not change the movement at all, hence the assumption they are not taken into account while moving characters.

    #36880

    Neyjay
    Participant

    Here’s my test project: RAIN Test

    If you just look at the Civilian BT, you might find what you’re looking for.

    #36883

    mandi
    Participant

    Thank you, I will look at it and let you know what was the problem.

    #36884

    Neyjay
    Participant

    Alright, I wish you good luck

    If you have any questions, don’t hesitate to ask. Oh and don’t worry about the other actions/behaviours. I just used my whole test project.

    #36895

    mandi
    Participant

    Thank you for your support. I checked your project, and avoidance works there fine. If I may suggest something, a lerp or iTween would smooth the motion while avoiding.
    I think I got it to work, but it looks pretty ugly, because my characters are pretty bulky - they are non humanoids. I will have to do some smoothing, or place several aspects of same name per character, to define their boundaries. For some reason I had to disable checking position on nav mesh - otherwise it wouldn’t work. Thank you very much for your help!

    #36899

    Neyjay
    Participant

    No problem. Where do you mean to use the iTween/lerp?

    #36905

    mandi
    Participant

    It was just a loose thought, but maybe lerping from 0 to the avoidVector would smooth the movement a little bit at AI.Motor.MoveTo(). Avoidance works for me, but yields quite jagged movement, as my characters are of substantial volume - I think I will play with this smoothing in several days (have other responsibilities now), and if I am successful, I will let you know.

    • This reply was modified 1 year, 7 months ago by  mandi.
    #37506

    Neyjay
    Participant

    I finally got to test it on the proper map, and started to see some flaws in my logic. I updated my code, to find avoidance points more accurately. Before if point A or point B (left and right) were not available it would fail to avoid, even if it would be possible to avoid. My update fixes this by searching for points around those 2 points and see if they are available.

    Here’s my code:

    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    using RAIN.Action;
    using RAIN.Core;
    using RAIN.Representation;
    using RAIN.Navigation;
    using RAIN.Navigation.Graph;
    using RAIN.Entities.Aspects;
    [RAINAction]
    public class AvoidCollision : RAINAction
    {
    	public Expression avoidRange;
    	private Vector3 _target;
    	private IList<RAINAspect> _targetsToAvoid;
    	private float range;
    	private Vector3 between;
    	private Vector3 avoidVector;
        public override void Start(RAIN.Core.AI ai)
        {
            base.Start(ai);
    		_targetsToAvoid = ai.Senses.Match("proximity", "aObj");
    		if(!float.TryParse(avoidRange.ExpressionAsEntered, out range))
    			range = 2f;
        }
        public override ActionResult Execute(RAIN.Core.AI ai)
        {
    		if(_targetsToAvoid.Count == 0)
    			return ActionResult.SUCCESS;
    		foreach(RAINAspect aspect in _targetsToAvoid) {
    			if(IsTooClose(ai, aspect))
    				DoAvoidance(ai, aspect);
    		}
            return ActionResult.SUCCESS;
        }
        public override void Stop(RAIN.Core.AI ai)
        {
            base.Stop(ai);
        }
    	private bool IsTooClose(AI ai, RAINAspect aspect) {
    		float dist = Vector3.Distance(ai.Kinematic.Position, aspect.Position);
    		if(dist <= range)
    			return true;
    		return false;
    	}
    	private void DoAvoidance(AI ai, RAINAspect aspect) {
    		between = ai.Kinematic.Position - aspect.Position;
    		avoidVector = Vector3.Cross(Vector3.up, between);
    		Vector3 avoidPoint;
    		int direction = Random.Range(0, 100);
    		avoidVector.Normalize();
    		if(direction < 50)
    			avoidVector *= -1;
    		avoidPoint = GetPositionOnNavMesh(avoidVector, ai);
    		if(avoidPoint == Vector3.zero)
    		{
    			avoidVector *= -1;
    			avoidPoint = GetPositionOnNavMesh(avoidVector, ai);
    		}
    		if(avoidPoint == Vector3.zero)
    		{
    			Debug.Log("Avoid not possible!");
    			// Change destination
    			ai.WorkingMemory.SetItem("hasArrived", true);
    			return;
    		}
    		ai.Motor.MoveTo(ai.Kinematic.Position + avoidPoint);
    		Debug.Log (ai.Body.name + " is avoiding " + aspect.Entity.Form.name);
    	}
    	private Vector3 GetPositionOnNavMesh(Vector3 loc, AI ai) {
    		Vector3 avoidPoint;
    		RAIN.Navigation.Pathfinding.RAINPath myPath = null;
    		int tries = 0;
    		do {
    			avoidPoint = new Vector3(loc.x + Random.Range(-0.8f, 0.8f),
    			                         loc.y,
    			                         loc.z + Random.Range(-0.8f, 0.8f));
    			tries++;
    			if(tries >= 1000)
    				return Vector3.zero;
    		} while(Vector3.Distance(loc, avoidPoint) > 1f && !ai.Navigator.GetPathTo(avoidPoint, 10, true, out myPath));
    		return avoidPoint;
    	}
    }

    You can increase the amount of “tries” you want the loop to go through to find a point. Still can be improved, but I just wanted to fix my flaw from earlier.

    • This reply was modified 1 year, 7 months ago by  Neyjay.
    • This reply was modified 1 year, 7 months ago by  Neyjay.
    #39809

    Mad_Mark
    Participant

    Thank you for sharing this project, Neyjay! This helps a lot with learning how to make this asset work!
    I have tried to replicate with modification, building on your example structure. I seem to have run into a roadblock with the “home1/home2” Navigation Targets. How did you get the Mount Point field to show up on those? My NavPoints only have a Range field.

    Moocho Gracias, and yes, I do mean mooch…
    Mark

Viewing 10 posts - 16 through 25 (of 25 total)

You must be logged in to reply to this topic.