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
#necessities.
base.disableMouse()
self.assetLoader = AssetLoader()
self.sphere = self.assetLoader.modelLoader("sphere.egg")
self.goal = self.assetLoader.modelLoader("sphere.egg")
self.sphere.reparentTo(render)
self.goal.reparentTo(render)
self.sphere.setPos(-10,-10,0)
self.goal.setPos(10,7,0)
base.camera.setPos(0,0,50)
base.camera.setHpr(0,-90,0)
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)
print(report)
self.sphere.setPos(sX + (dirX * self.speed), sY + (dirY * self.speed), 0)
def myTask(self, task):
self.move()
return task.cont
w = World()
run()
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?