2
$\begingroup$

I would like to know how it is possible to create a radar type sensor with only python in bge.

To specify, he would have to fire at the Y + position up to a distance of 100 meters, starting from the cube.

The radius has to be 0, it will stay as a line.

enter image description here

$\endgroup$
2
  • $\begingroup$ In other words you want to detect a line crosses any face? see KX_GameObject.rayCast $\endgroup$
    – Monster
    Jul 3, 2018 at 7:19
  • $\begingroup$ That's it! If you know how to do it, please post here. $\endgroup$ Jul 4, 2018 at 1:00

1 Answer 1

4
$\begingroup$

Please have a look at the BGE API -> KX_GameObject.rayCast

Basically you measure the faces an imaginary line is crossing through.

The mesh of the game object you measure with will be ignored (you want to find another object, rather than itself).

Here is a short demo measuring between [0,-10,0] and [0,10,0]. The coordinates are given in scene space (world coordinates). The direction of the measurement matters as the nearest face to the start point gets returned. The endPosition is a reference where to point to. The measurement will stop after reaching the distance of 15 (regardless the distance to the endPoint).

Code:

import bge

controller = bge.logic.getCurrentController()
owner = controller.owner


startPosition = [0,-10,0] 
endPosition = [0,10,0]
distance = 15

hitObject, hitPosition, hitNormal = owner.rayCast(endPosition, startPosition, distance)

if hitObject:
    print("Object hit:", hitObject, "at", hitPosition, "the normal at that point is", hitNormal)
else: 
    print("hit nothing")

Be aware this is a one-shot. This means you need to call it each time you need to measure. The python solution fits when you need to measure:

  • only at specific time (rather than all the time)
  • with dynamic parameters (rather than fixed distance straight forward)
  • you need to measure multiple times with changing parameters within a single frame
  • you need settings the ray sensor does not offer (e.g. measure along a reflected imaginary line)

When you just want to "reinvent" the ray sensor, notice it is way easier to use the ray sensor.

$\endgroup$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .