Directional Light that follows a node?

Good evening everyone!

I’m having quite the pickle when it comes to lighting a sidescroller. As you can see by the two screen shots below, when I use

charControl.setWalkDirection(walkDirection);

to face Mario left the directional lighting light’s him up correct. When I face Mario right it’s like the directional lighting (or point of reference, but I don’t think it’s this as the whole level’s lighting stays good both left and right facing) is swapping to the other side of Mario. Or maybe the model is reversing the shadow/lighting effect?


Either way I’ve spent literally days on this issue and cannot figure it out. I’ll paste some relevant code below, please let me know if you need more.

Model:

/** Load Mario Model /
charGeom = (Geometry) assetManager.loadModel(“Models/Mario/Mario.j3o”);
/
* Move Mario Model */
charGeom.scale(.02f);
charNode = new Node(“Char Node”);
charNode.attachChild(charGeom);
charNode.setLocalTranslation(new Vector3f(8.0f, 0.0f, 0.0f));
// Create a appropriate physical shape for it
CapsuleCollisionShape collisionShape = new CapsuleCollisionShape(.5f, 1.20f);
charControl = new CharacterControl(collisionShape, .1f);
// Attach physical properties to model and PhysicsSpace
charNode.addControl(charControl);
// Rotate CharControl which rotates spatial
charControl.setViewDirection(Vector3f.UNIT_X);
bulletAppState.getPhysicsSpace().add(charControl);
// Attach charNode to rootNode
rootNode.attachChild(charNode);

Lighting:

/** Add AmbientLight /
AmbientLight ambient = new AmbientLight();
ambient.setColor(ColorRGBA.White);
rootNode.addLight(ambient);
/
* Add directional lighting */
DirectionalLight sun = new DirectionalLight();
sun.setColor(ColorRGBA.White);
sun.setDirection(new Vector3f(2.0f, -2.0f, -2.0f).normalizeLocal());
rootNode.addLight(sun);

Modified CameraControl.java (to allow the camera to follow Mario in the fashion of a sidescroller):

case SpatialToCamera:
if (Main.facingLeft) {
modifiedWorldTranslation = new Vector3f(spatial.getWorldTranslation().x,
7.5f , -spatial.getWorldTranslation().z);
camera.setLocation(modifiedWorldTranslation);
}
else {
modifiedWorldTranslation = new Vector3f(spatial.getWorldTranslation().x,
7.5f , spatial.getWorldTranslation().z);
camera.setLocation(modifiedWorldTranslation);
}
break;

initCamera (only called once):

private void initCamera() {
//create the camera Node
camNode = new CameraNode(“Camera Node”, cam);
//This mode means that camera copies the movements of the spatial:
camNode.setControlDir(ControlDirection.SpatialToCamera);
//Attach the camNode to the target:
charNode.attachChild(camNode);
//Position the camNode above and to the -X of the model/ground so it looks like a side-scroller
camNode.setLocalTranslation(camDistance);
//Rotate the camNode to look at the node, add height to make the camera focus above the model
camNode.lookAt(charNode.getLocalTranslation().add(cameraHeight), Vector3f.UNIT_Y);
// camNode.setLocalTranslation(charGeom.getWorldTranslation());
// camNode.setLocalRotation(charGeom.getWorldRotation());
}

it could be Mario’s model normals are all facing 1 direction?

Edit: (toward Mario’s left)

1 Like
@Decoy said: it could be Mario's model normals are all facing 1 direction?

Edit: (toward Mario’s left)

I was going to say the same thing. It does seem like a bad normals problem.

Maybe you aren’t setting normals at all so they end up defaulting to some default value (which of course will always be in the same direction).

Firstly; thank you both for the very prompt responses! I truely appreciate the help.

Do would that be an easy-ish fix you think? I ganked the model from a free site just to use temporarily, but it really annoys me to see it lol. I do have Blender setup (i exported it from there originally) if someone thinks it’d be simple to explain some things for me to look at/verify. 3D modeling is not my forte… yet :smiley:

If it’s too difficult I’ll just live with it until I get my final model.

Thanks again!
talitore

Did you import the model from blender as a .blend file directly? Maybe the default blender light is still attached? You can look at the model, its nodes and any lights in the SceneComposer.

It did. I found a fix online to correct normals in Blender (because I really have no clue about 3d models) :

Edit mode, select all vertices (AKEY), CTRL-N -> “Recalculate normals outside”, confirm.

That seems to have helped, but now I’m getting the dreaded com.jme3.scene.Node cannot be cast to com.jme3.scene.Geometry at charGeom = (Geometry) assetManager.loadModel(“Models/Mario/Mario2.j3o”); so I need to go find the solution to it again!

You’ve been a big help. I hope you understand how very thankful I am.
talitore

Open the model in scene composer and you can see what is imported. A single blender model often turns into multiple geometries on import (due to material differences etc) and these are then wrapped in a single node.

Your loaded model is most likely a Node containing one or more Geometry objects.

General Solution: Don’t cast loaded models to Geometry.

If this creates a new problem like “But I need to do XXX on my model that I can only do on a Geometry” then that is a different question. But your model is still a Node.

Yeah, I just dropped the cast and it loaded fine. Fixing the normal’s via that method didn’t correct my issue though. I’m over it and will just live with it.

Thanks!
talitore