Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (739)
Games in Android Showcase (224)
games submitted by our members
Games in WIP (820)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Tile movement toward mouse not working correctly  (Read 870 times)
0 Members and 1 Guest are viewing this topic.
Offline Bassex96

Senior Devvie


Medals: 5



« Posted 2016-12-27 19:15:29 »

Hey guys, i'm playing around with code for a tower defense game i'm working on..or it will be a tower defense game when i'm done. Right now, i've been trying to play around with pathfinding and a movement system..I did some googling and tried to implement a system that will move the sprite tile by tile to where the mouse clicks. I haven't implemented astar or anything yet..I'm trying to get a basic movement system setup so I can move the sprite one tile at a time toward a mouse click...However, it isn't working. If I click below or to the left of the sprite, it will move extremely fast to the tile clicked. If I click above or to the right of the sprite, nothing happens.

I'm a little embarrassed that i'm having trouble doing this. I'm still a newbie but have been putting a little more time into learning lately. I'm having a problem getting things structured correctly and figuring out things like this. I'm not sure how to turn it into a decent working movement system. Here is the code i'm having a problem with. My tiles are 32x32, when I call the moveTo() function, i'm breaking the mouse position down into tile position by dividing by 32.

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  
public void moveTo(int px, int py) {
      state = state.WALKING;
     
      destx = px;
      desty = py;
     
      dx = px - x;
      dy = py - y;
     
      float length = (float) Math.sqrt(dx*dx+dy*dy);
     
      dx=dx/length;
      dy=dy/length;
     
     
     
   }
   
   
   
   public void update(float delta) {
     
         if (state == state.WALKING) {
           
         if (x != destx) {
            x += (dx * speed) * delta;  
         }else{
            dx = 0;
         }
           
         if (y != desty) {
            y += (dy * speed) * delta;        
         }else{
            dy = 0;
         }
         
         if (dx == 0 && dy == 0) {
            state = state.IDLE;
            destx = 0;
            desty = 0;
         }
         
         System.out.println("x: " + x +
               " y: " + y +
               " dx: " + dx +
               " dy: " + dy);
         
         }
      //System.out.println("X: " + x + " Y: " + y);
   }
Offline QuicK2800

Senior Newbie


Medals: 1
Exp: 4 years



« Reply #1 - Posted 2017-01-05 14:04:42 »

When I face a problem like this (one in which it seems simple, yet I can't get it to work no matter what), I do one of two things:
1) I start from scratch and develop a system that works (not recommended)
2) I start simple. In your case, making a function that successfully moves an entity from one tile to an adjacent tile and keep improving on this until you have reached the desired effect.

I would try first getting adjacent tile movement working (where you can freely move an entity around one tile at a time using wasd or any simple movement controls).

I would then work on automating movement along the axes so that I could move the entity any number of spaces in any of the four directions (or 8 if you are allowing diagonal movements).

Once both of these are functioning properly, I would use the axis aligned movement to create an 'L' shaped path to the point where I want the entity to move.

From there, you should be able to implement any kind of path-finding algorithm you would like.
(most of them could probably be done without the axis aligned movement)

As far as the "move extremely fast to the tile clicked" issue...try doing some research on delta-time for game development.
Offline kevglass

« JGO Spiffy Duke »


Medals: 319
Projects: 25
Exp: 22 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #2 - Posted 2017-01-05 14:08:35 »

More code please, especially what type dx, dy are.

Cheers,

Kev

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Bassex96

Senior Devvie


Medals: 5



« Reply #3 - Posted 2017-01-08 03:26:08 »

More code please, especially what type dx, dy are.

Cheers,

Kev

Here is the rest of the class. I'm still learning so please ignore the dirty code. I was playing around with switching everything to vectors as well so ignore the unused variables!

dx/dy are floating points.


Quote from: QuicK2800
As far as the "move extremely fast to the tile clicked" issue...try doing some research on delta-time for game development.

I'm using delta time already.

The sprite actually moves one tile at a time now, it just only moves in a negative direction. I took a break from the code. I'm going to go through it again and try to figure out what i'm doing wrong.

*EDIT* I actually got it working. I had it setup incorrectly. Here is the new pastebin
http://pastebin.com/yY3UMDUz
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 
Ecumene (56 views)
2017-09-30 02:57:34

theagentd (79 views)
2017-09-26 18:23:31

cybrmynd (187 views)
2017-08-02 12:28:51

cybrmynd (185 views)
2017-08-02 12:19:43

cybrmynd (192 views)
2017-08-02 12:18:09

Sralse (203 views)
2017-07-25 17:13:48

Archive (759 views)
2017-04-27 17:45:51

buddyBro (892 views)
2017-04-05 03:38:00

CopyableCougar4 (1441 views)
2017-03-24 15:39:42

theagentd (1326 views)
2017-03-24 15:32:08
List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!