grid game help plz

helo im a bit new here… :blush:
im making a game using the grid like the chessboard tutorial.
i already loaded a model on one of the squares, and make it move around…

my question is. how to make the square on which the model is highlighted as the model move around the grid

thx a lot

i think your question is to general. How do you store all models?

grid100x100 = [[None]*100 for i in xrange(100)]

If you store your objects in a grid then you can have x,y be highlighted positions.

Then you can also have all objects be in a list (if the grid is empty most of the time)

allbojects = [nodepath,nodepath ... ]

And just hold a reference to it selected =

It all up to you and how you structure your program.

actually i loaded a model wizard and loaded it as such:

def SquareWiz(i):
  return Point3((i%8) - 3.5, int(i/8) - 3.5, 0.5)

self.pieces = self.wizard.setPos(SquareWiz(1)) 

the grid is as follows:

      for i in range(64):
          self.squares[i] = loader.loadModelCopy("models/square")
          self.squares[i].reparentTo(self.squareRoot)
          self.squares[i].setPos(SquarePos(i))
          self.squares[i].setColor(SquareColor(i))
          self.squares[i].setScale(1)

and for moving around

[/code]

  def move(self, task):
      elapsed=task.time - self.prevtime
      
      if (self.keyMap["left"]!=0):
          self.wizard.setH(self.wizard.getH() + elapsed*200)
      if (self.keyMap["right"]!=0):
          self.wizard.setH(self.wizard.getH() - elapsed*200)
      if (self.keyMap["forward"]!=0):
          backward = self.wizard.getNetTransform().getMat().getRow3(1)
          backward.normalize()
          self.wizard.setPos(self.wizard.getPos() - backward*(elapsed *0.8))

great!

one last thing… :S

how to add a lensnode to a model? can be done? :blush:

thx lots

no it cant… lens can only be added to cameras. Camera can be added to a model though. And you can add camera to a display region which is part of your screen. Is this some thing you want?

Or you want a lens node which deforms your model some how?

      wpos = self.wizard.getPos()
      self.pickerRay.setFromLens(base.camNode, wpos.getX(), wpos.getY())
      self.picker.traverse(self.squareRoot)
      if self.pq.getNumEntries() > 0:
          self.pq.sortEntries()
          i = int(self.pq.getEntry(0).getIntoNode().getTag('square'))
          self.squares[i].setColor(HIGHLIGHT)

i retrieve the position of the wizard, then i find the square on which he is… so i highlight the square… but… the part “base.camNode” is not good since the node, when i display it, is not on the model…

how can i attach it to the model? :s
[/code]

here is the full code :


import direct.directbase.DirectStart
from pandac.PandaModules import CollisionTraverser,CollisionNode
from pandac.PandaModules import CollisionHandlerQueue,CollisionRay
from pandac.PandaModules import AmbientLight,DirectionalLight,LightAttrib
from pandac.PandaModules import TextNode
from pandac.PandaModules import Point3,Vec3,Vec4,BitMask32
from direct.gui.OnscreenText import OnscreenText
from direct.actor.Actor import Actor
from direct.showbase.DirectObject import DirectObject
from direct.task.Task import Task
import sys

BLACK = Vec4(0,0,0,1)
WHITE = Vec4(1,1,1,1)
HIGHLIGHT = Vec4(0,1,1,1)
PIECEBLACK = Vec4(.15, .15, .15, 1)


def PointAtZ(z, point, vec):
  return point + vec * ((z-point.getZ()) / vec.getZ())
def SquarePos(i):
  return Point3((i%8) - 3.5, int(i/8) - 3.5, 0)
def SquareColor(i):
  if (i + ((i/8)%2))%2: return BLACK
  else: return WHITE
def SquareWiz(i):
  return Point3((i%8) - 3.5, int(i/8) - 3.5, 0.5) 

class World(DirectObject):
  def __init__(self):
      
      self.accept('escape', sys.exit)              
      base.disableMouse()                         
      camera.setPosHpr(0, -13.75, 6, 0, -25, 0)    
      
      self.picker = CollisionTraverser()
      self.pq     = CollisionHandlerQueue()
      
      self.pickerNode = CollisionNode('mouseRay')
              
      self.squareRoot = render.attachNewNode("squareRoot")
    
      #For each square
      self.squares = [None for i in range(64)]
      self.pieces = [None for i in range(64)]
      for i in range(64):
          self.squares[i] = loader.loadModelCopy("models/square")
          self.squares[i].reparentTo(self.squareRoot)
          self.squares[i].setPos(SquarePos(i))
          self.squares[i].setColor(SquareColor(i))
          self.squares[i].setScale(1)
          
          self.squares[i].find("**/polygon").node().setIntoCollideMask(
          BitMask32.bit(1))
          self.squares[i].find("**/polygon").node().setTag('square', str(i))

          self.keyMap = {"left":0, "right":0, "forward":0}
 
      self.wizard = Actor('Wizard/gw01',                                     
                          {'waving2':'Wizard/gw02',
                         'waving3':'Wizard/gw03',
                         'waving4':'Wizard/gw04',
                         'waving5':'Wizard/gw05',
                         'waving6':'Wizard/gw06',
                         'waving7':'Wizard/gw07',
                         'waving8':'Wizard/gw08',})
        
      self.wizard.reparentTo(render)
      self.wizard.setScale(0.0005)                                           
      self.pieces = self.wizard.setPos(SquareWiz(1))  

      self.wizard.setHpr(180, 0,0)                                          
      self.wizard.loop('waving8')   
      self.accept("arrow_left", self.setKey, ["left",1])                      
      self.accept("arrow_right", self.setKey, ["right",1])
      self.accept("arrow_up", self.setKey, ["forward",1])
      self.accept("arrow_left-up", self.setKey, ["left", 0])
      self.accept("arrow_right-up", self.setKey, ["right",0])
      self.accept("arrow_up-up", self.setKey, ["forward",0])
      
      self.pickerNP = self.wizard.attachNewNode(self.pickerNode)
      self.letshope = self.wizard.attachNewNode(base.camNode)
      self.pickerNode.setFromCollideMask(BitMask32.bit(1))
      self.pickerRay = CollisionRay()
      self.pickerNode.addSolid(self.pickerRay)
      self.picker.addCollider(self.pickerNP, self.pq) 
      self.picker.showCollisions(render)

      taskMgr.add(self.move, "movetask")
      self.prevtime = 0
    
  def setKey(self, key, value):
      self.keyMap[key]=value
      
  def move(self, task):
      elapsed=task.time - self.prevtime
      
      backward = self.wizard.getNetTransform().getMat().getRow3(1)
      backward.normalize()
      j = backward*(elapsed *0.8)
          
      if (self.keyMap["left"]!=0):
          self.wizard.setH(self.wizard.getH() + elapsed*200)
      if (self.keyMap["right"]!=0):
          self.wizard.setH(self.wizard.getH() - elapsed*200)
      if (self.keyMap["forward"]!=0):
          self.wizard.setPos(self.wizard.getPos() - j)
        
        
      wpos = self.wizard.getPos() + backward*(elapsed *0.8)
      
      self.pickerRay.setFromLens(self.letshope, wpos.getX(), wpos.getY())
      self.picker.traverse(self.squareRoot)
      if self.pq.getNumEntries() > 0:
          self.pq.sortEntries()
          i = int(self.pq.getEntry(0).getIntoNode().getTag('square'))
          self.squares[i].setColor(HIGHLIGHT)
      
      
      self.prevtime = task.time
      return Task.cont   
    

w = World()
run()

cant run need your square model
and i fail to see your problem

chessboard tut :

#Set the position of the ray based on the mouse position
self.pickerRay.setFromLens(base.camNode, mpos.getX(), mpos.getY())

Using pickerRay is NOT the way to find the square on which the actor is. setFromLens wants the coord relative to the lens, which is only in range (-1 ~ 1), which is render2d coord range, which is eventually the value range of mpos.
If you don’t need to pick something in the 3d world, based on the mouse location onscreen, you wouldn’t need pickerRay.

You could attach a collision ray to your actor, point it downward. But since it’s full grid, you could go without collision :

x=int( floor(self.wizard.getX()+numSquaresXhalf) )
y=int( floor(self.wizard.getY()+numSquaresYhalf) )
idx = y*numSquaresX+x

idx is the square index, if you use 1 dimension squares array. If you use 2 dimensions array, just use x and y directly.
For chessboard size :

numSquaresX=8
numSquaresY=8
numSquaresXhalf=numSquaresX*.5
numSquaresYhalf=numSquaresY*.5

thx lots this works :smiley:

thx @ ynjh_jo and treeform for ur help :slight_smile:

hello again…

i get a problem while loading a model with animation

the animation works because i tried it on a clean sheet.

but when i try to load it in an if statement or along with another model:

      if (SquareNum == 21):
          self.wiz2 = Actor('trex/trex',
                            {'anim1':'trex-run',})
      
          self.wiz2.reparentTo(render)
          self.wiz2.setScale(0.05)
          self.wiz2.setPos(SquareWiz(21))
          self.wiz2.loop('anim1')

it only loads the model and i get the following error:

i dnt understand… where is the problem? :s

if statement could not cause this problem. You did some thing with the model path. Either moved it or moved your script.

i tried the animation normally on a cleensheet, it works perfectly.

but when i put the loading code just beneath another model’s code, i get this prob…

post all of the code or zip it up.

here zip file containing codes and models:
h1.ripway.com/irene1986/game.zip

*looks like i found where the problem was problem in script… thx lot

self.wiz2 = Actor('trex/trex',
               {'anim1':'trex/trex-run',})

Fixes it, the picking or moving still do not work though.

yep thx a lot :stuck_out_tongue:

helo again

hmm i try to load a png image as background behind the chessboard using the onScreenImage, but the image is loaded in front of the chessboard…

any idea of how to put if behind?
thx