Normal Calculation

How do you find a normal vector from three points? In the .blend posted, I have 3 empties casting a ray to reveal 3 hit-points. From those 3 hit-points I am attempting to figure out the normal in order to alignAxisToVect() of the Turret_Mesh object. Keeping the Turret_Mesh object touching the floor on all 3 legs. Unfortunately, I am unable to figure this out.:frowning: Thank you for your help. :yes:

Attachments

New_Turret.blend (175 KB)

How about one ray on the bottom of the object that aligns the object’s axis to the hit normal.

Also, if you do not want to use a ray sensor, you can use my ray class instead:


# A ray class, replaces ray sensor
class ray:
	# sends the ray. Use attributes to access the results.
	def __init__(self, obj, prop=None, rayRange=0.01, axis='y', xray=0):
		self.object = obj
		self.range = rayRange = max(0.01, min(10000, rayRange)) # the range of the ray.

		# check if the axis should be negitive.
		x = 1
		if '-' in axis:
			self.range *= -1
			x *= -1
			axis = axis.replace('-', '')

		self.axis = axis # set the axis

		# calculate the axis.
		axis = {'x':0,'y':1,'z':2}[str(axis).lower()[0]]

		# create an empty vector
		v = Vector()
		# set the axis to x which is calculated obove.
		v[axis] = x

		# Change the direction of the object's orientation
		point = Matrix(*self.object.worldOrientation) * v
		# then set the length based on the rayRange
		point.magnitude = rayRange

		# last step is to add the object's position to the point of the ray.
		vec = Vector(self.object.worldPosition)+point

		# send the actual ray, and collect the results.
		if not prop: prop = ''

		self.hitObject, self.hitPosition, self.hitNormal, self.poly = self.object.rayCast(vec, self.object, rayRange, prop, 1, xray, 1)

		self.rayDirection = Vector([self.object.worldOrientation[i][axis] for i in range(3)]).normalize()

		self.positive = True
		if not self.hitObject or self.hitObject == self.object: self.positive = False


It has all the attributes of the normal ray sensor including positive.

ray variables:

  • obj - A KX_GameObject
  • prop - The property to search for, leave as None to check for anything
  • rayRange - The range of the ray
  • axis - the axis to look through. (eg. “x”, “y”, “-x”)
  • xray - boolean; whether to use xray or not.

Hope it helps.
-Sunjay03

Pick any one of the three points, then draw two vectors from that point to the remaining points, take the cross product of those two vectors, and you’ll either have a normal vector, or an anti-normal vector.

I don’t know if this is what I am looking for. I want that my enemy(spider) rotates to the face hit by the ray. But it is really beyond my Python skills. Hope you can help me a bit.

Here is an example:

Attachments

rot_hitObj3.blend (145 KB)