Ray casting gives strange results

Hi i have some problems on ray casting, now i have 1 node player and it has +1 geom that gives me direction(basically it fly at face of player node)
Now when i do

     CollisionResults results = new CollisionResults();
      Vector3f origin=player.getWorldTranslation();
   Vector3f dir = new Vector3f( geomDir.getWorldTranslation().getX()-(player.getWorldTranslation().getX()- 
     geomDir.getWorldTranslation().getX())*2,
              geomDir.getWorldTranslation().getY(),geomDir.getWorldTranslation().getZ()-
           (player.getWorldTranslation().getZ()- geomDir.getWorldTranslation().getZ())*2);

 Ray ray = new Ray(origin, dir);
      refer.collideWith(ray, results);
      System.out.println("collision  NOT"+"  origin"+origin + "  dir:"+dir+"  refer"+refer.getWorldTranslation());
      if(results.getClosestCollision()==null){
        System.out.println("collision!!!!!!!!!!!!!!!!");
       channel.setAnim("Walk");   
      }else{//push back a bit
          System.out.println("collision  HERE!!"+"  origin"+origin + "  dir:"+dir+"  refer"+refer.getWorldTranslation());
          System.out.println(results.getClosestCollision().getContactPoint());
          player.setLocalTranslation(player.getLocalTranslation().getX()-
       Math.abs(player.getWorldTranslation().getX()- geomDir.getWorldTranslation().getX())*4,
                  player.getLocalTranslation().getY(),
            player.getLocalTranslation().getZ()-Math.abs(player.getWorldTranslation().getZ()- 
       geomDir.getWorldTranslation().getZ())*4);
          results.clear();
          
      }

and i get this result(its a system.out)

collision NOT origin(495.41574, 0.100001335, 513.5365) dir:(495.4239, 0.100001335, 513.8364) refer(500.0, 1.0, 520.0)
collision HERE!! origin(495.41574, 0.100001335, 513.5365) dir:(495.4239, 0.100001335, 513.8364) refer(500.0, 1.0, 520.0)
(500.68347, 0.10106462, 519.0)

refer its a geom 1.3.1

    Box box = new Box(1,1,1);
    refer = new Geometry("Item", box);
    refer.setLocalScale(1, 3, 1);

how is this possible,it should not collide,even on print out it say it should not.
What am i missing?

I’ve caused myself a similar problem with rays in the past that was solved by normalizing all of my ray directions. Maybe try using Ray ray = new Ray(origin, dir.normalize()); and see if that changes the results at all

wow thnx :slight_smile: what it do ?“NORMALAZING” :smiley:

eeh :frowning: aint worked :frowning: it seems it get stuck on corners.i just get stuck in air all the way long the corners diagonal

For a vector, “normalizing” means converting to a vector with the same direction and a norm/length/magnitude of 1.0 . In Cartesian coordinates, spatial directions are typically represented by a “normalized” vector.

This is not a direction vector. I’m not even sure what it’s supposed to be… but I can guarantee you that it’s not a direction vector.

Like, why is it multiplying x and z by 2 and just arbitrarily plopping some random y value in for y?

You really need to go back to basics and learn the fundamental stuff. I suggest this first:
https://jmonkeyengine.github.io/wiki/tutorials/math/assets/fallback/index.html

oh thnx :slight_smile: ,but it seems it not working
collision NOT origin(457.57126, 0.100001335, 474.26443) dir:(457.59808, 0.100001335, 474.56326) refer(500.0, 1.0, 520.0)
collision HERE!! origin(457.57126, 0.100001335, 474.26443) dir:(457.59808, 0.100001335, 474.56326) refer(500.0, 1.0, 520.0)
(500.70758, 0.109428145, 519.0)

men they are not random actually,if you try to use this formula you will get the results :slight_smile:
and you can see them at system.out :smiley:
Origin and Direction are perfect,and perfectly where i want them,they are what must to be,and i can not multiplicate,and get same result :smiley: soo its not it :slight_smile:
just look on numbers i get in sys.out

I’ve noticide when i move around that it happens only on 1 corner
tried

               Vector3f dir = new Vector3f(
              geomDir.getWorldTranslation());

same thing

