Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (574) Games in Android Showcase (154) games submitted by our members Games in WIP (620) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Best motion algorithm  (Read 5197 times) 0 Members and 1 Guest are viewing this topic.
zazza991

Junior Newbie

 « Posted 2012-03-22 19:36:13 »

I seek an optimal algorithm to move via directional keys an image in a JFrame for a game. Someone knows how to help me? Thank you
Eli Delventhal

JGO Kernel

Medals: 42
Projects: 11
Exp: 10 years

Game Engineer

 « Reply #1 - Posted 2012-03-22 23:25:39 »

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29 `public class MoveableImage{    private float x, y;    private Image image;        public MoveableImage(Image i, float startX, float startY)    {        x = startX;        y = startY;        image = i;    }    public void draw(Graphics g)    {        g.drawImage(image, (int)x, (int)y);    }    public void moveTo(float newX, float newY)    {        x = newX;        y = newY;    }    public void moveBy(float deltaX, float deltaY)    {        x += deltaX;        y += deltaY;    }}`

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15 `MoveableImage image;public void keyReleased(KeyEvent e){    int c = e.getKeyCode();    if (c == KeyEvent.VK_LEFT)    {        image.moveBy(-1.0f, 0.0f);    }    if (c == KeyEvent.VK_DOWN)    {        image.moveBy(0.0f, 1.0f);    }    //etc.}`

Clear enough? That's not optimal, but sounds like you're really far from optimal. When you're a lot more comfortable coding I'd put the moveBy's into a game loop instead of being called directly by the keyReleased function.

See my work:
OTC Software
zazza991

Junior Newbie

 « Reply #2 - Posted 2012-03-22 23:32:08 »

Thank you. But if I keep the directional pad down the sprite will move quickly or slowly? to be clear I would like to create a super mario type platform
ra4king

JGO Kernel

Medals: 374
Projects: 3
Exp: 5 years

I'm the King!

 « Reply #3 - Posted 2012-03-23 00:21:25 »

It's up to you to change those "moveBy" values in keyPressed to your liking

Eli Delventhal

JGO Kernel

Medals: 42
Projects: 11
Exp: 10 years

Game Engineer

 « Reply #4 - Posted 2012-03-23 00:26:44 »

Yes. Also go read a programming book, because this all should be clear unless you're very inexperienced. No offense at all meant, by the way...

See my work:
OTC Software
ReBirth
 « Reply #5 - Posted 2012-03-23 06:47:40 »

Clearly no offense. By learning how object is you can figure it out easily. Next you can do complex things by yourself.

zazza991

Junior Newbie

 « Reply #6 - Posted 2012-03-23 12:37:13 »

I studied several books and supported tests in java. My problem is that I want an equal movement in case you press or hold a button. For this reason I asked
zazza991

Junior Newbie

 « Reply #7 - Posted 2012-03-23 13:12:34 »

