Delayed rotation

Hi there all

This is my first post and don’t care about my spelling, English is not my native
language :).

So I’m creating just a little tank game, everything is fine, tank is moving and cannon turning.

The problem is

	
public void setDirection(float x, float y) { // parametres are mouse x,y
		
	float rx = x - this.getX();
	float ry = y - this.getY();
	this.angle = (float)Math.atan2(ry, rx); // cannon angle
}

This rotates the cannon okay, but, I would like to have some kind of delay to that turning (I mean, now
it’s instant.
How is such a thing even possible? Somehow with delta time? I suck at math and physics sooo. :slight_smile:

Well, if someone could help, I would really appreciate that!

Thanks in advance :slight_smile:

edit. I try to explain better…

The cannon is now facing direction x,y
When I move the mouse, the facing direction changes of course to nx,ny.
I want it to be so that cannon slowly rotates to the new facing direction… :slight_smile:

Note I am not talking about experience here - have yet to succesfully build a game that lets you change angles =(

However, I do know a little bit about interpolation and extrapolation, which you probably need to former here if my knowledge is correct, or a form of it at least.

Save those variables as currentX, currentY and have it move some percent or absolute amount max at a time toward the current x/y and you have done it. Note that if this is an FPS of some sort or requires ‘twitch’ responses, this should be configurable, because it will give the user a different response then what he does, which will annoy them if it’s twitch.

Here’s some code


protected static final float MAX_X_CHANGE = 10; // Random number
protected static final float MAX_Y_CHANGE = 10; // will need testing

protected float currentDesiredX;
protected float currentDesiredY;

protected float actualX;
protected float actualY;

protected void interpolateDirection() {
  float diffX = currentDesiredX - actualX;
  float diffY = currentDesiredY - actualY;
  if(Math.abs(diffX) <= MAX_X_CHANGE) {
    actualX = currentDesiredX;
  }else {
    actualX += Math.signum(diffX) * MAX_X_CHANGE;
  }
  
  if(Math.abs(diffY) <= MAX_Y_CHANGE) {
    actualY = currentDesiredY;
  }else {
    actualY += Math.signum(diffY) * MAX_Y_CHANGE;
  }
}

EDIT - noticed it’s just rotation, in which you can just modify the code appropriately. The variable names can be improved =P

Well thanks :), I will take a look at it

Ugh, dunno how to implement this…

Haha

Well you have this.angle.
You could make another variable this.currentangle

then use something like this:


float diff = this.angle - this.currentangle;
if(diff < 0){ diff += (float)Math.PI * 2; }

if(diff < Math.PI){
    this.currentangle += 0.01f;
}else{
    this.currentangle -= 0.01f;
}

You check the diffrence between the current and the destination angle.
If its close to the right, move right, else move left.

I always mess up + and - so you might need to switch the variables :slight_smile:

Heh okay thx. That’s seems easier :slight_smile:

Thanks dude! That works veeery nicely! :slight_smile:

No problem, i remember i have had this problem once to.
The only problem you could have is; the cannon will be switching between 2 values all the time, with a greater movement rate you can spot this better.
Its easy to fix this problem anyway, didnt want to type to many code :slight_smile:

Yeh, I noticed that little bug :). Gonna try and fix it by myself :P. Thank you very much again :slight_smile: