I've managed to make a version that seems to work.
Unfortunately, you've misunderstood a couple of Processing-isms causing some problems.
You were doing a rotate, which rotates the entire screen, making xpos and ypos not where you'd think they should be.
For example if you rotate by 20 degrees, then what would be "right" in terms of an xpos value, becomes "right and down a bit" and ypos becomes "down and left a bit".
To solve that problem, you shoudl do a translate(xpos,ypos) to move the centre of any rotation to the point you want to draw, then the rotate, then draw a square around that new point.
Also, calling noLoop() and loop() is bad ju-ju, if noLoop() has happened, keyPressed() stops being looked at I think, and so once it's been caled once, your sketch is over. If you want to stop drawing for this frame, you just call "return;" which stops the draw() loop at that point for the frame (and subsequent ones if your boolean is true).
The "else" part fo your "if(trig)" was fairly pointless, in that it changed nothing. as was the "if(keyPressed)" in "void keyPressed()". If the function keyPressed is being run, that means a key must be pressed.
Anyway, here's a version of the program that does what it should, baring probbaly not working quite how you want if a key is pressed and held.
Quote: float xpos; //current position on the x axis
float ypos; //current position on the y axis
float xrest;//new random destination on the x axis
float yrest;//new random destination on the y axis
float speed;//sxp. slew rate (speed)
boolean trig = false; //state of key press
void setup()
{
size(400, 400); //size of window
background(255); //initial background color
//moved assignment into setup() just in case...
xpos=0;
ypos=0;
xrest=random(10,380);
yrest=random(10,380);
speed=90;
keyp=false;
}
void draw()
{
background(255); //background color
float xdist = xrest - xpos;
if(abs(xdist) > 1)
{
xpos = xpos + xdist/speed;
}
float ydist = yrest - ypos;
if(abs(ydist) > 1){
ypos = ypos + ydist/speed;
}
if(trig == true){
xrest = random(10, 380);
yrest = random(10, 380);
// noLoop();
return; // Maybe does what you want.
}
/* else
{
xrest = xrest; //destination x does not change
yrest = yrest; //nor does destination y
}*/ //This whole block does nothing.. so is useless.
float angle = atan2(ydist, xdist);
translate(xpos,ypos); // move 0,0 to in reality be xpos,ypos.
rotate(angle + HALF_PI); //Doesn't need the 90, and now rotates the screen to make the box rotate.
stroke(0); //square color
rect(-5, -5, 10, 10); //draw a 10 by 10 square, centered around the current "0,0" point.
}
void keyPressed()
{
//no key is currently pressed.
// if(keyPressed == true){ //test to see if any key is pressed
trig = true; //if pressed, change trig variable to true
// }
}
void keyReleased()
{
if(trig == true){ //test to see if any key is released
trig = false; //if released, change trig variable to false
// loop(); //and start the draw loop
}
}