Earlier i asked how to get a mouse coordinates into world coordinates. The Tut-Chessboard.py examples suggests using CollisionNode(‘mouseRay’). I can then use the collision traverser( and associated classes) to get the object it collides with. The question is how do i get the points which the ray collided with? Its a 2D game so i really want a ray Plane collision.
If you are writing a 2d game then I suggest looking at the astroids tut.
I am writing a 2d game and merely just parent everything to render2d then check the distance between the object’s getPos() or I just use card textures and ignore the ‘y’ axis.
JB
Actually i found what seems to be a simple solution.
Part of what i was doing wrong was that i had the mouseTask actually be a separate task, but didnt ensure that the main loop fired first. But i just use a Collision ray, attach it to the camera, and then get the origin and direction of the ray with the mouse coords.
class World(DirectObject):
def __init__(self):
self.bg = loadObject("space", scale =300, depth = 10,
transparency = False) #Load the background starfield
self.ship = Ship()
self.accept("escape", sys.exit) #Escape quits
self.pickerRay = CollisionRay() #Make our ray
self.gameTask = taskMgr.add(self.gameLoop, "gameLoop")
self.gameTask.timeLast = 0 #Task time of the last frame
def gameLoop(self, task):
timeStep = task.time - task.timeLast
task.timeLast = task.time
self.ship.update(timeStep)
shipPos = self.ship.obj.getPos()
base.camera.setPos(shipPos[0],shipPos[1]-20,shipPos[2])
self.mouseTask()
return Task.cont
def mouseTask(self):
#Check to see if we can access the mouse. We need it to do anything else
if base.mouseWatcherNode.hasMouse():
#get the mouse position
mousepos = base.mouseWatcherNode.getMouse()
#Set the position of the ray based on the mouse position
if self.pickerRay.setFromLens(base.camNode, mousepos.getX(), mousepos.getY()):
nearPoint = render.getRelativePoint(camera, self.pickerRay.getOrigin())
nearVec = render.getRelativeVector(camera, self.pickerRay.getDirection())
nearVec.normalize()
print nearVec
return Task.cont