# use one node to divide world into two?

I have a NodePath with an angle. By using its angle I want to kind of “divide the world into two parts” and find out if another node of mine is on which side. How?

Try using a cross product. First you have to define two points in your NodePaths frame, usually the origin and a point along your angle. ie…, if your angle is 60, a set of possible points is A = (0,0) and B = (sin(60), cos(60)

To test whether a dot is to the left or right of your angle, check the sign of (C-A) cross (B-A).

being math dumb, what do you mean by “NodePath frame”, is “A” the x,y position of the NodePath which in your example happens to be (0,0)?

If zhao and me are right, you want to divide your rendered world into two halfs. You can imagine an line infinite into both directions.
So, imagine a line A–>B
A is in coordinates 0, 0
B is like zhao said on (sin(60), cos(60)

now look at this code:

``````def crossProduct(a, b):
a1, a2, a3 = a
b1, b2, b3 = b
cp = (a2 * b3 - a3 * b2,
a3 * b1 - a1 * b3,
a1 * b2 - a2 * b1)
return cp

def dotProduct(a, b):
a1, a2, a3 = a
b1, b2, b3 = b
dp = a1 * b1 + a2 * b2 + a3 * b3
return dp

def sameSide(p1, p2, a, b):
a1, a2 = a
b1, b2 = b
u = (b1 - a1, b2 - a2, 0)
p11, p12, h = p1
v1 = (p11 - a1, p12 - a2, 0)
p21, p22 = p2
v2 = (p21 - a1, p22 - a2, 0)
cp1 = crossProduct(u, v1)
cp2 = crossProduct(u, v2)
if dotProduct(cp1, cp2) >= 0:
return True
else:
return False``````

Actually, the simplest way to figure your problem is just bring the point into the NodePath’s frame of reference and check the relative position directly.

If the terminology is confusing to you, let me give you an example of my dog chasing a squirrel.

Suppose my dog is at position (12, 30, 0) with his body slanted at a 30 deg angle

``````npDog = NodePath("My dog")
npDog.setHpr( 30, 0, 0)
npDog.setPos( 12, 30, 0)``````

Suppose the dog is chasing a squirrel. Relative to me (render), they both have a certain position, angle of body…etc.

But what does the squirrel’s position look like to my dog?

Basically, we want to convert how I see the squirrel to how my dog sees the squirrel. This conversion is
very simple code wise.

``````#Position the squirrel slightly away from the dog
ptSquirrel = Point3( 0, 1, 0) + Point3( 12, 30, 0 )
relPt_FromDogsPointOfView = npDog.getRelativePoint( render, ptSquirrel )
print relPt_FromDogsPointOfView

#What about a second squirrel at a slightly different position?
ptSquirrel2 = Point3( 0, -1, 0) + Point3( 12, 30, 0 )
relPt_FromDogsPointOfView2 = npDog.getRelativePoint( render, ptSquirrel2 )
print relPt_FromDogsPointOfView2``````

Once you do the relative conversion, to test for left, or right, just check the relPt_…[0] as you would normally.

( Don’t forget that implicitly, in Panda’s coordinate system ‘forward’ means +Y. )

Keep in mind that most of the time you don’t need to do your own linear algebra in Panda. Panda provides many convenience methods on the point and vector classes including cross() and dot(), that are usually faster than doing it yourself.

Why not use a collision plane? panda3d.org/manual/index.ph … ion_Solids
It divides the whole space into two and you can check for an object being in one, another or intersecting the infinite plane.

Of course this is only any good if you need to test for all three cases. In case you only need to test the center of every object, simple math will do.

Won’t just checking the sign of object2.getY(object1) do?

Only if it’s a plane along the X and Z axes.

Not really.

``````divisor = render.attachNewNode("divisor")
divisor.setHpr(VBase3(<whatever>))

if myNode.getY(divisor) > 0:
print "My node is in front of divisor node"
else:
print "My node is behind divisor node"``````

Not sure if that’s of any use for redpanda’s use case, but it sure looks like the simplest way to find out if some node is behind or in front of another. Would also work for “on the left/right” or “above/below” with X or Z.