Oh and we learn this stuff in elementray school in russia soo… im pretty fine with it :slight_smile:

That’s because your “direction” is not a direction. Despite all arguments to the contrary, it’s clear you have no idea what you are talking about.

a) a direction vector will have a length of 1
b) it will be based a delta between two values… when you arbitrarily stick some values into y then it’s no longer a direction… it’s a position.

So, then I guess your code is working fine.

…or it’s possible you’ve forgotten a bunch of stuff. (Hint: you’ve forgotten a bunch of stuff)

I suggest reading that tutorial carefully again. Especially the part about direction vectors.

ehm… ok soo you say when i make a ray from origin(X457.57126, to dir:(X457.59808, i can actually hit
refer(X500.0, ? same for Z and for Y
btw just to be shure i’ve actually did red it ,and nothing seems wrong …
Now why do you think they are rundom? its as you say there is X1Y1 then X1Y5 in cartesian coordinates that show 2 objects locations ,and i say ,give a look whats in middle of them by creating a ray . Then you say ok,but giving they coordinates is rundom values for ray ?-? this is strange to me men… i cant get how their coordinates might be not good input values for vector…
have you actually cheked values i’ve put in? they are not random they are 2 exect locations,and all i expect from them is to check whats in the middle of them

well its not ,but i cant see why in your answers,look what i’ve used next

Vector3f origin=player.getWorldTranslation();
its an object in the world on coordinates from where i want a ray to go

Vector3f dir = new Vector3f(
geomDir.getWorldTranslation()); (geomDir its an object in the world on coordinates to where i want a ray to go

And i still have same problem… soo could you pls explain me what am i missing ,couse i red link and its say nothing differend from get where from ,and to go to ,and get whats inside…

and actually it works fine :slight_smile: exept for 1 diagonal collision that shouldnt be ,but all other collisions are perfect ,its like a line shoots from 1 corner and keeps in a short line for ever,or at least pretty far

its not multiplying x and z by 2 ,it takes X to -(Xfrom-Xto)*2it mult only this distance not whole X its in math basic rules ,first mult and divide ,then other operations soo it substracts distance form coordinates( basically it makes a vector lets say From X 1 To X 6 .
6-(1-6)*2= 6-(-5)*2
6-(-5)*2 =6-(-10)
6-(-10)=16
soo now new TO is 16 and not 6,when you do same for Z you get a ray to direction u wanted just forward

A ray to go from where?

In the above, dir is just a coordinate.

Usually, if you want to cast a ray, you have some idea of where you are starting from and some idea of where you are going to.

Vector3f start = myPlayer.getWorldTranslation();
Vector3f end = someOtherThings.getWorldTranslation();

Vector3f dir = end.subtract(start).normalizeLocal(); ← a direction vector of length 1

Or, you know where you want to start and you want to cast in some direction…

Vector3f dir = myPlayer.getWorldRotation().mult(Vector3f.UNIT_Z);

If a vector is not of length one then it is not a direction vector. It’s just some random coordinate in space. It’s hard to tell from the messy code you’ve posted, but it seems like there is a fundamental misunderstanding on what you think a direction vector is.

well i know here i want i to start and where i want it to finish

lets say i want it to start at Vector3f start = players.getWorldTranslation();
soo i supposed i have to put it at origin/start
and i want it to go to geomDir,soo ive put it to dir
Vector3f dir = geomDir…getWorldTranslation();
soo you say its wrong parameter? how can i convert it to right 1? and witch 1 is wrong here
start or dir?

Vector3f origin=player.getWorldTranslation();
from here

I mean, it’s literally exactly what I already said… except for instead of someOtherThings you replace that with geomDir.

Whatever you do, if you:
System.out.println(dir.length());

…and the result is not 1 (or within floating point error close to 1) then you do not have a direction vector.

Another thing I noticed in your original code, you could simplify your subtraction by using the .subtract() method for vectors. Subtracting the origin location from the destination location like this tends to be the most reliable way to do ray casting in my experience

     Vector3f dir = (geometry.getWorldTranslation().subtract(player.getWorldTranslation))

and like @pspeed is mentioning, the above vector would return varying lengths depending on how far away the player is from the geometry. So to fix that you need to use the .normalize() method to get a working direction vector.