Crowd - Refresh Offset_Location of an Agent

Hi All,

First of all, please forgive my english… -__-

So, I’m trying to code a sample of a crowd simulation.
I want my agents follow one leader. Thats works in the scene but the problem start in the BGE.

I explain you with some screenshots :
Here it is the logic view of my leader.
http://www.edouardr.fr/res/img/projects/Leader_logic.png

Then you see there is an Always sensor where i run a script “moveAgents”
In this scripts I do some things but the most important (and where is the problem) is that I change the offset_location of each agent.


#Move all agents
def move_all_agents():
	'''
	Function    :
	__init__()
	Summary     :
	Arguments   :
	'''

	# bpy.ops.object.select_name(name=own.name)
	# cube = bpy.context.selected_objects[0]
	GetAgents()
	for a in agents:
		velocity = [0,0,0]
		position = [0,0,0]
		v1 = rule1(a)
		v2 = rule2(a)
		v3 = [0, 0, 0]	#rule3(a)
		v4 = [0, 0, 0]	#v4 = self.bound_position(a)
		v5 = followLeader(a, GetLeader())

		velocity[0] = v1[0] + v2[0] + v3[0] + v4[0] + v5[0]
		velocity[1] = v1[1] + v2[1] + v3[1] + v4[1] + v5[1]
		velocity[2] = 0 #a.getVelZ() + v1[2] + v2[2] + v3[2] + v4[2] + v5[2]

		a.location[0] = a.location[0] + velocity[0]
		a.location[1] = a.location[1] + velocity[1]
		a.location[2] = a.location[2] + velocity[2]
		move = a.game.actuators["move"]
		<b>move.offset_location.x = velocity[0]
		move.offset_location.y = velocity[1]
		move.offset_location.z = velocity[2]</b>
	#end for
#end move_all_agents

Here I show you the logic view of an agent :
http://www.edouardr.fr/res/img/projects/Agent_Logic.png

So finally, the offset_location (of each agents) is changed first time I run BGE (on press “P”) but their are not refresh, only if i stop BGE and run another time.

I hope you will understand and I thank you for the time you will spend on it.

triguN.

Well, thank you for your answer Monster.
I took account of your advice and I’m reading your pdf.
I also correct the little things like 2 same sensors = 1.

But just to go back to the problem, there is no way to refresh the offset_location during the game ?

There is no offset_location at the ObjectActuator (which is labeled Motion Actuator in Blender). See http://www.blender.org/documentation/blender_python_api_2_63_5/bge.types.html#bge.types.KX_ObjectActuator.

Thanks again Monster.

I understand better. So now I need to get each controllers of my agents to change their KX_ObjectActuator.dLoc propertie. Then I hope thats will work.

To do that I search in the forum how to register controllers of all my objects in a list.
I found that code :


import GameLogic

scene = GameLogic.getCurrentScene()
objList = scene.getObjectList()

try:
    GameLogic.DictObjects
    init=1
except:
    init=0
if init:
    cont = GameLogic.getCurrentController()
    own = cont.getOwner()
    name = own.getName()
    if not GameLogic.DictObjects.has_key(name):
        GameLogic.DictObjects[name]=cont
    own.init=0

First thing, I changed “scene.getObjectList()” by “scene.objects”. The function getObjectList() seems not exists anymore.

But GameLogic.DictObjects also don’t exists. Any Ideas ?
If I succeed to create my list like that, I would be able to change my “move_all_agents()” code like that :

SEE EDIT…

Do you think that will works ?

EDIT : I found another way to do this. I use bge.logic.globalDict

Then this script add to this globalList each agent I want to work with :


import GameLogic
import bge

try:
	init=1
except AttributError:
	init=0
if init:
	cont = GameLogic.getCurrentController()
	own = cont.owner
	name = own.name
	bge.logic.globalDict[name] = cont
	init=0

Then in my script “moveAgents” I do this :


#Move all agents
def move_all_agents():
	GetAgents()
	for a in agents:
		velocity = [0,0,0]
		position = [0,0,0]
		v1 = rule1(a)
		v2 = rule2(a)
		v3 = [0, 0, 0]	#rule3(a)
		v4 = [0, 0, 0]	#v4 = self.bound_position(a)
		v5 = followLeader(a, GetLeader())

		velocity[0] = v1[0] + v2[0] + v3[0] + v4[0] + v5[0]
		velocity[1] = v1[1] + v2[1] + v3[1] + v4[1] + v5[1]
		velocity[2] = 0 #a.getVelZ() + v1[2] + v2[2] + v3[2] + v4[2] + v5[2]

		if len(bge.logic.globalDict) != 0:
			# get the controller
			agentController =  bge.logic.globalDict[a.name]
			# get the object actuator attached to the controller named act
			contrActuatorMotion = agentController.actuators["move"]
			# set amount to be moved
			contrActuatorMotion.dLoc = [ velocity[0], velocity[1], velocity[2]]
	#end for
#end move_all_agents

But that don’t works… each agent take the first velocity I give, and then they go far far away… dLoc propertie isn’t refreshed

Hi Monster,

Big thanks for your contribution !

Juste before change my code and adapt it with your script I would like to show you what I’ve done until here.

So I attach a blend file where you will find 4 script Init, RegisterObject, moveAgents, cubeMove. Well you juste have to run Init.py script and then on the TOOLS panel you will find Crow Config section. Then click on Generate and press P to start the game.

With arrow’s keys move the leader. But you will see that agents go far in their own direction.

While that I modify script with yours.

Thank you.

triguN

Attachments

crowdSimulationFinalBGE.blend (425 KB)