Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (565) Games in Android Showcase (151) games submitted by our members Games in WIP (606) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Delayed rotation  (Read 1618 times) 0 Members and 1 Guest are viewing this topic.
_Tumppu_

Junior Newbie

 « Posted 2012-09-27 16:06:48 »

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

 1  2  3  4  5  6  7 `   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.

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

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...
Tjstretchalot

Junior Devvie

Medals: 2
Projects: 1

 « Reply #1 - Posted 2012-09-27 16:17:23 »

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
 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24 `protected static final float MAX_X_CHANGE = 10; // Random numberprotected static final float MAX_Y_CHANGE = 10; // will need testingprotected 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
_Tumppu_

Junior Newbie

 « Reply #2 - Posted 2012-09-27 16:20:06 »

Well thanks , I will take a look at it
_Tumppu_

Junior Newbie

 « Reply #3 - Posted 2012-09-27 18:15:24 »

Ugh, dunno how to implement this....
RobinB

JGO Ninja

Medals: 44
Projects: 1
Exp: 3 years

Spacegame in progress

 « Reply #4 - Posted 2012-09-27 20:34:44 »

Haha

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

then use something like this:

 1  2  3  4  5  6  7  8 `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
_Tumppu_

Junior Newbie

 « Reply #5 - Posted 2012-09-27 20:56:41 »

Haha

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

then use something like this:

 1  2  3  4  5  6  7  8 `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

Heh okay thx. That's seems easier
_Tumppu_

Junior Newbie

 « Reply #6 - Posted 2012-09-27 21:04:25 »

Haha

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

then use something like this:

 1  2  3  4  5  6  7  8 `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

Thanks dude! That works veeery nicely!
RobinB

JGO Ninja

Medals: 44
Projects: 1
Exp: 3 years

Spacegame in progress

 « Reply #7 - Posted 2012-09-27 22:13:57 »

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
_Tumppu_

Junior Newbie

 « Reply #8 - Posted 2012-09-28 04:49:32 »

Yeh, I noticed that little bug . Gonna try and fix it by myself . Thank you very much again
Pages: [1]
 ignore  |  Print

You cannot reply to this message, because it is very, very old.

 ags1 (21 views) 2015-03-31 10:55:12 theagentd (13 views) 2015-03-27 23:08:20 wxwsk8er (54 views) 2015-03-20 15:39:46 Fairy Tailz (47 views) 2015-03-15 21:52:20 Olo (29 views) 2015-03-13 17:51:59 Olo (32 views) 2015-03-13 17:50:51 Olo (39 views) 2015-03-13 17:50:16 Olo (44 views) 2015-03-13 17:47:07 ClaasJG (61 views) 2015-03-10 11:36:42 ClaasJG (43 views) 2015-03-10 11:33:01
 BurntPizza 21x LiquidNitrogen 21x basil_ 19x KevinWorkman 18x EgonOlsen 17x Roquen 17x theagentd 16x Varkas 12x wessles 11x 65K 11x Riven 11x Rayvolution 9x phu004 8x SHC 8x princec 8x Ashedragon 8x
 How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org