Raycasting help

Can anyone point me to a few raycasting examples, but not the BGE api documentation…I have been going over that for about 2 hours:p. I just need simple working barebones examples. I am trying to find the vector to a ray hit and place my character there(position.z) for my first movement script, but mine does not seem to be working


   hitobj, hitpoint, hitnormal = player.rayCast(ground_target, player_vec, 20, '', 1, 0, poly, 0)
    player.position.z = hitpoint.z - 3

I did predefine the to and from vectors, but I am not sure I am understanding the documentation 100%
either that or assigning the z value to the character is just wrong?? Or using the character physics object is? I want to prevent the player from sliding around on slopes atm.

EDIT: managed to work out a solution after some help in this thread(and a little bit of proofing, since people like to give false code to get people to do some work on their own ;))


    player_vec = ([player.worldPosition.x, player.worldPosition.y, player.worldPosition.z - .5])
    ground_target = ([player.worldPosition.x,player.worldPosition.y,player.worldPosition.z - 20])

    ray = player.rayCast(ground_target, player_vec, 2, '', 0, 0, 0)
    bge.render.drawLine(player_vec,ray[1],(1,1,0))#debug visual aid

    if ray[1]:
        tempVec = ray[1]
        player.worldPosition.z = tempVec.z + 1.2

        if forward == pressed:
            player.applyMovement((0.2,0,0), True)
        if back == pressed:
            player.applyMovement((-0.15,0,0), True)
        if left == pressed:
            player.applyRotation((0,0,.05), True)
        if right == pressed:
            player.applyRotation((0,0,-.05), True)

Look at the API documentation on this site http://www.tutorialsforblender3d.com/.
http://bgepython.tutorialsforblender3d.com/GameObject/rayCast
http://bgepython.tutorialsforblender3d.com/GameObject/rayCastTo

looks like the same info, but a bit more consistent…one of the problems I have with the bge official docs is slight inconsistencies…
I see things like vector((0,0,0,)) and vector([0,0,0]) with the latter being python correct(I think)

but does using character, rigidbody, static etc have any adverse results??..I’ve felt that using character gives me better collision.

from mathutils import Vector 
ray = player.rayCast(ground_target, player_vec, 20, '', 1, 0, poly, 0)
If ray[0]:
    player.position.z = ray[1] + Vector([0,0,3])

And I use static, rigid body or dynamic,

character controller is buggy and not easily customized.

Don’t use player.position - its deprecated and acts strange sometimes. Use player.worldPosition

I’s also had issues with strange objects doing the raycasting: ie don’t raycast from font objects…
There should be lots of demo’s for raycasting. At the very least, there is one in Inferno (finished games forum) in menu.py to detect what the user clicks on.

Thanks, I actually was going to go with static, I prefer to calculate everything myself usually through ray/linecasting…I guess technically these are line casts since they have a fixed distance…meh potatoes pot"ah"toes. I just picked up the bge and will more than likely be coming around a lot for little bits of info but, is there another place(forum or other) where I could be hunting down answers?

Just in case anyone else is looking for something similar, or is having similar problems here is the simple code I ended up with.


    player_vec = ([player.worldPosition.x, player.worldPosition.y, player.worldPosition.z - .5])
    ground_target = ([player.worldPosition.x,player.worldPosition.y,player.worldPosition.z - 20])

    ray = player.rayCast(ground_target, player_vec, 2, '', 0, 0, 0)
    bge.render.drawLine(player_vec,ray[1],(1,1,0))#debug visual aid
    if ray[1]:
        tempVec = ray[1]
        player.worldPosition.z = tempVec.z + 1.2
        if forward == pressed:
            player.applyMovement((0.2,0,0), True)
        if back == pressed:
            player.applyMovement((-0.15,0,0), True)
        if left == pressed:
            player.applyRotation((0,0,.05), True)
        if right == pressed:
            player.applyRotation((0,0,-.05), True)

I will update the first thread.

if you use character physics don’t use collisions bounds, if you have that ticked you get sliding.