rayCast() using vectors

Could someone guide/make me (to) a demonstration/explanation of to how to use rayCast(), specifically how to use vectors instead of objects as objectFrom/objectTo. Everything i’ve tried has had unexpected results…

This would not only be useful to me (I need to use this for a collision avoidance system), but it would also be useful to the tons of people that want to make FPS’s with hitscan weapons…

What the rayCast() function returns (after typing in all of the required arguments), is a list of data that includes the hitObject, hitPosition, hitNormal ect…

So for example, if you want to only execute a piece of code if rayCast() detected something you’d do.

if myRayCast[0]:
    position = myRayCast[1]
    normal = myRayCast[2]
    #do something else#

If you’re also wondering how to set up the function, then the following link provides a lot of detail on what it does, how to use it, and the type of data it puts out.

As for using a vector instead, you might be able to, but remember that using a simple list of three values would mean that the coordinates would be fixed and wouldn’t be relative to any object. Instead, you would need to do something like this which adds, subtracts, multiplies, or does any math operation on the position of the casting object to get the position you want. (do note that I only provided an example for the first argument.)

myRayCast = rayCast([object.worldPosition[0]+myValue,object.worldPosition[1]+mySecondValue,object.worldPosition[2]+myThirdValue],...

Note that you can do this:

gobj, pos, normal = own.rayCast(to, from)

If you only want the position:

_, pos, _ = own.rayCast(to, from)

It’s nicer, I think.


The link Ace Dragon posted for raycast is for Blender 2.49.


I’ve been updating the BGE Python documentation on my website. The link for raycast for Blender 2.6x is



What would be the easiest way to take into account rotation?
something like:

rotY = self.worldOrientation.to_euler()[2]

    to = [self.worldPosition[0],(self.worldPosition[1]*rotY)+1,self.worldPosition[2]]

What exactly are you trying to do?

If you want to fire a ray in axis direction:

to = from + self.worldOrientation.col[0] # X for Y -> 1, for Z -> 2

hi dragonspammer,

To be honest, I’m not really sure what it is that you are trying to do. It sounds as if you want a detection ray that moves/rotates with your character or game object.

If that is the case then rayCastTo would probably be a better fit. It allows you to set the distance past the other game object. In other words, parent an empty to your character. Use rayCastTo. The ray direction is from the character object center to the empty. Set the distance to 10, 20 or etc. Blender Units past the empty. The empty rotates/moves with your character. Which means the ray rotates/moves with your character.


I need to see the length of the ray and/or the hit position, so I can’t use rayCastTo
here’s what I’m trying to do, ctrl-c - ctrl-p -ed from elsewhere:

Okay, I came up with a relatively straight-forward collision avoidance system:

I’d have two rays, one on either side of the head of my character, going forwards forever. I’d then have them look for property ‘wall’ or whatever. Then I would have to determine the length of both rays.* I would then self.localLinearVelocity[0] = 10/whatever speed I wanted - to walk forward.
If the left ray was longer, the char would rotate [B]left, and if the right ray was longer, it’d rotate [I]right - so in corridors it would stay to the center-ish, and in wider spaces, it would avoid walls

This sort of thing would be perfect for ‘roaming’ creatures/NPC’s.

SO. I thought I’d just put two invisible ‘eyes’ parented to the char, either side of it’s head, that would shoot rays forward, and report back to the main code, being run by the char.[/I][/B]

@ Goran, post #6

Oh great code wizard, uh, what’s .col?

Sorry, the API is being of surprisingly little help.

The matrix alignment is now different in math utils, so to get the column vectors er use that attribute

Okay, so I wrote:

rot = [self.worldOrientation.col[0],self.worldOrientation.col[1],self.worldOrientation.col[2]]

to = [self.worldPosition[0]+rot[0],self.worldPosition[1]+rot[1]+1,self.worldPosition[2]+[2]]

but apperently I can’t add vectors to normal numbers, so I wrote:

rot = [float(self.worldOrientation.col[0]),float(self.worldOrientation.col[1]),float(self.worldOrientation.col[2])]

to = [self.worldPosition[0]+rot[0],self.worldPosition[1]+rot[1]+1,self.worldPosition[2]+[2]]

But I apparently I can’t use float() on a vector. :frowning:

You should be able to just do:

rot = self.worldOrientation.col[0] # Orientation for the +X axis

to = self.worldPosition + rot # Vector representing the right of the game object (i.e. the game object's position + 1 Blender Unit to its right)