Move a thing to another thing. [SOLVED]

So, there are some things in my game. Surprising, I know. I have a certain desire to create motion among these things, which is a bold desire, I know. More specifically, I want to move one of these things towards another one, and that is where I’m having trouble.

One solution to this problem that I’m aware of is to have the moving thing lookAt(staticThing) and then use setY(movingThing, Amount) to move the movingThing forward along it’s own Y.

The problem is that I want to do a flocking algorithm too. That means, as I understand it, taking several prioritized directions and compiling them to get a final direction to move in. I don’t know how to generate these component directions from two points in coordinate space.

I made a little program to test a theory I had. The idea was to figure out the proportional distance I needed to move in x vs y, and divide the speed of the movement according to that proportion across the two axis. This is that program:

import direct.directbase.DirectStart
from direct.showbase.DirectObject import DirectObject
from LoaderClass import AssetLoader
from pandac.PandaModules import Point3

class World(DirectObject):
	def __init__(self):
	#This class will be the highest level in the game, and will serve to switch the game between modes as well as provide certain high-level
		self.assetLoader = AssetLoader()
		self.sphere = self.assetLoader.modelLoader("sphere.egg")
		self.goal = self.assetLoader.modelLoader("sphere.egg")
		self.speed = .025
		taskMgr.add(self.myTask, "myTask")
	def move(self):
		gX = self.goal.getPos().getX()
		gY = self.goal.getPos().getY()
		sX = self.sphere.getX()
		sY = self.sphere.getPos().getY()
		cX = gX - sX
		cY = gY - sY
		if(cX > cY):
			pro = cY / cX
			dirX = 1 - pro
			dirY = pro
		elif(cY > cX):
			pro = cX / cY
			dirX = pro
			dirY = 1 - pro
		elif(cY == cX):
			dirX = .5
			dirY = .5
		report = "Pos:" + str(self.sphere.getPos()) + "     dirX:" + str(dirX) + "     dirY:" + str(dirY)
		self.sphere.setPos(sX + (dirX * self.speed), sY + (dirY * self.speed), 0)
	def myTask(self, task):
		return task.cont
w = World()

This sort of works. The sphere object reaches the goal object, but it doesn’t follow a straight path to get there. It goes along a sort of curve. I’m not sure why that is.

I also have a feeling that there is probably some built in utilities that would make this whole thing incredibly easy and there are orangutans reading this right now and thinking I am an idiot for not using them. This not to imply that you, dear reader, are an orangutan. I leave that assertion to your own discretion.

Can someone, orangutan or not, throw me a bone on this one?

I looked into vector math a bit, and from what little I learned I was able to create what I think is a solution.

Firstly, I assume the movingThing to be at 0,0. That is, I work in it’s relative coordinate space. In that coordinate space, I find the location of the target object. This gives me the vector that describes where the movingThing needs to go. To normalize this vector, that is, give it a length of 1, I divide the destination object’s position in the relative coordinate space by the distance between the objects. Then I take that normalized vector and multiply it by my speed. Lastly, I take the speed-adjusted vector and add it to the position of my movingThing.

Like I said, I’m pretty sure this works. I’m still curious if there is a pre-built solution, though.

There is a nice solution if you want to look at it called pandasteer it can do point and click movement, random movement or direcitonal movement. I am not sure if he put flocking in there but this would at least give you an idea of how to smoothly get from point a to point b. Also the coodinates can be controled by Player or computer decide, also he does have follow, flee, obstical avoidance and more at least worth a glance if anything!

Almost forgot this was suggested to me:

It has flocking and various types of walking as well, def something for you to check out!

Thanks for the references, Chia. I’ve bookmarked them for later use. What I was really looking for, though, was something more along the lines of this bit I discovered recently:

If I use a Vec3 object for the directional vector, I can call Vec3.normalize() on it instead of having to calculate the distance myself and divide the vector by it.

I realize that there’s probably some of that in the references you showed me, and I’ll check them out later on if another question like this comes up.

For now, I have my vector finding code down to only two lines, a .getPos(reference object) and a Vec3.normalize(), so I’m pretty happy. I’m calling this one solved.