# Search against a list of angles and choose closest?

I have many objects that are 2x2x2,

I need them to indicte where the cube will stick by
“Flashing” - (spawning a generic 2x2x2 “Blank” alpha cube every other frame )

then once in the correct position I need a key press to initiate snapping the cube to the highlighted position, and match it’s orientation to the other cube, to the closest 90 degree angle.

to parent and set orientation to the closest face when they get near enough,

what is a good way to do this?

### Attachments

Snap.blend (845 KB)

https://sketchfab.com/models/5hH4MbH0WbBIQrIlU9wng2zBQoF

here is a sketchfab

Now I know alpha materials do not work

I have

On collision with outer box--------------------python

in python

import bge
GL = bge.logic
cont = GL.getCurrentController()
own = cont.owner
target = bge.logic.getCurrentController().sensors[“Sensor”].hitObject
own.setParent(target)
print(target)
own.setWorldPosition(target.WorldPosition+2.1 on closest axis)

how would I easily do this?
I can do it with another 6 objects, but I would rather us python for this one, and keep it clean

Hello? I guess I will have to go with the empties for now,

I am a tad under the weather, so I might not get to it today

Just multiply the relative positions you want by the cube’s worldTransform matrix.

i.e. You want a snap location off each face of the cube. If the cube is centered at (0,0,0) then the snap locations would be:
(2, 0, 0)
(-2, 0, 0)
(0, 2, 0)
(0, -2, 0)
(0, 0, 2)
(0, 0, -2)

Turn those into 4-component vectors and multiply them by your parent cube’s worldTransform.

How do I choose the closest?

also setting the angle?

I can do own.WorldOrientation = target.WorldOrientation but then it looses which side is facing it,

thank you Kastoiria

If you transform those coordinates by the parent’s worldTransform then you will have 6 coordinates in world-space stored in Vector objects. I imagine that you have another cube that you are trying to drag-and-snap to this parent cube. In that case you just test each of the snap locations to see which one has the smallest distance to the cube being dragged.

Pseudo-code (probably does not compile):

``````
# 'parentCube' would be the snap target
# 'dragCube' is the cube being dragged
# Both are assumed to be GameObjects

# Get the snap targets in world-space
targets = [Vector((2, 0, 0, 1)), Vector((-2, 0, 0, 1)), ..., Vector((0, 0, -2, 1))]
for target in targets:
target = parentCube.worldTransform * target

# Find the snap point that is closest to the dragCube
minDist = (dragCube.position - targets[0]).magnitude
snapLoc = targets[0]
for target in targets:
dist = (dragCube.position - target).magnitude
if dist &lt; minDist:
minDist = dist
snapLoc = target

# 'snapLoc' now has the coordinate you want to snap to.

``````

also setting the angle?
I can do own.WorldOrientation = target.WorldOrientation but then it looses which side is facing it,

Something like this might work. Pseudo-code:

``````

# You have to decide if you want to copf this value or reference it.  If you
# reference it, then any changes to the 'parentCube' or the 'dragCube'
# will affect the other one. If your application is what I think it is, then
# I recommend doing a copy.
dragCube.worldOrientation = parentCube.worldOrientation.copy()
dragCube.position = snapLoc

``````

I think I get you, I will have to tinker with it tonight, I have to get some college prep stuff done today,

So the “snap Locs” are set for each cube, and it chooses which is closest,

I think which ever cube has the most “attached” sides, should be the “host” cube that the other parents to…

this may help…

So my next question is, is there a command that returns all child objects names in a list?

Edit:flagged for delete
double post

If a GameObject is parented to another one, then it will be added to the GameObject.children member. You can iterate it, access by index numbers, and access by the GameObject.name of the child.

is there a way to get the number of items in the index?

so

(I know this is not the code)

I will need that later though, for a few other problems,

If you check the documentation, you will see that the GameObject.children is of type CListValue. The documentation for this says that it was design to work like a typical Python list in most regards.

The length of a list is gotten like this:

``````
# Assumes 'parentCube' is a GameObject

``````

The ‘len(…)’ function is the standard Python way to get the number of elements in a container class.

Ahh,you see I am learning python to make a blendergame,

not knowing python and applying it to the BGE,

so I am unaware of the basic commands I guess,

thank you

that will come in handy in MANY situations

Empties don’t cost anything cpu wise compared to some things

I want the block “Parent” to be the one with the highest value,

I think that this might be easier,

each block has 6 “snap targets” that are empty blocks,

the hard part is the getting the closest angle, then I just set it to that, and have it grab one of the empty locations,

So I need, a value that represents how many items are parented, and a property that is a flag for each occupied side,

In short, I know roughly how to do this, but I lack the skill to achieve it, help?
here is a .blend of the “test” so far

so

own[‘Value’] = len(parentCube.children)

### Attachments

ExplainBlend (1).blend (467 KB)

Bumpdate, added code for parent value,

in .blend

and debug text

and a snappy graphic!

I am going to have the “empty” slot actually be a cube, not a empty now, so I can have it flash a wire frame when the cube is in a “snap slot” and then a keypress will engage the parent…

It would seem that this is a pretty simple thing to deal with.

1. Make a Python dictionary of open slots on each cube.

2. Populate the dictionary with the holographic placeholders (which signify an open slot), or make a separate Python dictionary of holographic slots on each cube.

3. When you lock cubes together, occupy the correct slot for one cube ( i.e. obj[‘slots’][’+X’] ) and the opposite slot for the other cube ( i.e. other[‘slots’][’-X’] ). You can use the references to the actual cubes so that you can refer to them later.

4. Set the ‘to-be-parented’ cube to have the position and orientation of the holographic slot cube. You get the reference to the hologram by the slot dictionary (which should contain the holographic object references, which signify an open slot), or by using the separate holographic dictionary.

5. Parent the ‘to-be-parented’ cube to the destination cube.

6. Delete the holographic cubes when necessary to help speed up the game.

Side-note: Looking at it now, your game idea sounds similar to RawBots.

That’s cool (raw bots)

I have not made any dictionaries yet, is that like a group of lists?

Also, the orientation matters, so I need it to get the closest rotation and use that,

So if Cube A is @ 33 degrees, it rotates back to 0, but if it is at 46+ it goes to 90

I was thinking

Cube angle A - B = Difference,
how do I make this only a positive number ?

then I can us If’s to pick the angle with the lowest value,

A dictionary is what is known as an associative array in other languages. It’s like two lists, one for indices (i.e. a string) and the other for values (i.e. your cubes). You access the values by using a separate index. Check out the Python docs online for more info.

I would imagine that since your cubes are going to be composed of unique faces, that the player would designate which face he wants to snap to the cube. This gets into vector math to determine how far of a rotation objects are apart, if I recall. You should probably check the difference between face normals to get how far the cube should rotate.

When you say ‘only a positive number’, do you mean make it a positive number only? If so, use the abs() function (check the Python docs).

Awesome link SolarLune. I’d be interested in how to create the firing effect of the bug bot in Blender (the turret when it shoots).

Can post back in my thread as I don’t mean to thread-jack here.

I am not ever frustrated by thread jacking, conversations in the real world shift attention, that does not mean they do not shift back

and yes, this is very similar to what I hope to achieve,