Please login or register.

Login with username, password and session length
Advanced search  

News:

This forum provides RSS feed. To query recent posts use this url. More...


Author Topic: Question about keyboard support  (Read 2637 times)

0 Members and 1 Guest are viewing this topic.

Juan Bonair

  • Lurker
  • *
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 33
    • View Profile
Question about keyboard support
« on: April 22, 2010, 09:04:17 PM »

Hi y'all!

Some weeks ago I came up with this idea for the movement of a character in a game, and I wanted to know if it was possible to make it with Wintermute (I've been reading the forums for a year or so, so by now I don't think anything is impossible with Wintermute).

I've seen this, so I know the arrow keys can make my actor walk (and they do, flawlessly). But what if the floor region was circular (I mean, like a circle with a smaller and also circular not-walkable region inside)?

1) Could I make my actor (or some entity) walk clockwise with, say, the right arrow key, and counterclockwise with the left arrow?

2) And if my floor region was a big S that connects the bottom left corner of the scene with the top right, could I make my actor go through the S with one key, even if it means that it must go right and left and right again (and, of course, stop anywhere when the arrow key is released and go back with the other arrow)?

I guess that, if possible, it has something to do with waypoints, but I really don't know.
So basically I'm asking if this kind of movement is possible and how could I make it happen, and if it's not then what would you suggest as a workaround.

Nothing more, nothing less.
And I guess you get this a lot, but I'll say it anyway: sorry for my bad English and ask again if you didn't get the question.
Logged

metamorphium

  • Global Moderator
  • Addicted to WME forum
  • *
  • Karma: 12
  • Offline Offline
  • Gender: Male
  • Posts: 1511
  • Vampires!
    • View Profile
    • CBE  software s.r.o.
Re: Question about keyboard support
« Reply #1 on: April 23, 2010, 08:56:53 AM »

As you are speaking about direct control, waypoints are useless. For direct control, you have to script your movement directly. So eg. for circular motion you can multiply results from Math.Sin and Math.Cos to determine the trajectory.

Example for circular motion (untested)

Code: WME Script
  1.  
  2. var ent = Scene.GetNode("someEntity");
  3.  
  4. var radius = 100; // circle radius
  5. var degree = 0; // IMPORTANT! wme Math.Sin and Math.Cos works with degrees NOT radians !IMPORTANT
  6.  
  7. var centerX = ent.X;
  8. var centerY = ent.Y;
  9.  
  10. while(1)
  11. {
  12.     degree = degree + 1;
  13.     if (degree == 360) degree = 0;
  14.    
  15.     ent.X =  centerX + Math.Sin(degree) * radius;
  16.     ent.Y = centerY + Math.Cos(degree) * radius;
  17.    
  18.     Sleep(50);
  19. }
  20.  
  21.  

Now for your circular character motion you simply don't put it to loop but rather make the degree value global and increment it on clockwise motion and decrement it on counter-clockwise motion.

Also for regular S movement you can use the simple Math.Sin * radius which would be applied only to one axis depending on what orientation does the S have.

EDIT: As I am in math mood today, if you want to have elipse rather than circle, you can tweak the code like this:

Code: WME Script
  1.  
  2. var ent = Scene.GetNode("someEntity");
  3.  
  4. var radiusWidth = 100;
  5. var radiusHeight = 50;
  6.  
  7. var degree = 0; // IMPORTANT! wme Math.Sin and Math.Cos works with degrees NOT radians !IMPORTANT
  8.  
  9. var centerX = ent.X;
  10. var centerY = ent.Y;
  11.  
  12. while(1)
  13. {
  14.     degree = degree + 1;
  15.     if (degree == 360) degree = 0;
  16.    
  17.     ent.X =  centerX + Math.Sin(degree) * radiusWidth;
  18.     ent.Y = centerY + Math.Cos(degree) * radiusHeight;
  19.    
  20.     Sleep(50);
  21. }
  22.  
  23.  
« Last Edit: April 23, 2010, 09:26:35 AM by metamorphium »
Logged
J.U.L.I.A. Enhanced Edition, Vampires!, J.U.L.I.A., J.U.L.I.A. Untold, Ghost in the Sheet

Juan Bonair

  • Lurker
  • *
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 33
    • View Profile
Re: Question about keyboard support
« Reply #2 on: April 23, 2010, 05:46:26 PM »

Good Lord, fast answer!

K, I don't quite get the S movement yet, but I guess that's just because I don't get this all Trigonometry functions, so I'll have to investigate that myself.

The circular thing works like a charm (thank you!), but I would like to control certain things and I don't know how.

1) The circle coordinates. How do I control where in the scene is this circle going to be placed?

2) The initial entity coordinates. Every time y press the key to make my entity move, it skips to the bottom of the circle. How do I change that? It has something to do whit the var degree = 0; line?

3) The movement speed. Right now, directly from the code you gave me, the entity moves at some speed for a second or so, and after that it goes wildly fast.

Oh, and where do I put the code? Just for the quick test I made and attached directly to the game a mov.script that goes like this:

Code: [Select]
#include "scripts\base.inc"
#include "scripts\keys.inc"

