Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
0
Members

Rotating an object - what am I doing wrong?

A topic by rsbrowndog created Feb 17, 2016 Views: 707 Replies: 2
Viewing posts 1 to 3
(1 edit)

So I've been fiddling around with the Light and shadows demo and wanted to add controls to the plane so I could fly it around. Here's part of my script:


this.actor.moveOriented(0,0,0.1);

if (Sup.Input.isKeyDown("LEFT")) {

this.actor.rotate(new Sup.Math.Quaternion(0, 0, -0.01));

}

if (Sup.Input.isKeyDown("RIGHT")) {

this.actor.rotate(new Sup.Math.Quaternion(0, 0, 0.01));

}

if (Sup.Input.isKeyDown("UP")) {

this.actor.rotate(new Sup.Math.Quaternion(0.01,0,0))

}

if (Sup.Input.isKeyDown("DOWN")) {

this.actor.rotate(new Sup.Math.Quaternion(-0.01,0,0))

}


Now this all works up to a point. The plane rotates around and will fly up and down, but when the plane is rotated to say 90 degrees, pressing up still makes it go upwards, whereas I want it to go sideways. Like an actual plane would!

I've spent much of the day with the TypeScript API browser trying different things but still no joy.

Any suggestions?

Thanks!

Sounds like you are applying the rotation using global coordinates and angles. Perhaps try using the Local methods, rotateLocal and getLocalEulerAngles. There are so many of them, and they aren't well documented right now, but a bit of trial and error will get you there.

Thanks! Without your question I probably wouldn't have figured out how to rotate anything at all.

Using your code, changing the left/right to the following made rotation and forward movement seem ok at fist:

 if (Sup.Input.isKeyDown("LEFT")) {
this.actor.rotateLocal(new Sup.Math.Quaternion(0, 0.01, 0));
}
if (Sup.Input.isKeyDown("RIGHT")) {
this.actor.rotateLocal(new Sup.Math.Quaternion(0, -0.01, 0));
}

but when using larger values or starting off with one huge rotation, the model gets squished together:

https://youtu.be/4GElYV1-BJo

Full code (assumes "CatTankHead" 3d model exists):

let mainCharacterActor = new Sup.Actor("CatTankHead");
// Tell our actor to dress in a 3d model
new Sup.ModelRenderer(mainCharacterActor, "CatTankHead");
// Summon a second person on stage, that'll be the camera
let cameraActor = new Sup.Actor("Camera1");
// Give the person a camera
new Sup.Camera(cameraActor);
// Place our actors. The main character actor is at the center of the stage
mainCharacterActor.setPosition(0, -1, -4);
// The camera person will be looking at the main actor from a distance
cameraActor.setPosition(0, 0, 5);
class CatBehavior extends Sup.Behavior {
  awake() {
      this.actor.rotateLocal(new Sup.Math.Quaternion(0, 0, 0));
  }
  update() {
    if (Sup.Input.isKeyDown("LEFT")) {
      this.actor.rotateLocal(new Sup.Math.Quaternion(0, 0.1, 0));
    }
    if (Sup.Input.isKeyDown("RIGHT")) {
      this.actor.rotateLocal(new Sup.Math.Quaternion(0, -0.1, 0));
    }


    if (Sup.Input.isKeyDown("UP")) {
      this.actor.moveOriented(0,0,0.1);
    }


    if (Sup.Input.isKeyDown("DOWN")) {
      this.actor.moveOriented(0,0,-0.1);
    }
  }
}
mainCharacterActor.addBehavior(CatBehavior);