Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   FireMonkey (https://www.clubdelphi.com/foros/forumdisplay.php?f=50)
-   -   Hacer girar un objeto... es dificil (a veces). (https://www.clubdelphi.com/foros/showthread.php?t=88930)

pokexperto1 28-08-2015 03:35:56

Hacer girar un objeto... es dificil (a veces).
 
Hola a todos, debido a mi corta edad y a que la educacion en españa ha bajado notablemente, me encuentro en 3º de ESO y todavia no hemos hecho nada sobre funciones, ¡Y eso que me apasiona el tema y me encanta la programación! Como no tengo a nadie que me enseñe normalmente busco por internet pero no he encontrado nada sobre este tema aplicado a lo que quiero hacer (la verdad es que no he buscado mucho :rolleyes:).
Os explico, lo que quiero hacer es una funcion que me devuelva un valor integer (RotationAngle.Y)
Se como hacer una función y todo eso, lo que no se es LO QUE TENGO QUE HACER
En el evento onKeyPress hago un case para detectar que flecha se ha pulsado.

EJEMPLO:
Código Delphi [-]
//SI PRESIONO ARRIBA
posfinal:=180;
AnimaciónRotación.Enabled=true;

//TIMER ANIMACIONROTACION
if Objeto.RotationAngle.Y=posfinal then
begin
AnimaciónRotación.Enabled=false;
end;
else
begin
Objeto.RotationAngle.Y:=Objeto.RotationAngle.Y+5;
end;
De momento mi objeto "gira". Hasta ahí todo bien.
El problema es que en el supuesto caso de que mi posicion actual sea 0 (abajo) y quiero que se mueva hasta 270 lo que quiero es que en vez de sumar +5 y hacer todo el recorrido para llegar a 270, sume -5 y haga el trayecto mas corto.

AQUÍ VIENE MI PREGUNTA:
¿HAY ALGUNA FORMA DE SABER SI TENGO QUE SUMAR 5 o -5 dependiendo de la posición?
Y no, no quiero hacer un if anidado no soy tan cutre.

¡Gracias de antemano!

PD: Lo he preguntado en este foro por que sois muy majos todos.

AgustinOrtu 28-08-2015 03:54:02

Aparentemente estas usando Firemonkey, creo que seria mas apropiado moverlo a ese foro

Que tipo de animacion es "AnimaciónRotación"?
Le configuraste alguna propiedad desde el inspector de objetos o en alguno lado via codigo?
Que es "Objeto"?

La aplicacion es 3D?

AgustinOrtu 28-08-2015 04:03:05

Haciendo experimentos rapido:

1. Aplicacion Firemonkey HD
2. Meto un TCube
3. Meto un TFloatAnimation, se la asigno al TCube
4. Seteo las propiedades como muestra la foto
5. Profit! El cubo gira y gira y gira :D


AgustinOrtu 28-08-2015 04:31:56

Mas pruebas:

Código Delphi [-]
unit Unit1;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, FMX.Types, FMX.Controls, FMX.Forms3D,
  FMX.Types3D, FMX.Forms, FMX.Graphics, FMX.Dialogs, System.Math.Vectors, FMX.Ani, FMX.Controls3D, FMX.Objects3D;

type
  TForm1 = class(TForm3D)
    Cube1: TCube;
    FloatAnimation1: TFloatAnimation;
    procedure Form3DKeyDown(Sender: TObject; var Key: Word; var KeyChar: Char; Shift: TShiftState);
    procedure Form3DKeyUp(Sender: TObject; var Key: Word; var KeyChar: Char; Shift: TShiftState);
    procedure Form3DCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.fmx}

procedure TForm1.Form3DCreate(Sender: TObject);
begin
  FloatAnimation1.Enabled := False;
  FloatAnimation1.AnimationType := TAnimationType.In;
  FloatAnimation1.AutoReverse := FalsE;
  FloatAnimation1.Delay := 0;
  FloatAnimation1.Duration := 0.1;
  FloatAnimation1.Interpolation := TInterpolationType.Linear;
  FloatAnimation1.Inverse := FalsE;
  FloatAnimation1.Loop := False;
  FloatAnimation1.PropertyName := 'RotationAngle.Y';
  FloatAnimation1.StartFromCurrent := True;
  FloatAnimation1.StartValue := 0;
  FloatAnimation1.StopValue := 360;
end;

procedure TForm1.Form3DKeyDown(Sender: TObject; var Key: Word; var KeyChar: Char; Shift: TShiftState);
begin
  if FloatAnimation1.Running then
    Exit;

  if Key in [vkLeft, vkRight] then
  begin
    FloatAnimation1.AnimateFloat(FloatAnimation1.PropertyName, FloatAnimation1.CurrentTime);
    case Key of
      vkLeft: FloatAnimation1.StopValue := Cube1.RotationAngle.Y + 10;
      vkRight: FloatAnimation1.StopValue := Cube1.RotationAngle.Y - 10;
    end;

    FloatAnimation1.Start;
  end;
end;

procedure TForm1.Form3DKeyUp(Sender: TObject; var Key: Word; var KeyChar: Char; Shift: TShiftState);
begin
  if (Key in [vkLeft, vkRight]) and (FloatAnimation1.Running) then
    FloatAnimation1.StopAtCurrent;
end;

end.

En este caso al presionar las teclas izquierda/derecha el cubo rota en la direccion indicada. Al soltar la tecla el cubo se detiene. No hace falta usar timer, de hecho las animaciones crean su propio thread para animar el objeto

pokexperto1 28-08-2015 04:38:47

Cita:

Empezado por AgustinOrtu (Mensaje 495959)
Haciendo experimentos rapido:

1. Aplicacion Firemonkey HD
2. Meto un TCube
3. Meto un TFloatAnimation, se la asigno al TCube
4. Seteo las propiedades como muestra la foto
5. Profit! El cubo gira y gira y gira :D


pero para el mismo lado xD

AgustinOrtu 28-08-2015 05:14:39

No respondiste las preguntas :)

En realidad la idea era darte una pista, realmente nunca había hecho algo semejante :D

pokexperto1 28-08-2015 05:15:43

Gracias a todos
 
pero creo que no me he explicado del todo bien, igualmente gracias xD


La franja horaria es GMT +2. Ahora son las 07:32:02.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi