BGE Proposal: giving KX_GameObject the ability to read an object's " Dimension Scale"

Lately while I was messing around with the blender game engine, I was exploring ideas on how to align the text to the center or right(not the UV Text) in python.

I’ve tried multiple attempts by reading the scale of the object, and then use the scale to tell the position to go backwards by making the number from the scale negative.

Example:

import bge


cont = bge.logic.getCurrentController()


text = cont.owner


length = text.scaling


print (length)


text.position.x = (-(length.x/2))


print (text.position.x)

so far the resulting code only moves the text object back a few blender units rather than centering the text allignment.

The idea of this was to take the scale of the object, divide it into half, and then tell the game engine to push the object halfway of the position based on the object’s scaling.

It wasn’t until I realized that the Scaling was equal to “1” not the actual size of the object, I would need the “Dimension” size to bring the position halfway back.

So if it is possible, I would really like the blender game engine to have “Dimension” scaling based on the object so then the Text can become center aligned or right aligned.

Another Example:

import bge


cont = bge.logic.getCurrentController()


text = cont.owner


length = text.<i><b>dimensions</b></i>


print (length)


text.position.x = (-(length.x/2))


print (text.position.x)

I think this is a useful idea for the blender game engine, it would finally allow text objects to be centered or right aligned, making it easier for things like dialog text boxes or etc.

What do you think?:yes:

You mean the bounding box?

Just now you have to analyze the mesh. But I do not know about text objects, as they are very special.

For now, the easiest solution would just be to parent 2 empties to the object at opposite corners of it’s bounding box. You can then easily get the objects dimensions by just taking the difference between the empties’ positions in the game. It will work regardless of scaling as long as you use the world position instead of local.

Yes! I was just thinking about this dimension issue, it’s really annoying that we don’t have access to the bounding box through the Python API. I’m looking into the source code now and want to add the functionality… (if I manage to figure out how things are laid out…). For the time being I use this workaround (if scaling issues don’t affect your development):

  1. create the object and give it the size you want
  2. take note of it’s dimensions by opening the properties panel (N in the view port)
  3. set all of it’s dimensions to 1
  4. apply the scale (CTRL + A)
  5. now you can change the scale of the object to the dimensions you took note of (at step 2.)

After this you have access to it’s real dimensions from Python API with object.worldScale. Obviously this will mess with the scale of the object and if this isn’t desirable… then you just have to wait until someone (me maybe?!) adds the bounding box info to the Python API or write your own routine to find the bounding box :)…

You can determine the bounding box of the object using Python.

Here’s some sample code for finding the width, depth and height of an object


def dimensions_to_properties(cont):
    own = cont.owner
    mesh = own.meshes[0]
    transform = own.worldTransform
    dimensions = get_dimensions(mesh, transform)
    
    own['x'], own['y'], own['z'] = dimensions




def get_dimensions(mesh, transform):
    
    xyz = [[], [], []]
    
    for i in range(mesh.getVertexArrayLength(0)):
        vertex = mesh.getVertex(0, i)
        vertex_pos = transform * vertex.XYZ
        for component, all_components in zip(vertex_pos, xyz):
            all_components.append(component)
     
    dimensions = []
    for components in xyz:
        components.sort()
        min_, *_, max_ = components
        dimensions.append(max_ - min_)
    
    return dimensions


             


Yeah, but that only works for mesh objects, not text.

EDIT:
Regarding this, I’m looking into the source code and I think I’ll be able to submit a patch in a couple of days that will give access to:

worldDimensions (dimensions aligned to world axis) &
localDimensions (dimensions aligned to normal axis)

in the Python API.

And he now has a patch ready for testing

I can really see the potential uses of this (like determining if an object can fit somewhere). Much more straightforward than going through vertexProxy to calculate everything manually (and probably faster as well).

Yes, unfortunately I’ve just noticed that this doesn’t work correctly for font objects. I won’t go in the details, this is an internal issue as far as I can tell, the way the bounding box is calculated (or rather not calculated) for font objects is the issue… but nonetheless, it will work correctly for mesh objects… for the time being :slight_smile:

Hello again! Sorry for the absence but University work in Animation/Illustration is tough work.

Meanwhile, I’ve been trying out few suggestions that everyone in this thread has posted. One of them was that I’ve tried razvanc87’s method of setting the dimension scale to 1 but the apply scale does not work for text objects.

Trying to Center allign the text object just made it harder than I bargained for, and I understand why this is happening, it is to no surprise that a lot of blender users including some in this thread have explained that there are text object issues that are internal just because they are different and special than the rest of the other meshes in Blender.

However, I did see that there is now a patch revealed that allows “localDimension” and “worldDimension” for KX_GameObject, and this is good, perhaps what started as a suggestion might become bigger in the long run one day.

Still though, I’m trying to figure out how to center align the text object, and I am not going to give up now. I see a lot of potential for the Blender Game Engine. Even when its still so far away from being an ideal game engine for AAA quality games, it’s slowly getting there. :yes:

Here’s hoping that the Blender Game Engine will become a great engine! :smiley:

P.S. How do I apply the local/world dimension patch to blender? And thanks for everyone for participating in this thread so far! I’m still leaving it open for a little while to hear more suggestions and ideas…

Welllll…

You have two options:

  1. wait for the patch to be approved, integrated and then download the new version
  2. download the source code of blender, apply the patch yourself and then build it. It shouldn’t be that very difficult if you know your way around compilers a bit :slight_smile: and they do have pretty good guides about building (for linux at least…)

I do have to point out again, you might have missed this:

(yet…)

Ohhh yea, and a third option… for getting the dimensions of the text which involves… detective work :smiley:
is:

  1. find the font you’re using
  2. open the font file and retrieve the dimensions of each character
  3. create a map from each character to dimensions (a file, or directly in your script, probably a file is more logical :D)…
  4. calculate the dimensions based on the text you have…
  5. yeah…

My goodness, dealing with fonts (the detective way) is hard then, it just goes to show you that text objects are just one of many within the blender game engine that needs to be improved on. However, I am looking forward on the progress of the patch that is being worked on. :yes: I have high hopes for that work in progress!

Giving KX_GameObject the ability to read local/world dimensions can be a huge jump from collision detection to other things unimaginable within the BGE.

I don’t think it’s actually that big of a deal… because with mesh objects you can do your own bounding box calculation anyway (in Python) :)… I just thought it’s good to have them directly calculated in C++, at least it should be faster… and it bugged me because such a “basic” property of the objects didn’t exist in the first place.

But anyway, about the fonts, I’m gonna take a look at that when I have some time, I don’t promise anything though, it’s kind of messy the way they are handled in BGE and I didn’t understand anything when I took a look the first time :))…

Not a problem. :yes: Progress is simply progress within computer software…

No news about this issue? I’m facing this problem with font objects. I need to center the text inside a button, and it near impossible without knowing the dimensions of the text.