Can't get my object to move around axis

Hey guys,

I have this piece of code where I am trying to move a spaceship around the 0 axis. It also moves up and down. However, the further I move it up, the greater the turning circle is. I have tried various methods but cannot get it to where I want it to at the moment. If anyone could give me a hint that would be great!

int angle = 0;
PImage ship;
PVector shipVelocity, shipLocation;


void setup() {
ship = loadImage("asteroid_ship.png");
background(0);
smooth();
size(500,500);
noStroke();
shipVelocity = new PVector(1,1);
shipLocation = new PVector(1,1); 
}

void draw() {
if(keyPressed){
    if (key == CODED) {
        if (keyCode == RIGHT) {
            angle+=5;
        } 
        else if (keyCode == LEFT) {
           angle-=5;
        }
        else if (keyCode == UP) {
           shipLocation.y -= shipVelocity.y; 
        }
        else if (keyCode == DOWN) {
           shipLocation.y += shipVelocity.y; 
        }
    }
}
background(0);
translate(250,250);
rotate(radians(angle));
image(ship, shipLocation.x, shipLocation.y, 32, 38);
}
void keyPressed() {

}
1 Like

Use image with 0,0

Put the coordinates into translate 250+…

set imageMode (CENTER); in setup ()

1 Like

Hi, thanks for the response.

I tried changing those things. Still stuck on this translate function though and the hint that you gave me. Tried various additions of angles and using cosine and sine but still not working. The image is moving well, but the greater it moves from the original point, the less it spins around its origin.

set imageMode (CENTER); in setup ()

1 Like

int angle = 0;
PImage ship;
PVector shipVelocity, shipLocation;

void setup() {
  size(500, 500);

  ship = loadImage("img.png");
  background(0);
  smooth();

  noStroke();
  shipVelocity = new PVector(1, 1);
  shipLocation = new PVector(1, 1);

  imageMode(CENTER);
}

void draw() {
  if (keyPressed) {
    if (key == CODED) {
      if (keyCode == RIGHT) {
        angle+=5;
      } else if (keyCode == LEFT) {
        angle-=5;
      } else if (keyCode == UP) {
        shipLocation.y -= shipVelocity.y;
      } else if (keyCode == DOWN) {
        shipLocation.y += shipVelocity.y;
      }
    }
  }
  background(0);
  translate(250+shipLocation.x, 250+shipLocation.y);
  rotate(radians(angle));
  image(ship, 0, 0, 32, 38);
}
void keyPressed() {
}
2 Likes

Hi, I just tried that but it still does the same thing.

posted my entire code now

works for me

image must be with 0,0

  image(ship, 0, 0, 32, 38);

By the way

by the way… image() with 5 parameters is very time-consuming for the processor

instead use resize() in setup() once and for all

https://www.processing.org/reference/PImage_resize_.html

then say

  image(ship, 0, 0);

with only 3 parameters

Chrisir

2 Likes

Ahhh, my was issue with the translate section not with the imageMode in the end. I was overthinking it and using sine and cosine with the translate function.

Thank you so much! I really do appreciate your help (apologies for sounding so daft). I’ve been spending the last hour figuring this out.

1 Like

Thanks for that tip!

1 Like

You are welcome!

Chrisir

1 Like

Hello again,

So I have a new problem now. I am attempting to move it in the direction that it is pointing. So far, I have made some progress, but it seems to just be a roll of the dice in terms of where it is going.

        shipVelocity = PVector.fromAngle(angle + HALF_PI);
        shipLocation.add(shipVelocity);

Tried using this as well as adding cosine and sine to each individual component of the Location PVector but it’s not making much of a difference. Any tips here? Thanks in advance.

Update: All good! Realised that radians was causing my issue the whole time and also making my angle and int not a float was not helpful either. It now spins and moves in the correct direction :slight_smile:

1 Like