Einzelnen Beitrag anzeigen

Benutzerbild von Lumpiluk
Lumpiluk

Registriert seit: 25. Dez 2008
110 Beiträge
 
#12

Re: OpenGL bewegen von Objekten geht schief

  Alt 5. Dez 2009, 15:08
Delphi-Quellcode:
var
  rotX, rotY, rotZ, transX, transY, transZ, TimeFactor: Single;
  StartTime, LoopTime: Integer;

//...

procedure Render;
begin
  LoopTime := gettickcount-starttime;
  TimeFactor := LoopTime*0.00000000001+1;
  StartTime := gettickcount;

  //keys
  if Active = True then
  begin
    if getasynckeystate(vk_down) < 0 then rotx := Rotx + 1*(TimeFactor);
    if getasynckeystate(vk_up) < 0 then rotx := Rotx - 1*(TimeFactor);
    if getasynckeystate(vk_left) < 0 then roty := Roty - 1*(TimeFactor);
    if getasynckeystate(vk_right) < 0 then roty := Roty + 1*(TimeFactor);
    if getasynckeystate(ord('D')) < 0 then transx := transx - 0.01*(TimeFactor);
    if getasynckeystate(ord('A')) < 0 then transx := transx + 0.01*(TimeFactor);
    if getasynckeystate(ord('W')) < 0 then transy := transy - 0.01*(TimeFactor);
    if getasynckeystate(ord('S')) < 0 then transy := transy + 0.01*(TimeFactor);
    if (getasynckeystate(ord('Q')) < 0) and (transz + 0.01*TimeFactor > 0) then
      transz := transz + 0.01*(TimeFactor);
    if (getasynckeystate(ord('E')) < 0) and (transz - 0.01*TimeFactor > 0) then
      transz := transz - 0.01*(TimeFactor);
    if getasynckeystate(vk_prior) < 0 then rotz := rotz + 1*(TimeFactor); //Bild auf
    if getasynckeystate(vk_next) < 0 then rotz := rotz - 1*(TimeFactor); //Bild ab
    if getasynckeystate(ord('R')) < 0 then
    begin
      rotx := 0; roty := 0; rotz := 0; transx := 0; transy := 0; transz := 1;
    end;

    if rotx > 360 then rotx := rotx - 360;
    if roty > 360 then roty := roty - 360;
    if rotz > 360 then rotz := rotz - 360;
  end;

  //...

  glTranslatef(0,0,-2);

  gltranslatef(transx,transy,0);
  glRotatef(Rotx,1,0,0);
  glRotatef(Roty,0,1,0);
  glRotatef(Rotz,0,0,1);

  //... Haus und Würfel zeichnen
end;
Sieht jetzt zwar nicht so schön aus, aber meistens mache ich es so ähnlich und es funktioniert
Der 'TimeFactor' bewirkt übrigens, dass sich bei unterschiedlichen Frameraten alles immernoch genauso schnell bewegt.
  Mit Zitat antworten Zitat