- Edited
I have little problem and because I am quite new, I dont know how to handle it and will be grateful for any help.
So I have this test project that is like a rubik's cube, but i just want to test rotating group of elements around axis. I use only rotation but I think it will be necessary to use some kind of quaternion, which i dont know.
At this point, everything i want it to do works well except if i rotate row once and than want to rotate column, it will do the wrong rotation.
I think it can be visible on this video
Here is my code:
using Godot;
using System;
using System.Reflection;
public partial class TestKostky : Node3D
{
[Export] private MeshInstance3D kosticka;
[Export] private Node3D schranka;
[Export] private Node3D otoceni;
bool isTweening = false;
int cisloRadku = 0;
bool otoceniDoprava = false;
string osaOtoceni = null;
public void OtocKostku()
{
float hodnota = otoceniDoprava ? Mathf.DegToRad(90) : Mathf.DegToRad(-90);
isTweening = true;
Tween tween = GetTree().CreateTween();
tween.Finished += TweenFinished;
tween.TweenProperty(otoceni, osaOtoceni, hodnota, 1.0).AsRelative();
}
public override void _Input(InputEvent @event)
{
if (isTweening) return;
if (Input.IsActionJustPressed("rada"))
{
osaOtoceni = "rotation:y";
cisloRadku++;
cisloRadku = Mathf.Wrap(cisloRadku, -1, 2);
ZmenBarvu("Y");
}
if (Input.IsActionJustPressed("column"))
{
osaOtoceni = "rotation:x";
cisloRadku++;
cisloRadku = Mathf.Wrap(cisloRadku, -1, 2);
ZmenBarvu("X");
}
if (Input.IsActionJustPressed("row"))
{
osaOtoceni = "rotation:z";
cisloRadku++;
cisloRadku = Mathf.Wrap(cisloRadku, -1, 2);
ZmenBarvu("Z");
}
if (Input.IsActionJustPressed("RotateL"))
{
otoceniDoprava = false;
OtocKostku();
}
if (Input.IsActionJustPressed("RotateR"))
{
otoceniDoprava = true;
OtocKostku();
}
}
private void TweenFinished()
{
isTweening = false;
}
private void ZmenBarvu(string osa)
{
foreach (MeshInstance3D kostka in otoceni.GetChildren())
{
kostka.SetSurfaceOverrideMaterial(0, null);
kostka.Reparent(schranka);
}
foreach (MeshInstance3D kostka in schranka.GetChildren())
{
float axisValue;
switch (osa)
{
case "X":
axisValue = kostka.GlobalPosition.X;
break;
case "Y":
axisValue = kostka.GlobalPosition.Y;
break;
case "Z":
axisValue = kostka.GlobalPosition.Z;
break;
default:
GD.Print("Neplatná osa: " + osa);
return;
}
if (Math.Round(axisValue) == cisloRadku)
{
kostka.Reparent(otoceni);
StandardMaterial3D material = new StandardMaterial3D();
material.AlbedoColor = new Color(1, 0, 0);
kostka.SetSurfaceOverrideMaterial(0, material);
}
}
}
}