I’m having some problems with rays when I rotate bones (in what I believe is termed a custom animation), and am wondering if anyone can help me.
I put a bone (called head_target) in the middle of each character’s head, and made it so that NPCs use a ray from their eyes to the head_target bone to determine whether that character is in their line of sight. The ray either collides with the character’s head mesh first (which means there’s a clear line of sight), or collides with something else first (which means there isn’t a clear line of sight).
I then added some code to rotate the chest bone of characters when they aim their weapons. Rotating the chest bone causes all the bones above it, including the head_target bone, to move. I did this using
setUserControl(true), and then
setUserTransforms(Vector3f.ZERO, rotation, Vector3f.UNIT_XYZ) on the chest bone. Visually, this works fine.
However, I then found that when a character (PC or NPC) is aiming his weapon, the rays going to that character often don’t collide with anything at all. Some debugging reveals that the rays are behaving (colliding or not colliding) as though the mesh was where it was before the bone was rotated. The head_target bone is in the correct place, and the rays are going to that bone, but they don’t actually register any collisions. I’ve been trying to figure out what I’m doing wrong, but nothing I’ve tried has worked.
I’ve reproduced the problem by adding a few lines of code to the TestCustomAnim example class (which is what I used to understand how to rotate bones in the first place).
Rather than print out the whole class, I’ll just give the code that I add to the simpleUpdate method:
Ray ray = new Ray(new Vector3f(-10, 0.9f, 0.9f), new Vector3f(1, 0, 0)); CollisionResults results = new CollisionResults(); rootNode.collideWith(ray, results); System.out.println(results.size() == 0 ? "No collisions" : results.getClosestCollision().getContactPoint());
I’m casting a ray at one corner of the box. As it rotates, that ray should alternately collide and not collide with the box. However, in practise, the ray always collides with the box.
I’ve run this on 3.0 and 3.1, and the same thing happens in each.