Crowd agents control

Is there any way to have a bit more control over crowd agents?
Like changing position or params after agent creation?

Pinging @Cedric

1 Like

Hi @Nevergrind

At the moment, the agents are autonomous agents: You can only control their next destination based on their creation parameters.

What is your use case? Which functionalities do you need ?

To set position:
crowd.setAgentPosition(index, position);

And to set agent parameters:
crowd.setAgentMaxSpeed(index, position)
or
crowd.setParam("maxSpeed", 5);

Need for actions like “teleport” of “equip boots”.

That seems doable! I’ll ping you when I have something :slight_smile:

1 Like

@ Cedric I am doing it like this for now:

In RecastJSCrowd constructor added this.agentParams = new Array();
in RecastJSCrowd.prototype.addAgent added agentParams.destination = pos;
In RecastJSCrowd.prototype.agentGoto added this.agentParams[index].destination = destination;
This is my final thing for now

    /**
     * Sets the agent position in world space
     * @param index agent returned by addAgent
     * @param pos Vector3
     * @returns agentIndex
     */
    RecastJSCrowd.prototype.setAgentPosition = function (index, pos) {
    	if(this.agents[index] == null) return false;
    	this.recastCrowd.removeAgent(index);
    	this.agents[index] = this.recastCrowd.addAgent(new this.bjsRECASTPlugin.bjsRECAST.Vec3(pos.x, pos.y, pos.z), this.agentParams[index]);
    	return this.agents[index];
    };

    /**
     * Sets the agent parameter
     * @param index agent returned by addAgent
     * @param param for agentParams
     * @param val value for param
     * @returns agentIndex
     */
    RecastJSCrowd.prototype.setAgentParam = function (index, param, val) {
    	if(this.agents[index] == null) return false;
    	if(this.agentParams[index][param] == null) return false;
    	this.recastCrowd.removeAgent(index);
    	this.agentParams[index][param] = val;
    	this.agents[index] = this.recastCrowd.addAgent(this.recastCrowd.getAgentPosition(index), this.agentParams[index]);
    	this.agentGoto(index, this.agentParams[index]["destination"]);
    	return this.agents[index];
    };

    /**
     * Gets the agent parameter
     * @param index agent returned by addAgent
     * @param param from agentParams
     * @returns param value
     */
    RecastJSCrowd.prototype.getAgentParam = function (index, param) {
    	if(this.agents[index] == null) return false;
    	if(this.agentParams[index][param] == null) return false;
    	this.agentGoto(index, this.agentParams[index]["destination"]);
    	return this.agentParams[index][param];
    };

    /**
     * Returns the agent destination in world space
     * @param index agent index returned by addAgent
     * @returns world space position
     */
    RecastJSCrowd.prototype.getAgentDestination = function (index) {
    	return this.agentParams[index]["destination"];
    };

I’ll upgrade the recast-detour WASM library for less creation/destruction. But for now, your changes should work.

1 Like

PRs are done!

In Babylonjs : recastjs update with agent parameters update and teleport by CedricGuillemet · Pull Request #7447 · BabylonJS/Babylon.js · GitHub
Extension: agent teleport and setter/getter for agent parameters by CedricGuillemet · Pull Request #192 · BabylonJS/Extensions · GitHub
Documentation : update params, teleport doc by CedricGuillemet · Pull Request #1840 · BabylonJS/Documentation · GitHub

1 Like