Find the end of a ray/ new function?

#1

Hi!

I know you can make a ray, but is there any way to console.log the endpoint of one in the console if it doesn’t hit anything?

I figured it out, but an actual function would be nice.

raylength would be the length of the ray.

The end of the ray can be found at new BABYLON.Vector3(ray.direction.x*raylength, ray.direction.y*raylength, ray.direction.z*raylength);

Hey @Deltakosh would this be possible?

#2

A ray is by essence infinite until it reaches something (like a laser)
you can anyway create a ray with a length already (ray.length) :slight_smile:

#3

I know the ray function lets you set a length, and so does the camera.getForwardRay(*length*)
and so like the ray.origin line of code I though maybe a ray.end might be helpful.

Just a thought.

#4

If you are doing camera.gerForwardRay as your method then your ray origin is your cameras position.

I am confused as to what you are trying to accomplish? Maybe I am just misunderstanding the intent.

#5

I’m thinking of like doing the opposite of ray.origin.

with camera.getForwardRay you can specify the length, and so I thought a function the would find the end of the ray might be helpful in some cases.

Right now you can find the end of it by doing
var length = 100; var ray = camera.getForwardRay(length); var endofray = new BABYLON.Vecor3(ray.direction.x*length, ray.direction.y*length, ray.direction.z*length);

#6

if you just do

rayOrigin.add( ray.direction.clone().scale(length) )
1 Like
#7

That seems to crash the playground. :thinking:
unless if I’m using it wrong

#8

show me your pg.

#9

https://www.babylonjs-playground.com/#E8C51D#14

It’s for nyns, and at lines 158 and 159 is where I’m teleport the camera to the end of the ray when it doesn’t hit anything.

#10

https://www.babylonjs-playground.com/#ZHDBJ#49

1 Like
#11

I raise you this:
https://www.babylonjs-playground.com/#E8C51D#15

Thanks for the help!

It made the code more efficient.:smiley:

#12

why the .clone() ?

#13

Some points about your scene need to be cleaned up.

dont use

canvas.addEventListener('keydown', onKeyDown, false);
canvas.addEventListener('keyup', onKeyUp, false);
scene.onPointerDown
scene.registerBeforeRender
etc

take a look at:
https://doc.babylonjs.com/features/events

#14

So no DOM events?

I can switch everything over.

#15

Old habit.

I’ve had situations where I declare a vector then set it to a variable added and it effected the old one in other systems and situations.

Prolly does not need to be done, but I forget if BJS returns a new v3 on the add or not so I just do it to be safe. I know addInPlace does not… blah blah simple answer old habit.

@Givo yeah, I mean you can for simplicity when you control the page loading completely but on the playground Id recommend those.

2 Likes
#16

Maybe switching over to babylon events would fix my mouse issue.

I have aim as a toggle because if you hold right click left click will not register as fire and will do nothing.

#17

Ohh shit, its that game you were talking about. Right on!

1 Like
#18

Nyns?

yes, that game. I’ve been trying to switch over to rays since the ball mechanic of the old demo was wonky. You had to aim up to hit someone far away.

Now at the end of the ray a small “bullet” will spawn if you don’t hit anything. It will have a high mass and will fall. Kinda a weird way to do bullet-drop, but will make the game a bit easier.