3
$\begingroup$

I want to make a level editor where objects can be added on a grid kind of like minecraft when you place down I cube.
Here is the code.

from bge import logic as l 
from bge import events
from mathutils import Vector

c = l.getCurrentController()
o = c.owner

scn = l.getCurrentScene()
obj = scn.objects

mouse = l.mouse
lm = l.KX_INPUT_JUST_RELEASED  == mouse.events[events.LEFTMOUSE]

sens = c.sensors

ray = sens['Ray']
hitobj = ray.hitObject
hitpos = ray.hitPosition
hitnorm = ray.hitNormal

pos_vec = Vector(ray.hitPosition)
norm_vec = Vector(ray.hitNormal)*2
if lm:
    cube = scn.addObject("Cube1", hitobj, 0)
    cube.worldPosition = hitobj.worldPosition
cur.worldPosition = norm_vec

level editor

In the picture above I can not build any more than that. Is there something that I'm doing wrong?

$\endgroup$
4
  • $\begingroup$ Could you share the blend file, so that we can actually give it a try and see what's going wrong? $\endgroup$
    – dr. Sybren
    Dec 23, 2014 at 22:50
  • $\begingroup$ You might find this useful $\endgroup$
    – sambler
    Dec 24, 2014 at 11:11
  • $\begingroup$ Okay here's the link for it dropbox.com/s/cps8rb45mwt61pc/untitled.blend?dl=0 $\endgroup$
    – Ricky Moon
    Dec 24, 2014 at 13:47
  • $\begingroup$ well that's almost what im looking for but he has his go to preset points i'm trying to get the object normal's but thanks c:. $\endgroup$
    – Ricky Moon
    Jan 2, 2015 at 18:51

1 Answer 1

1
$\begingroup$

add a 'Filter' in each cube, that is the localized hit point for 'Buildible' faces,

then use .hitPosition and localize it, (this is using a rayCast)

    offset= Vector([5,0,0])
    offset = own.worldPosition+(own.worldOrientation*offset)
    Ray = own.rayCast(offset,own.worldPosition,0,'',1,1,0)
    ## attempting to localize hit point to hit object Ray[0]=hitObject
    ## Ray[1]=HitPosition
    Diff = Ray[0].worldPosition-Ray[1]
    Diff = Ray[0].worldOrientation.inverted()*Diff
    ## this will be used to then check to see if this type of part
    ## can go at this location and that the jack in unoccupied and 
    ## that the unit the part is hooking to is friendly
    Diff*=-1
    own['localHit']=str(Diff)

this is then used to check against hit jacks and put the block there, so you can build on a grid, but also you can define other setups like grid size vs block

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .