Swap axis of linear velocity using python (2.49b)

Hey guys I wonder if you can help me out,

Having played far too much Portal 2 in the past week I got the idea of making a simple ball puzzle game in blender based on portal’s principal of movable teleports ie; a ball is fired from a fixed position and has to be guided to it’s target using teleports and the local geometry.

Setting up the teleports is no problem however when I fire a ball at one teleport it maintains it’s original global velocity when it appears at the other end, ie an object entering a portal with velocity in the -x axis maintains this trajectory at the other end regardless of the orientation of the exit portal.

Say for instance I fire a ball at a portal along the -x axis and the exit portal is pointed along the -y axis how do I go about changing the vx value to it’s vy value instead?

My python knowledge is pretty limited, I understand that I can use get and setLinear/AngularVelocity but how do I perform the swap in the middle?

Any ideas, suggestions tutorials much appreciated.

Thanks

You’d probably need to use the collision sensor’s hitObject property to get the portal object, then get its orientation and adjust your object’s speed to the orientation of the object it’s in collision with.

NOTE: It would be easier and save you frustration if you use the mathutils module to convert the orientation matrix of the portal object to Eulers so you can get the X, Y and Z orientation in simple degrees (or radians if mathutils uses those values instead which I think that might be the case) instead of using complex matrix math.

I second Ace’s suggestion - Eulers are much easier to deal with than Matrices, though be careful of directly setting the axes. I believe there’s a known math caveat where setting certain axes to 0 directly can flip the axes inaccurately.

Vectors are even easier than euler angles.

When the portal isn’t rotated (ie, what it is like in the 3D view before you start the game), which direction does the portal point? I’ll assume it points in the +Y direction for my explanation, however you might need to alter my code a little bit.

What you need to do is change the direction of velocity, the new direction would be the direction that the portal faces. So, using the mathutils module to help with vector math it can be done fairly simply:

from Mathutils import Vector

portal_object = ...
ball_object = ...

portal_direction = Vector(portal.worldOrientation[1]) # take +Y direction of the portal
ball_velocity = Vector(ball_object.getLinearVelocity())

portal_direction.magnitude = ball_velocity.magnitude # portal_direction now becomes our new velocity vector
ball_object.setLinearVelocity(portal_direction) # setting the velocity

Cheers andrew 101 that’s just what I needed, it works fine.