added rotation for move piece, fixed problems because rotation on the z axis is reversed

This commit is contained in:
Cedric Beck
2024-11-18 01:58:24 +01:00
parent c95beaeb14
commit 8d9a923970
5 changed files with 91 additions and 66 deletions

View File

@@ -58,8 +58,11 @@ public void simpleInitApp() {
testList.add(UUID.randomUUID());
boardView.addPlayer(Color.AIRFORCE, testList);
boardView.movePiece(test0, 0);
boardView.moveHomePiece(test1, Color.AIRFORCE, 0);
boardView.movePieceStart(test0, 0);
boardView.movePiece(test0, 4);
boardView.movePiece(test0, 5);
boardView.movePiece(test0, 8);
boardView.movePiece(test0, 9);
}

View File

@@ -47,11 +47,6 @@ public BoardView(MdgaApp app) {
initMap();
initCamera();
Vector3f previous = new Vector3f(1, 1, 0); // Example vector
Vector3f next = new Vector3f(2, 1, 0); // Example vector
float angle = getRotationMove(previous, next);
System.out.println((angle));
}
@@ -75,11 +70,16 @@ private void initMap() {
case cir -> addFigureToPlayerMap(assetToColor(BoardAsset.cir), assetOnMap);
case marine -> addFigureToPlayerMap(assetToColor(BoardAsset.marine), assetOnMap);
case node_normal, node_bonus, node_start ->
infield.addLast(displayAndControl(assetOnMap, new NodeControl()));
infield.add(displayAndControl(assetOnMap, new NodeControl()));
case node_home_black -> addHomeNode(homeNodesMap, Color.AIRFORCE, assetOnMap);
case node_home_blue -> addHomeNode(homeNodesMap, Color.NAVY, assetOnMap);
case node_home_green -> addHomeNode(homeNodesMap, Color.ARMY, assetOnMap);
case node_home_yellow -> addHomeNode(homeNodesMap, Color.CYBER, assetOnMap);
case node_wait_black -> addHomeNode(waitingNodesMap, Color.AIRFORCE, assetOnMap);
case node_wait_blue -> addHomeNode(waitingNodesMap, Color.NAVY, assetOnMap);
case node_wait_green -> addHomeNode(waitingNodesMap, Color.ARMY, assetOnMap);
case node_wait_yellow -> addHomeNode(waitingNodesMap, Color.CYBER, assetOnMap);
default -> displayAsset(assetOnMap);
}
}
@@ -87,7 +87,7 @@ private void initMap() {
private void initCamera() {
app.getFlyByCamera().setEnabled(true);
int zoom = 20;
int zoom = 15;
app.getCamera().setLocation(new Vector3f(-zoom, 0, zoom));
app.getCamera().lookAt(new Vector3f(0, 0, 0), new Vector3f(0, 0, 1));
@@ -121,7 +121,7 @@ private Spatial createModel(BoardAsset asset, Vector3f pos, float rot) {
String texName = asset.getDiffPath();
Spatial model = app.getAssetManager().loadModel(modelName);
model.scale(asset.getSize());
model.rotate((float) Math.toRadians(0), 0, (float) Math.toRadians(90 + rot));
model.rotate((float) Math.toRadians(0), 0, (float) Math.toRadians(rot));
model.setLocalTranslation(pos);
model.setShadowMode(RenderQueue.ShadowMode.CastAndReceive);
Material mat = new Material(app.getAssetManager(), "Common/MatDefs/Light/Lighting.j3md");
@@ -143,6 +143,7 @@ public void addPlayer(Color color, List<UUID> uuid) {
for (int i = 0; i < playerAssets.size(); i++){
pieces.put(uuid.get(i), displayAndControl(playerAssets.get(i), new PieceControl()));
}
}
//displays an assets and return the created asset
@@ -158,10 +159,19 @@ private <T extends AbstractControl> T displayAndControl(AssetOnMap assetOnMap, T
return control;
}
public void movePiece(UUID uuid, int nodeIndex){
public void movePieceStart(UUID uuid, int nodeIndex){
movePieceToNode(pieces.get(uuid), infield.get(nodeIndex));
}
public void movePiece(UUID uuid, int nodeIndex){
PieceControl pieceControl = pieces.get(uuid);
System.out.println(pieceControl.getRotation());
NodeControl nodeControl = infield.get(nodeIndex);
pieceControl.rotate(getRotationMove(pieceControl.getLocation(),nodeControl.getLocation(),pieceControl.getRotation()));
System.out.println(pieceControl.getRotation());
movePieceToNode(pieceControl, nodeControl);
}
private void movePieceToNode(PieceControl pieceControl, NodeControl nodeControl){
pieceControl.setLocation(nodeControl.getLocation());
}
@@ -174,35 +184,23 @@ public void moveHomePiece(UUID uuid, Color color, int index){
private void addHomeNode(Map<Color, List<NodeControl>> map, Color color, AssetOnMap assetOnMap){
List<NodeControl> homeNodes = map.getOrDefault(color, new ArrayList<>());
homeNodes.addLast(displayAndControl(assetOnMap, new NodeControl()));
homeNodes.add(displayAndControl(assetOnMap, new NodeControl()));
map.put(color, homeNodes);
assert(homeNodes.size() <= 4) : "BoardView: to many homeNodes for " + color;
}
private float getRotationMove(Vector3f prev, Vector3f next, Rotation rotation){
Vector3f sub = next.subtract(prev);
if(sub.x != 0 && sub.y != 0){
private float getRotationMove(Vector3f prev, Vector3f next, float rotation) {
if(prev == null) return rotation;
}
else if(sub.x != 0){
return sub.x > 0 ?
90 //RIGHT
:
-90;
}
else if(sub.y != 0 ){
// Calculate the direction vector from prev to next
Vector3f direction = next.subtract(prev).normalizeLocal();
}
System.out.println(prev.x);
return 0;
}
// Determine the target angle based on the direction vector (we only care about movement on the X and Z axes)
float targetAngle = (float) Math.toDegrees(Math.atan2(direction.x, direction.y));
private Rotation rotationToEnum(float rot){
assert(-180 <= rot && rot <= 180) : "BoardView: Invalid rotation";
if(-45 <= rot && rot < 45) return Rotation.UP;
if(45 <= rot && rot < 135) return Rotation.RIGHT;
if(135 <= rot && rot <= 180 || -180 <= rot && rot < -135) return Rotation.DOWN;
if(-135 <= rot && rot <= -45) return Rotation.LEFT;
return null;
float angleDifference = targetAngle - rotation;
return angleDifference;
}
}

View File

@@ -1,22 +1,32 @@
package pp.mdga.client.board;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.AbstractControl;
public class PieceControl extends AbstractControl {
private Rotation rotation;
private float rotation;
public PieceControl(Rotation rotation){
public PieceControl(){
super();
this.rotation = rotation;
}
public Rotation getRotation() {
public float getRotation() {
return rotation;
}
public void rotate(float rot){
this.rotation = this.rotation + rot;
this.getSpatial().rotate(0, 0, (float) Math.toRadians(-rot));
}
public Vector3f getLocation(){
return this.getSpatial().getLocalTranslation();
}
@Override
protected void controlUpdate(float v) {
@@ -30,4 +40,14 @@ protected void controlRender(RenderManager renderManager, ViewPort viewPort) {
public void setLocation(Vector3f loc){
this.getSpatial().setLocalTranslation(loc);
}
@Override
public void setSpatial(Spatial spatial){
super.setSpatial(spatial);
if(spatial != null){
Quaternion rot = spatial.getLocalRotation();
this.rotation = (float) Math.toDegrees(rot.toAngleAxis(new Vector3f(Vector3f.UNIT_Z)));
}
}
}

View File

@@ -4,5 +4,9 @@ public enum Rotation {
UP,
RIGHT,
DOWN,
LEFT
LEFT,
UP_LEFT,
UP_RIGHT,
DOWN_RIGHT,
DOWN_LEFT
}

View File

@@ -1,10 +1,10 @@
world 0,0 0
world 0,0 90
#Marine Pos
marine 4,-5 180
marine 4,-4 180
marine 5,-4 180
marine 5,-5 180
marine 4,-5 270
marine 4,-4 270
marine 5,-4 270
marine 5,-5 270
#Blue (Marine) wait Node
node_wait_blue 4,-5 0
@@ -13,10 +13,10 @@ node_wait_blue 5,-4 0
node_wait_blue 5,-5 0
#Lw Pos
lw -5,4 0
lw -4,4 0
lw -4,5 0
lw -5,5 0
lw -5,4 90
lw -4,4 90
lw -4,5 90
lw -5,5 90
#Black (Lw) wait Node
node_wait_black -5,4 0
@@ -25,10 +25,10 @@ node_wait_black -4,5 0
node_wait_black -5,5 0
#Heer Pos
heer -4,-5 90
heer -4,-4 90
heer -5,-4 90
heer -5,-5 90
heer -4,-5 180
heer -4,-4 180
heer -5,-4 180
heer -5,-5 180
#Green (Heer) Wait Node
node_wait_green -4,-5 0
@@ -37,22 +37,22 @@ node_wait_green -5,-4 0
node_wait_green -5,-5 0
#CIR Pos
cir 4,5 -90
cir 4,4 -90
cir 5,4 -90
cir 5,5 -90
cir 4,5 0
cir 4,4 0
cir 5,4 0
cir 5,5 0
#Assets
jet -10,-1 -45
ship 11,0 79
big_tent -9,-7 40
big_tent 7,-10 135
small_tent -9,7 -45
small_tent -10,5 -30
radar 0,10 -110
small_tent 6,8 100
small_tent 8,7 70
tank -1,-10 45
jet -10,-1 45
ship 11,0 169
big_tent -9,-7 130
big_tent 7,-10 225
small_tent -9,7 45
small_tent -10,5 60
radar 0,10 -20
small_tent 6,8 190
small_tent 8,7 160
tank -1,-10 135
#Yellow (CIR) wait Node