Questions about raycast()

Hey guys,

first of all, I have to say, that I have only understand the basics of python, or better : I understand already written code quite good, but I can’t “learn” from it.
So, for a school project I wanted to recreate super mario bros.
Now, I found a script of Solar Lune (thanks to you, if you read this).
The code parts for walking left and right were already implemented, but I have to make mario jump of course.
I asked Solar Lune, and he told me, using something like

-pseudo code-

If the spacebar is pressend and character stands on ground
then jump
do nothing

He also suggested to use the raycast() function.
Now comes the problem: I dont really understand how I have to use it.
What I wrote :

from bge import logicimport GameLogic

cont = logic.getCurrentController()
obj = cont.owner

pos = obj.position         # Get the Player's current position
topos = pos.copy()         # Make a copy of the Player's current position

scn = GameLogic.getCurrentScene()
obj1 = scn.objects["cube"]
pos1 = obj1.position

if obj.rayCast(pos1, pos, 0, "wall", 1, 1) != None :
    print("Not hit")

In my scene, there is a cube, which has the property “wall” , and I have the cube which has the script assigned.
The “wall cube” just stands there, does nothing else.
The other cube is ‘Dynamic’ and placed above the "Wall"Cube.
What should happen, is while the cube is falling, it should print “Not Hit”, and as soon as it fell on the wallcube, it should print “Hit” (I think you get what I want to do)

Could maybe someone show me an example of how it should be written (It doesn’t have to be the exact solution, maybe I can learn just by an example)

Wow… a lot of text about a small problem :confused: But I cant explain good ^^

Anyways, if you could help me, I of course would appreciate it.

Cheers, TheHexaCube

PS: If something is unclear, please just ask

Where is the “cube” object (obj1 in your script) in relation to the wall and your player? Make sure that both objects can be hit by a ray. I think that static objects can only be detected by a ray that comes from a sensor type object, but I could be totally wrong on that one.

rayCast never returns None it always returns a list.

What you want to do can be achieved with a ray sensor too.

Monster is correct. So instead of:

if obj.rayCast(pos1, pos, 0, "wall", 1, 1) != None:
    print("Not hit")

you would use something like:

if obj.rayCast(pos1, pos, 0, "wall", 1, 1)[0] != None:
    print("Not hit")

But like monster said, it would most likely be easier to use a ray sensor, which doesn’t require any python :yes:

It might also be a little more versatile to use a sensor object, rather than rays, to detect the ground- just add a thin box that’s slightly less wide than the player, put it at the feet of the player (so it’s intersecting with the ground if the player is standing on the ground) and parent it to the player. Then set the physics type to sensor, and add a collision sensor to it in the logic panel.

The catch with a ray is if your character is standing on the edge of the floor the ray can cast down past the corner and not detect the floor, even if the character is still on the ground. A sensor object will detect the floor at those times. The disadvantage is you can’t get a return of the specific position the collision happens at, but in your case I don’t think you need that.