now place the code, maybe it's better

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99  100  101  102  103  104  105  106  107  108  109  110  111  112  113  114  115  116  117  118  119  120  121  122  123  124  125  126  127  128  129  130  131  132  133  134  135  136  137  138  139  140  141  142  143  144  145  146  147  148  149  150  151  152  153  154  155  156  157  158  159  160  161  162  163  164  165  166  167  168  169  170  171  172  173  174  175  176  177  178  179  180  181  182  183  184  185  186  187  188  189  190  191  192 `package Immagini;import java.awt.BorderLayout;import java.awt.Dimension;import java.awt.Graphics;import java.awt.GridLayout;import java.awt.Image;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.KeyAdapter;import java.awt.event.KeyEvent;import java.io.File;import java.io.IOException;import javax.imageio.ImageIO;import javax.swing.ImageIcon;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.Timer;public class MovimentoFrame extends JFrame{   public static final int MILLISECONDI = 100;   public static final int SPRITE_DIM = 32;   public static final int COLONNE = 20;   public static final int RIGHE = 20;   public static final int APPLET_WIDTH = RIGHE*SPRITE_DIM;   public static final int APPLET_HEIGHT = COLONNE*SPRITE_DIM;   public static final int JUMP = SPRITE_DIM;   private JLabel[] arraylabel;   private ImageIcon imgPers;   private JLabel labelPers;   private JPanel jp;   private boolean isControlled=true;   private int x;   private int y;   private int inc_x;   private int inc_y;   private Timer timer;   private Image image;   private ImageIcon immagine;   private ImageIcon imgTexture;      public MovimentoFrame(int x0, int y0){      x=x0;      y=y0;      inc_x=inc_y=0;            try {         image = ImageIO.read(new File("/home/vincenzo/Scrivania/grass09.jpg"));      } catch (IOException e) {         // TODO Auto-generated catch block         e.printStackTrace();      }            getContentPane().setLayout(null); //tolgo il LayoutManager predefinito      getContentPane().setPreferredSize(new Dimension(APPLET_WIDTH, APPLET_HEIGHT));      imgPers = new ImageIcon("/home/vincenzo/Scrivania/persT.PNG");      labelPers = new JLabel(imgPers);      labelPers.setBounds(0, 0, 32, 32); // Imposto posizione e dimensione del mio JTextPane      labelPers.setOpaque(false);      jp = new BackgroundPanel(image);      jp.setLayout(null);      jp.setBounds(0, 0, APPLET_WIDTH, APPLET_HEIGHT); // Imposto posizione e dimensione del mio JPanel      //getContentPane().add(labelPers);      jp.add(labelPers);//      createImageIcon();      createPanelImage();      getContentPane().add(jp);       addKeyListener(new DirectionKeyListener());       timer = new Timer(MILLISECONDI, new ActionListener() {               public void actionPerformed(ActionEvent evt) {                   int x1 = x + inc_x;                   int y1 = y + inc_y;                   if ((x1>=0) && (x1=0) && (y1
Fokusas

Senior Devvie

Medals: 3
Projects: 1

 « Reply #8 - Posted 2012-03-23 14:44:45 »

If this is real code then it has some grammatical mistakes and compiler shouldn't compile or run.
Looks like you are missing some ; and break in some switch cases. Also there maybe some other problems that I don't see.
 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15 `   private class DirectionKeyListener extends KeyAdapter {                public void keyReleased(KeyEvent event) {                      switch (event.getKeyCode()) {                case KeyEvent.VK_UP:                case KeyEvent.VK_DOWN:                    inc_y=0;                    break;                case KeyEvent.VK_LEFT:                case KeyEvent.VK_RIGHT:                    inc_x=0;                    break;            }        }    }`

zazza991

Junior Newbie

 « Reply #9 - Posted 2012-03-23 15:04:06 »

Sorry but to me it always compiled this code, maybe you can give problems only because I do not find images why don't you have.
There is some optimization that I can do for this code?
teletubo
« League of Dukes »

JGO Ninja

Medals: 60
Projects: 4
Exp: 8 years

 « Reply #10 - Posted 2012-03-23 15:06:46 »

If this is real code then it has some grammatical mistakes and compiler shouldn't compile or run.
Looks like you are missing some ; and break in some switch cases. Also there maybe some other problems that I don't see.
 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15 `   private class DirectionKeyListener extends KeyAdapter {                public void keyReleased(KeyEvent event) {                      switch (event.getKeyCode()) {                case KeyEvent.VK_UP:                case KeyEvent.VK_DOWN:                    inc_y=0;                    break;                case KeyEvent.VK_LEFT:                case KeyEvent.VK_RIGHT:                    inc_x=0;                    break;            }        }    }`

I haven't fully read his code, but I see no syntatic problems here. You don't need to have a break for each case.

@zazza:
I highly recomend you checking some 4k skeletons. They're simple at most but you'll have a nice skeleton on drawing things in screen and treating keypresses, in the best ways available (and not using AWT, JLabels, ImageIcon which are definetely not meant to be used with games).

zazza991

Junior Newbie

 « Reply #11 - Posted 2012-03-23 15:10:45 »

thank the Council. I saw some skeletons 4 k at other times, but inside there were too many things that I didn't know
Fokusas

Senior Devvie

Medals: 3
Projects: 1

 « Reply #12 - Posted 2012-03-23 15:18:15 »

Well looks like my eyes deceive me. Because I only look at code but don't compiled it. But I think that you should put break after each case or don't make that case, because switch without breaks could execute unwanted code which don't belong to that case (or belong to other case). And I must admire to teletubo that awt, swing isn't made for games, but you can use it if you don't have anything better. (But mostly there is)

If this is real code then it has some grammatical mistakes and compiler shouldn't compile or run.
Looks like you are missing some ; and break in some switch cases. Also there maybe some other problems that I don't see.
 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15 `   private class DirectionKeyListener extends KeyAdapter {                public void keyReleased(KeyEvent event) {                      switch (event.getKeyCode()) {                case KeyEvent.VK_UP:                case KeyEvent.VK_DOWN:                    inc_y=0;                    break;                case KeyEvent.VK_LEFT:                case KeyEvent.VK_RIGHT:                    inc_x=0;                    break;            }        }    }`

Mike

« JGO Spiffy Duke »

Medals: 143
Projects: 1
Exp: 6 years

Java guru wannabe

 « Reply #13 - Posted 2012-03-23 15:51:44 »

But I think that you should put break after each case or don't make that case, because switch without breaks could execute unwanted code which don't belong to that case (or belong to other case)

switch!=if
That function is clean and correct.

Also, shouldn't this be placed in the newbie section?

Mike

My current game, Minecraft meets Farmville and goes online
State of Fortune | Discussion thread @ JGO
zyzz

Senior Newbie

 « Reply #14 - Posted 2012-03-24 02:26:01 »

zazza991

Junior Newbie

 « Reply #15 - Posted 2012-03-24 14:33:34 »

Guys I have modified my code through a 4k skeleton but I have the problem that my image moves too fast. Now place the 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  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99  100  101  102  103  104  105  106  107  108  109  110  111  112  113  114  115  116  117  118  119  120  121  122  123  124  125  126  127  128  129  130  131  132  133  134  135  136  137  138  139  140  141  142  143  144  145  146  147  148  149  150  151  152  153  154  155  156  157  158  159  160  161  162  163  164  165  166  167  168  169  170  171  172  173  174  175  176  177  178  179  180  181  182 `package Gioco;import java.applet.Applet;import java.awt.BorderLayout;import java.awt.Dimension;import java.awt.Graphics;import java.awt.Image;import java.awt.event.KeyEvent;import java.io.File;import java.io.IOException;import javax.imageio.ImageIO;import javax.swing.ImageIcon;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.Timer;public class Gioco extends Applet implements Runnable {   private boolean[] keyPress = new boolean[32768];   private final int SPRITE_DIM = 32;   private final int COLONNE = 20;   private final int RIGHE = 20;   private final int APPLET_WIDTH = RIGHE*SPRITE_DIM;   private final int APPLET_HEIGHT = COLONNE*SPRITE_DIM;   private final int JUMP = SPRITE_DIM;   private JLabel[] arraylabel;   private ImageIcon imgTexture;   private JPanel jp;   private Image image;   //altre inizializzazioni      public void start(){      enableEvents(8);       new Thread(this).start();   }      public void run(){      //variabili di istanza      int playerX = 0;      int playerY=0;      final int VK_LEFT = 0x25;       final int VK_RIGHT = 0x27;       final int VK_UP = 0x26;       final int VK_DOWN = 0x28;       final int VK_ATTACK = 0x42;      final int MILLISECONDI = 100;      ImageIcon imgPers;      JLabel labelPers;      boolean isControlled=true;      int x=0;      int y=0;      int inc_x;      int inc_y;      ImageIcon immagine;            try {         image = ImageIO.read(new File("C:/Users/vincenzo/Desktop/img/grass09.jpg"));      } catch (IOException e1) {         // TODO Auto-generated catch block         e1.printStackTrace();      }                     /*getContentPane().*/setLayout(null); //tolgo il LayoutManager predefinito      /*getContentPane().*/setPreferredSize(new Dimension(APPLET_WIDTH, APPLET_HEIGHT));      imgPers = new ImageIcon("C:/Users/vincenzo/Desktop/img/persT.PNG");      labelPers = new JLabel(imgPers);      labelPers.setBounds(0, 0, 32, 32); // Imposto posizione e dimensione del mio JTextPane      labelPers.setOpaque(false);      jp = new BackgroundPanel(image);      jp.setLayout(null);      jp.setBounds(0, 0, APPLET_WIDTH, APPLET_HEIGHT); // Imposto posizione e dimensione del mio JPanel      //getContentPane().add(labelPers);      jp.add(labelPers);//      createImageIcon();      createPanelImage();      /*getContentPane().*/add(jp);             //inizializzazione      long nextFrameStartTime = System.nanoTime();      //while true      while(true){         do{            nextFrameStartTime += 16666667;            //inizio aggiornamenti                        x = playerX;              y = playerY;               if (keyPress[VK_UP]) {               y -= JUMP;              } else if (keyPress[VK_DOWN]) {                  y += JUMP;              } else if (keyPress[VK_LEFT]) {                  x -= JUMP;              } else if (keyPress[VK_RIGHT]) {                  x += JUMP;              }                              if ((x>=0) && (x=0) && (y 0) {            Thread.yield();         }      }      //   }   public void processKeyEvent(KeyEvent keyEvent) {      final int VK_LEFT = 0x25;      final int VK_RIGHT = 0x27;      final int VK_UP = 0x26;      final int VK_DOWN = 0x28;      final int VK_ATTACK = 0x42;      final int VK_W = 0x57;      final int VK_S = 0x53;      final int VK_A = 0x41;      final int VK_D = 0x44;      int k = keyEvent.getKeyCode();      if (k > 0) {         k = k == VK_W ? VK_UP : k == VK_D ? VK_RIGHT : k == VK_A ? VK_LEFT               : k == VK_S ? VK_DOWN : k;         keyPress[(k >= VK_LEFT && k <= VK_DOWN) ? k : VK_ATTACK]               = keyEvent.getID() != 402; //=key released      }   }      private void createImageIcon() {      int i;      int j;            arraylabel = new JLabel[RIGHE];      imgTexture = new ImageIcon("C:/Users/vincenzo/Desktop/img/erbaTR.PNG");      for(i=0;i
Fokusas

Senior Devvie

Medals: 3
Projects: 1

 « Reply #16 - Posted 2012-03-24 14:59:27 »

But I think that you should put break after each case or don't make that case, because switch without breaks could execute unwanted code which don't belong to that case (or belong to other case)

switch!=if
That function is clean and correct.

Also, shouldn't this be placed in the newbie section?

Mike
Well looks who's here
But i think that if you think that my post is wrong you are wrong!
Switch without break is just a lines of code and no switch.
Case without break is damned to execute all other following cases and code;
P.S. No offence.
sproingie

JGO Kernel

Medals: 202

 « Reply #17 - Posted 2012-03-24 17:47:11 »

Switch-case fall-through was properly used in this case, end of story.

You also really shouldn't reuse Java4K code other than for another Java4K project.  Especially not to learn a new algorithm.
ReBirth
 « Reply #18 - Posted 2012-03-25 03:31:59 »

Nuff said, switch-case only makes my tall 'if' statement become beauty.

Eli Delventhal

JGO Kernel

Medals: 42
Projects: 11
Exp: 10 years

Game Engineer

 « Reply #19 - Posted 2012-03-26 22:52:56 »

Well looks who's here
But i think that if you think that my post is wrong you are wrong!
Switch without break is just a lines of code and no switch.
Case without break is damned to execute all other following cases and code;
P.S. No offence.
Will +1 what others have said, a drop-through switch case is totally fine. Perhaps what you are alluding to is a dropthrough case that has additional logic, which some view as bad practice (and it's not even valid in C#). But a flat dropthrough like that is fine everywhere.

Also I will +1 not using the 4k example as 4k does a lot of bad practices to reduce memory usage. Instead check out Basic Game tutorial and Game Loops tutorial. But since you've already adapted the 4k skeleton just give it a go I suppose.

See my work:
OTC Software
Pages: [1]
 ignore  |  Print

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

 Riven (28 views) 2015-04-16 10:48:47 Duke0200 (42 views) 2015-04-16 01:59:01 Fairy Tailz (32 views) 2015-04-14 20:13:12 Riven (33 views) 2015-04-12 21:36:37 bus hotdog (49 views) 2015-04-10 02:39:32 CopyableCougar4 (51 views) 2015-04-10 00:51:04 BurntPizza (51 views) 2015-04-06 22:06:58 ags1 (53 views) 2015-04-02 10:58:48 Riven (52 views) 2015-04-01 18:27:05 ags1 (69 views) 2015-03-31 10:55:12
 theagentd 27x BurntPizza 17x wessles 15x 65K 11x kingroka123 11x Rayvolution 11x alwex 11x KevinWorkman 9x kevglass 8x phu004 8x Ecumene 7x Roquen 7x chrislo27 7x Hanksha 7x SHC 7x Olo 7x
 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