Jump to content

Character control like in a strategic game


osadchi
 Share

Recommended Posts

Снимок экрана 2016-10-13 в 21.59.45.png

Hello everyone, I need to find out coordinates when clicking on the ground.

Don't know is it correct directory for that question.

I have the FollowCamera, so when got click, need to get coordinates on the ground, don't even imagine how to do that. Need a theory.

 

//////////// added

Ok, I got it, Im using the ray. 

var ray = new BABYLON.Ray(camera.position, camera.getTarget().subtract(camera.position));

When I'm clicking it shows me the target position on the ground. But how I can create the ray from the cursor position and camera direction?

Is there some way to get cursor position relatively camera?

Link to comment
Share on other sites

Hello !

Babylon.js has some functions in the scene object for raycast like this one :

 scene.pick(scene.pointerX, scene.pointerY);


 


var x;
var z;

function clickf(){


   var pickResult = scene.pick(scene.pointerX, scene.pointerY);
    

    if (pickResult.hit) {
           x = pickResult.pickedPoint.x;
           z = pickResult.pickedPoint.z;
                   

    }// if result
    
}//clickf()

    
 window.addEventListener("click", function() {
 
    clickf();
});

A little example to move your character ;)

http://www.babylonjs-playground.com/#1JPHAD#1

Link to comment
Share on other sites

@osadchi

 There are better than the use of the scene.pick. Babylon to a function that does everything

scene.onPointerDown = function (evt, pickResult) {
        if (pickResult.hit) {
            impact.position.x = pickResult.pickedPoint.x;
            impact.position.y = pickResult.pickedPoint.y;
            impact.position.z = pickResult.pickedPoint.z;
        }
};

http://www.babylonjs-playground.com/#1VO5GO

Link to comment
Share on other sites

Thank everyone, I got something like this.

 

scene.onPointerDown = function(evt, pickResult) {

                if (pickResult.hit) {

                    if (pickResult.pickedMesh == ground1) { // if pickedMesh is a ground

                        sphere.position = pickResult.pickedPoint;

                    } else { // if not, trying to find it with a ray and picked position

                        var ray = new BABYLON.Ray(pickResult.pickedPoint, new BABYLON.Vector3(0,-1,0));
                        pickResult = scene.pickWithRay(ray, function(item) {

                            if (item == ground1) {

                                return true;

                            }

                            return false;

                        });

                        if (pickResult != null && pickResult.pickedPoint != null) {

                            sphere.position = pickResult.pickedPoint;

                        }

                    }

                }

 

Link to comment
Share on other sites

If you just want to recover the ground, your 'else{}' is useless because it will never use. 

For me 'if {}' and to 'else {}' are the same, so your 'if {}' is sufficient.

scene.onPointerDown = function(evt, pickResult) {
    if (pickResult.hit) {
        if (pickResult.pickedMesh == ground1) { // if pickedMesh is a ground

            sphere.position = pickResult.pickedPoint;

        }
    }
};

Otherwise if you want to recover any land or other object:

scene.onPointerDown = function(evt, pickResult) {
	if (pickResult.hit) {
		pickResult.pickedMesh.position = pickResult.pickedPoint;
	}
};

 

Link to comment
Share on other sites

Maybe he talks about pathfinding AI. I didn't see that here yet.

Very useful to program Non Player Characters movements.

 

You can look for A* algorithm, i think it's with 2d arrays.

Or make 2 raycasts (low range) from left and right shoulder of the bot pointing forward (in bot's space). The bot goes to the desired point and when one ray hits an obstacle, the bot has to turn in the direction where the obstacle is the shortest.

Or calculate where is the empty space before it moves.

Link to comment
Share on other sites

2 hours ago, osadchi said:

It will because I can get another mesh. If Im clicking on sphere or another mesh, it returns sphere, not ground.

scene.onPointerDown = function(evt, pickResult) {
	if (pickResult.hit) {
		pickResult.pickedMesh.position = pickResult.pickedPoint;
	}
};

This selects any type of mesh, either a sphere or land and you references the position of the click.

Link to comment
Share on other sites

Im talking about pathfinding. If we create a ray from the character position, what can get new ray from collision position?

I need to find the red - shortest way to the target. Blues are rays, from character and 2 from first collision... How use them to find the way?

Untitled-1.png

Link to comment
Share on other sites

^^ No. I edited my post.

The first ray is good. I mentionned 2 rays (for more precise obstacle detection) with short range (like 4 meters, to detect collision 4 seconds before it happens) pointing forward in bot's space.

But after the first pre-collision, the bot turns a little bit. And its rays are still pointing forward in local space. So he will turn smoothly.

 

This is a start method, you can add some functions.

Link to comment
Share on other sites

An idea can be and create a kind of moving a ghost mesh that can route all the paths and calculates what way was the course, without hindrance. this path is register memoir and character take this path. I do not know if the level of calculated time is the fastest, but I guess it can be a line of thought.

Or to make it faster, we send 3 ghost mesh and is the one who arrives first determines the path to take for the character. then "dispose()" has 3 famtomes :)

It's just an idea.

Link to comment
Share on other sites

Hi guys.  Yesterday, I was touring playgrounds, and I found this playground.  (Found while searching for 'boundingInfo').

Click around the outside edge (repeatedly).  Some kind of "pathfinding" is happening in that playground.

Not sure if useful, but I thought I should pass it to you. 

Also, busy forum member Sam Girardin has done some crowd sim work.  http://www.visualiser.fr/Babylon/Crowd/

(choose scene #5 or higher... for interesting pathfinding)

Be well.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...