var ent = Scene.GetNode("CosaQueGira");
 
var radiusWidth = 300;
var radiusHeight = 200;
 
var degree = 0; // IMPORTANT! wme Math.Sin and Math.Cos works with degrees NOT radians !IMPORTANT
 
var centerX = ent.X;
var centerY = ent.Y;

on "Keypress"
{
  while(Keyboard.IsKeyDown(VK_LEFT))
  {
   degree = degree + 1;
   if (degree == 360) degree = 0;
 
   ent.X =  centerX + Math.Sin(degree) * radiusWidth;
   ent.Y = centerY + Math.Cos(degree) * radiusHeight;
 
   Sleep(10);
  }
 
  while(Keyboard.IsKeyDown(VK_RIGHT))
  {
   degree = degree - 1;
   if (degree == 360) degree = 0;
 
   ent.X =  centerX + Math.Sin(degree) * radiusWidth;
   ent.Y = centerY + Math.Cos(degree) * radiusHeight;
 
   Sleep(10);
  }
}

How could I do it better or, as you folks say, more elegant? Because I want to change the movement behavior depending on the scene, and probably there's a better way to script it.
« Last Edit: April 23, 2010, 05:49:59 PM by Juan Bonair »
Logged

metamorphium

  • Global Moderator
  • Addicted to WME forum
  • *
  • Karma: 12
  • Offline Offline
  • Gender: Male
  • Posts: 1511
  • Vampires!
    • View Profile
    • CBE  software s.r.o.
Re: Question about keyboard support
« Reply #3 on: April 23, 2010, 06:55:55 PM »

let's take it step by step

1,

Code: WME Script
  1. var ent = Scene.GetNode("CosaQueGira");
  2. var centerX = ent.X;
  3. var centerY = ent.Y;
  4.  

This is the center of your circle. It's basically initial entity position as defined in Scene editor. By writing there values instead of ent.X or ent.Y you can make manual position. Note, that unless you make a sprite for your entity with hotspot in the middle of the entity, the X and Y is upper left corner of the entity (0,0)

2. I don't understand this. I tried this code and it actually worked out of the box. Must be something with your other code.

3. because it's poorly implemented. You're actually calling the loop again and again, so quick and dirty fix to your code could have been for example:

Code: WME Script
  1. on "Keypress"
  2. {
  3.   if (Scene.Rotating) return;
  4.        
  5.   while(Keyboard.IsKeyDown(VK_LEFT))
  6.   {
  7.    Scene.Rotating = true;       
  8.    degree = degree + 1;
  9.    if (degree >= 360) degree = 0;
  10.  
  11.    ent.X =  centerX + Math.Sin(degree) * radiusWidth;
  12.    ent.Y = centerY + Math.Cos(degree) * radiusHeight;
  13.               
  14.    Sleep(10);
  15.   }
  16.  
  17.   while(Keyboard.IsKeyDown(VK_RIGHT))
  18.   {
  19.    Scene.Rotating = true;       
  20.    degree = degree - 1;
  21.    if (degree < 0) degree = 359;
  22.  
  23.    ent.X =  centerX + Math.Sin(degree) * radiusWidth;
  24.    ent.Y = centerY + Math.Cos(degree) * radiusHeight;
  25.  
  26.    Sleep(10);
  27.   }
  28.  
  29.   Scene.Rotating = false;
  30. }
  31.  

hope this helps.
Logged
J.U.L.I.A. Enhanced Edition, Vampires!, J.U.L.I.A., J.U.L.I.A. Untold, Ghost in the Sheet

metamorphium

  • Global Moderator
  • Addicted to WME forum
  • *
  • Karma: 12
  • Offline Offline
  • Gender: Male
  • Posts: 1511
  • Vampires!
    • View Profile
    • CBE  software s.r.o.
Re: Question about keyboard support
« Reply #4 on: April 23, 2010, 07:02:04 PM »

btw. you can try adding this to your movement for S movement

Code: WME Script
  1.   while(Keyboard.IsKeyDown(VK_UP))
  2.   {
  3.    Game.Rotating = true;       
  4.    degree = degree - 1;
  5.    if (degree < 0) degree = 359;
  6.  
  7.    ent.Y =  centerY + Math.Sin(degree) * radiusWidth;
  8.    ent.X = ent.X + 1;
  9.  
  10.    Sleep(10);
  11.   }
  12.  
  13.  
Logged
J.U.L.I.A. Enhanced Edition, Vampires!, J.U.L.I.A., J.U.L.I.A. Untold, Ghost in the Sheet

Juan Bonair

  • Lurker
  • *
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 33
    • View Profile
Re: Question about keyboard support
« Reply #5 on: April 27, 2010, 06:02:05 PM »

OK, I see where this is going. I will not be able to easily customize the movement of my character for any irregular curve I may want to have as floor.

My bad, I asked for a simple (at least I thought it was) shape, assuming I could adapt the same concept to any shape.

Thanks anyway meta, you rock! And, well, if you come up with something more about this, let me know, I'm all ears.
Logged
 

Page created in 0.05 seconds with 25 queries.