KX_GameObject(SCA_IObject) width

Hello is it possible to access the width and height of a KX_GameObject(SCA_IObject) in the game engine?

You mean the physical size and dimensions of the object? No, not directly. I wrote a function to give it to you, but I’m not sure exactly how accurate it is. It takes into account the object’s scaling as well. Take note that this function loops through the vertices to get the size, so it probably will be slow. It’s best to get the size only when necessary (i.e. at the start of the game and if the object changes), and store it in an object variable.



def GetDimensions(object = None, roundit = 3, offset = 1, meshnum = 0):


	"""
	Gets the dimensions of the object (what you see under dimensions in the properties window in the 3D menu).
	mesh = which mesh to use to get the object's dimensions.
	roundit = how far down to round the returned dimension values; set it to a negative number to not round the numbers off at all.
	offset = Whether or not to return the offset point of the dimensions (the center point);
	This negated (-offset, literally) is the origin point, generally.
	meshnum = The index of the mesh to use. Usually 0 is okay.
	"""


	if object == None:	
		object = logic.getCurrentController().owner
		
	s = object.worldScale
	
	mesh = object.meshes[meshnum]


	#print (dir(mesh))
	
	verts = [[], [], []]
	
	originpos = [0, 0, 0]
	
	for mat in range(len(mesh.materials)):
		
		for v in range(mesh.getVertexArrayLength(mat)):
		
			vert = mesh.getVertex(mat, v)
			
			pos = vert.getXYZ()
			
			verts[0].append(pos[0])
			verts[1].append(pos[1])
			verts[2].append(pos[2])
			
	verts[0].sort()
	verts[1].sort()
	verts[2].sort()


	if offset != 0:
	
		offsetpos = [
			(verts[0][len(verts[0])-1] + verts[0][0]) / 2,
			(verts[1][len(verts[1])-1] + verts[1][0]) / 2,
			(verts[2][len(verts[2])-1] + verts[2][0]) / 2,
			]
	
	if roundit >= 0:
		size = [
		round( (verts[0][len(verts[0]) - 1] - verts[0][0]) * s[0] , roundit),
		round( (verts[1][len(verts[0]) - 1] - verts[1][0]) * s[1] , roundit),
		round( (verts[2][len(verts[0]) - 1] - verts[2][0]) * s[2] , roundit) ]
	else:
		size = [(verts[0][len(verts[0]) - 1] - verts[0][0]) * s[0],
		(verts[1][len(verts[0]) - 1] - verts[1][0]) * s[1],
		(verts[2][len(verts[0]) - 1] - verts[2][0]) * s[2]]


	#originpos = [0, 0, 0]
			
	if offset:
		return (mathutils.Vector(size), mathutils.Vector(offsetpos))
	
	else:
		return (mathutils.Vector(size), None)