# Calculating Slope

I need to block my character from going up slopes that are too high, and also, when climbing stairs need an easy way to stop the character from sliding backwards down the slope that I use for stairs. Is there an easy way to do this using python?

It was late, and now I’m using my real name anyway.

If you have any questions, I’ll be glad to answer them here OR on my programming-blog where the code is outlined with greater clarity. Be warned that I was feeling a little loopy while writing the articles, so they may be a confusing in their own right. ^^;

Ok , use a triangle,

cast two rays from p1

you know angle 1 and distance A and B

from this you can do ALOT

like detect ground type, detect sheer drop, anything really, as well as do the triangle math to get the rise/run of line C

Look at the file, open the console, make the blender window small enough so you can see…

blender does not support complex strings as far as I can tell, buy python does,

when you press space, notice the 0.0000 go to 0.0

this is actually changing to the string “String” in the cube, but you can’t see that at the moment,
all those properties, on the object “Collision” on layer 2, are linked to python,
the properties
Target = who was struck
Loc = Location of strike
string= attempt to make vector view-able in game//Fail

when the object “collision” strikes on layer 1 after hitting space, it prints in the console each time,
so you could, in theory, make a triangle math system, that would get the slope of the ground you are going to walk on next,

### Attachments

MathStartDemo.blend (493 KB)

Step 2
(I am going to need a slope calculator myself for my IK rig)

this one has X and Z values (world at the moment)
so two points
red = X
Blue=Z

( Z-One minus Z-Two)
divided by
X1-X2

Rise/ Run

### Attachments

MathStartDemo.blend (527 KB)

Well I’m attempting to distill only what I need for slope and stair navigation, so using the following code (modified, as I cannot get a touch sensor to work):

``````move = Vector((0, 0, 0))

if g1 or g2 or g3 or g4: #If any of the 4 ground sensors are within .54 BU from the ground, give user control over motion.  #If you change this, user could jump high, start walking far above the ground
if slopeSensor.positive and not jump_state:
render.drawLine(own.worldPosition, slopeSensor.hitPosition, [255,0,0])
print(slopeSensor.hitObject)
local = True
print("stairs")
speed = 15 / GameLogic.getLogicTicRate()
move = own['move']
move.normalize()
move *= speed
own.applyMovement(move, local)
if not jump_state:
local = False

ray = cont.sensors['stairFloor']
if ray.positive:
print("floor_sensed")
r = ray.hitNormal
own['r'] = r
else:
r = own['r']

move = own.localOrientation.to_3x3() * move
n = Vector(r)
d = round(move.dot(r), 3)
move = move - (n * d)

own['length'] = round(own.worldPosition.z - cont.sensors['stairFloor'].hitPosition[2], 2)

if move.z &lt; 0: own['push'] = True
elif move.z == 0:
if own['length'] &lt;= 1.14: own['push'] = False
else: own['push'] = False

if own['push']: move.z = speed * -0.7071067690849304
``````

Well, here is my first question… and it baffles me…

you set move to equal:
move = Vector((0, 0, 0))
and then assign it:
move = own[‘move’]
… and move does not exist on your sprite object as far as I can tell??
When I try it I get the error key “move” does not exist…

Can you talk me through this… Is this the only part of your script I need for slope/stair navigation?

EDIT: Let me know if you need to see my blend file…

Thanks.