Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Move by Keypressing  (Read 5546 times)

0 Members and 1 Guest are viewing this topic.

Metaby

  • Newbie
  • *
  • Posts: 23
    • View Profile
Move by Keypressing
« on: May 10, 2008, 02:01:54 pm »
Hello, I have the following problem
I have an green background, and a Texture from a knight,
the Texture can be rotatet by using the Arrow Keys right, and left,
I have one variable named Angle

Angle = 0;

Angle goes from 0-360, by using the arrow keys, the number of arrow is changing, so we can rotate the textur,

but now, i want to let the knight walk, key up should move the knight into his line of sight forward, and key down should move the knight backward.

I have tried anything, but nothing worked right...
Does anybody has an idea ?

sorry for my bad english, mfg max
I hacked 127.0.0.1 *g*

deps

  • Newbie
  • *
  • Posts: 14
    • View Profile
Move by Keypressing
« Reply #1 on: May 10, 2008, 02:15:14 pm »
Code: [Select]

x = x + cos(angle) * speed;
y = y + sin(angle) * speed;

You might have to convert the angle to radians for this to work.
Cannot remember exactly how, but a quick google should help you on that.

Metaby

  • Newbie
  • *
  • Posts: 23
    • View Profile
Move by Keypressing
« Reply #2 on: May 10, 2008, 02:19:48 pm »
yes, thanks, a buddy from me, have said the same, I have a big code for this, but it don't works, perhaps there is an failure, here is the code
Code: [Select]
void Player::Input(RenderWindow *Game)
{
Angle_Bogen = 2 * 3.1415 / 360 * Angle;
if (Game->GetInput().IsKeyDown(Key::Left))
{
Angle += 5;
}
if (Game->GetInput().IsKeyDown(Key::Right))
{
Angle -= 5;
}
if (Angle > 0)
{
Angle += 360;
}
if (Angle > 360)
{
Angle -= 360;
}
if (Game->GetInput().IsKeyDown(Key::Up))
{
if (Angle > 90 && Angle < 180)
{
X = 5 * cos(Angle_Bogen - 3.1415 / 2);
Y = 5 * sin(Angle_Bogen - 3.1415 / 2);
}
else if (Angle > 270)
{
X = 5 * cos(Angle_Bogen - 3.1415 / 2 * 3);
Y = 5 * sin(Angle_Bogen - 3.1415 / 2 * 3);
}
else if (Angle >= 180 && Angle <= 270)
{
X = 5 * cos(Angle_Bogen);
Y = 5 * sin(Angle_Bogen);
}
else
{
X = 5 * cos(Angle_Bogen - 3.1415);
Y = 5 * sin(Angle_Bogen - 3.1415);
}
PosX += (int) X;
PosY += (int) Y;
}
if (Game->GetInput().IsKeyDown(Key::Down))
{
if (Angle > 90 && Angle < 180)
{
X = 5 * cos(Angle_Bogen - 3.1415 / 2);
Y = 5 * sin(Angle_Bogen - 3.1415 / 2);
}
else if (Angle > 270)
{
X = 5 * cos(Angle_Bogen - 3.1415 / 2 * 3);
Y = 5 * sin(Angle_Bogen - 3.1415 / 2 * 3);
}
else if (Angle >= 180 && Angle <= 270)
{
X = 5 * cos(Angle_Bogen);
Y = 5 * sin(Angle_Bogen);
}
else
{
X = 5 * cos(Angle_Bogen - 3.1415);
Y = 5 * sin(Angle_Bogen - 3.1415);
}
PosX -= (int) X;
PosY -= (int) Y;
}
}


Mfg Max
I hacked 127.0.0.1 *g*

deps

  • Newbie
  • *
  • Posts: 14
    • View Profile
Move by Keypressing
« Reply #3 on: May 10, 2008, 02:26:35 pm »
Code: [Select]
  if (Angle > 0)
   {
      Angle += 360;
   }
   if (Angle > 360)
   {
      Angle -= 360;
   }

Now that doesn't look completley right, does it? ;)

Metaby

  • Newbie
  • *
  • Posts: 23
    • View Profile
Move by Keypressing
« Reply #4 on: May 10, 2008, 02:34:57 pm »
hm...what do you meaning?
ähm.. it works now, i have tried a little bit to change the code,
my knight walks perfekt into his line of sight, forward and backward...
Code: [Select]
void Player::Input(RenderWindow *Game)
{
Angle_Bogen = 2 * 3.1415 / 360 * (Angle + 90);
if (Game->GetInput().IsKeyDown(Key::Left))
{
Angle += 5;
}
if (Game->GetInput().IsKeyDown(Key::Right))
{
Angle -= 5;
}
if (Angle > 0)
{
Angle += 360;
}
if (Angle > 360)
{
Angle -= 360;
}
if (Game->GetInput().IsKeyDown(Key::Up))
{
X = 5 * cos(Angle_Bogen);
Y = 5 * sin(Angle_Bogen);
PosX -= (int) X;
PosY += (int) Y;
}
if (Game->GetInput().IsKeyDown(Key::Down))
{
X = 5 * cos(Angle_Bogen);
Y = 5 * sin(Angle_Bogen);
PosX += (int) X;
PosY -= (int) Y;
}
}
I hacked 127.0.0.1 *g*

deps

  • Newbie
  • *
  • Posts: 14
    • View Profile
Move by Keypressing
« Reply #5 on: May 10, 2008, 02:43:55 pm »
Code: [Select]
  if (Angle > 0)
   {
      Angle += 360;
   }
   if (Angle > 360)
   {
      Angle -= 360;
   }


Let's pretend Angle is 1.
Ok, then we, following the logic above, adds 360 to Angle, since Angle is greater than 0.
Now Angle is 361, and we must now substract 360 from it, making Angle equal to 1. again.

I do believe this is what you should use;
Code: [Select]
  if (Angle < 0)
   {
      Angle += 360;
   }
   if (Angle >= 360)
   {
      Angle -= 360;
   }

Now, Angle will increase by 360 if it is below 0.
And Angle will decrease by 360 if it greater or equal to 360.

Metaby

  • Newbie
  • *
  • Posts: 23
    • View Profile
Move by Keypressing
« Reply #6 on: May 10, 2008, 02:47:03 pm »
hm.. i think you're right! ;)
thanks, but I am happy about that, that the code works^^
I hacked 127.0.0.1 *g*

Julien_v42

  • Newbie
  • *
  • Posts: 26
    • View Profile
    • http://tinyrpgsim.wordpress.com
Move by Keypressing
« Reply #7 on: May 10, 2008, 05:05:46 pm »
Just wanted to mention:
Code: [Select]
Angle_Bogen = 2 * 3.1415 / 360 * (Angle + 90);

Personally, I'd rather not mix integers (2, 360, 90) with double precision (3.1415) in arithmetic operations, especially not if there are divisions involved.
Either this is meant to yield integer results, and I had explicit casting (like (int)(2*3.1415) for example, or static_cast<int>(2*3.1415)), or double, and I make all values double like this:
Code: [Select]
Angle_Bogen = 2.0 * 3.1415 / 360.0 * (Angle + 90.0);
Just to avoid some weird bugs when values get truncated in the middle of a computation.
Working on TinyRPGSim

Metaby

  • Newbie
  • *
  • Posts: 23
    • View Profile
Move by Keypressing
« Reply #8 on: May 10, 2008, 07:38:59 pm »
ok thanks, i've done this ;)

and i changed my code a little bit

Code: [Select]
Angle_Bogen = 0.0174532925 * (Angle + 90.0);

mfg Max
I hacked 127.0.0.1 *g*

 

anything