Flashlight troubles

I suppose this would be the proper place to put this, but I am working on a flash light for a game originating from the location of the player and pointing towards where they are looking. I’ve gotten almost all of this set up but I’ve hit a wall when it has come to pointing the flashlight in the right way, I can only get it to point at the ground.

I have the code below if anyone knows how to fix it but otherwise if anyone could point me towards some flashlight tutorials or something like that it would be much appreciated.

Also I was wondering if I have the proper inner and outer angles set, as this might have something to do with it…

[java]public static void playerFlashLight(Node rootNode){
flashLight = new SpotLight();
flashLight.setSpotRange(100);
flashLight.setSpotOuterAngle(35f * FastMath.DEG_TO_RAD);
flashLight.setSpotInnerAngle(35f * FastMath.DEG_TO_RAD);
flashLight.setColor(ColorRGBA.White.mult(2f));
flashLight.setPosition(playerControl.getPhysicsLocation());
flashLight.setDirection(camLeft);
rootNode.addLight(flashLight);
}

public static void updatePlayer(Camera cam){
    camDir = cam.getDirection().clone().multLocal(.5f);
    camLeft = cam.getLeft().clone().multLocal(.5f);
    flashLight.setDirection(camLeft);
    camDir.y = 0;
    camLeft.y = 0;       
    
    walkDirection.set(0, 0, 0);
    
    if(left){
        moveLeft();
    }
    if(right){
        moveRight();
    }
    if(up){
        moveUp();
    }
    if(down){
        moveDown();
    }
    
    cam.setLocation(playerControl.getPhysicsLocation());
    flashLight.setPosition(playerControl.getPhysicsLocation());
    playerControl.setWalkDirection(walkDirection);
}[/java] 

Thanks for your time!

I’m curious… what do you think these lines are doing?
camLeft = cam.getLeft().clone().multLocal(.5f);
camLeft.y = 0;

…and why would you want your flashlight to point in that (non) direction.

@pspeed said: I'm curious... what do you think these lines are doing? camLeft = cam.getLeft().clone().multLocal(.5f); camLeft.y = 0;

…and why would you want your flashlight to point in that (non) direction.

I set the flashlight pointing towards the camLeft before changing the camLeft.y to 0, which I do for the player moving left and right:

[java]public static void moveLeft(){
walkDirection.addLocal(camLeft);
}

public static void moveRight(){
    walkDirection.addLocal(camLeft.negate());
}[/java] 

I have also tried flashLight.setDirection(cam.getLeft()); which just does the same thing and I’ve tried flashLight.setDirection(cam.getDirection()); which just causes a massive black hole with only being able to see around the hole

@Benzoat said: I set the flashlight pointing towards the camLeft before changing the camLeft.y to 0, which I do for the player moving left and right:

[java]public static void moveLeft(){
walkDirection.addLocal(camLeft);
}

public static void moveRight(){
    walkDirection.addLocal(camLeft.negate());
}[/java] 

I have also tried flashLight.setDirection(cam.getLeft()); which just does the same thing and I’ve tried flashLight.setDirection(cam.getDirection()); which just causes a massive black hole with only being able to see around the hole

I didn’t think it was necessarily the problem but once you make a vector non-unit length it’s technically no longer a “direction vector”. Multiplying by 0.5 or zeroing a parameter makes it not a direction vector anymore unless you normalize it. It could cause issues so it’s important to keep in mind.

Anyway, does a regular directional light have these same issues? Maybe there is something wrong with your scene. Try pointing a directional light sideways and see if everything is black then too or something.

I’m not really familiar with how the spolight works but maybe someone else spots an error.

@Benzoat said: ... Also I was wondering if I have the proper inner and outer angles set, as this might have something to do with it… ... flashLight.setDirection(cam.getDirection()); which just causes a massive black hole with only being able to see around the hole

you SHOULD set the direction of the flashLight to the direction of the cam.getDirection()…
you SHOULD NOT set the inner and outer light angles to the same value.

after testing:
with both angles set to 35f, everything outside of the lightcone is illuminated and nothing within it, hence the blackhole effect.
setting 34f inner and 35 outer, everything inside the lightcone was illuminated (with a small border of light dropoff, as presumably desired.)

2 Likes
@Decoy said: you SHOULD set the direction of the flashLight to the direction of the cam.getDirection()... you SHOULD NOT set the inner and outer light angles to the same value.

after testing:
with both angles set to 35f, everything outside of the lightcone is illuminated and nothing within it, hence the blackhole effect.
setting 34f inner and 35 outer, everything inside the lightcone was illuminated (with a small border of light dropoff, as presumably desired.)

Thanks! That worked, much appreciated.