Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (540)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (603)
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  
  Character movement with Mouse  (Read 1586 times)
0 Members and 1 Guest are viewing this topic.
Offline vyh

Senior Newbie





« Posted 2011-07-23 21:22:39 »

Hello,
Sorry for newbie question but How to movement Game Character with Mouse?
Something like I am have: mouseX, mouseY, playerX = 0, playerY = 0.
If I am click mouse get cordinates in mouseX and mouseY(Something like now mouseX = 5 and mouseY = 10) and move Character to x 1 and y 3(playerX = 1 and playerY = 3 now)
How to move Game character(wihout Teleport) to mouseX and mouseY cordinates?

Simple code snippets very useful.

Thanks and sorry for my English language.
Offline Karmington

Senior Devvie


Medals: 1
Projects: 1


Co-op Freak


« Reply #1 - Posted 2011-07-23 21:36:53 »

movement over time: http://en.wikipedia.org/wiki/Linear_interpolation

The simplest case, two known points. A good exercise to code yourself, though a googlefu may find it readyrolled.

Offline Dx4

Junior Devvie


Medals: 5



« Reply #2 - Posted 2011-07-24 07:40:32 »

first you need to find the angle

1  
double angle = Math.atan2(mouseY-playerY,mouseX-playerX);


then you need to build a vector to calculate the movement threshold

1  
2  
double movex = Math.cos(angle);
double movey = Math.sin(angle);


and now to move towards the point

1  
2  
3  
4  
5  
x += movex;
y += movey;
if (x == mouseX && y == mouseY) { // you can use epsilon here
 stop();
}


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

« JGO Bitwise Duke »


Medals: 366
Projects: 2
Exp: 8 years



« Reply #3 - Posted 2011-07-24 12:20:01 »

and now to move towards the point

1  
2  
3  
4  
5  
x += movex;
y += movey;
if (x == mouseX && y == mouseY) { // you can use epsilon here
 stop();
}


hth
That is NOT going to work. In 99.999999999999% of all cases it will just overshoot and continue forever. You would need to change the if-statement to detect overshooting and then set the position to (mouseX, mouseY) to actually land on the mouse position.

Myomyomyo.
Offline Dx4

Junior Devvie


Medals: 5



« Reply #4 - Posted 2011-07-24 13:03:04 »

and now to move towards the point

1  
2  
3  
4  
5  
x += movex;
y += movey;
if (x == mouseX && y == mouseY) { // you can use epsilon here
 stop();
}


hth
That is NOT going to work. In 99.999999999999% of all cases it will just overshoot and continue forever. You would need to change the if-statement to detect overshooting and then set the position to (mouseX, mouseY) to actually land on the mouse position.

thats why I also write use an epsilon here in the comment, its up to OP to implement an epsilon though.

- David
Offline h3ckboy

JGO Coder


Medals: 5



« Reply #5 - Posted 2011-07-24 13:56:52 »

just quick question, is epsilon sort of like margin of error? so it can be off slightly of the exact point, and doesnt skip it?
Offline dishmoth
« Reply #6 - Posted 2011-07-24 16:27:20 »

thats why I also write use an epsilon here in the comment, its up to OP to implement an epsilon though.

I think theagentd's point is that there's no guarantee that N steps, for any integer N, will land the player on the target position, either exactly or within the epsilon margin-of-error.  You'd need to choose the step length (as well as the step direction) carefully to achieve that.  Or (as Karmington said) interpolate between the original position and the target position.  Or do something like the following, which moves the player towards the target at a fixed speed.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
static final double stepSize = 5.0;  // the player's speed (how far to move each update)

double playerX, playerY; // current position
double targetX, targetY; // where the mouse click happened

// move the player one step closer to the target position
void updatePosition() {
  double dx = targetX - playerX,
         dy = targetY - playerY;
  double distance = Math.hypot(dx,dy);
  if ( distance > stepSize ) {
    // still got a way to go, so take a full step
    playerX += stepSize*dx/distance;
    playerY += stepSize*dy/distance;
  } else {
    // we're either at the target already, or within one step of it
    playerX = targetX;
    playerY = targetY;
  }
}


Simon

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Mr.CodeIt (22 views)
2014-12-23 03:34:11

rwatson462 (53 views)
2014-12-15 09:26:44

Mr.CodeIt (45 views)
2014-12-14 19:50:38

BurntPizza (83 views)
2014-12-09 22:41:13

BurntPizza (110 views)
2014-12-08 04:46:31

JscottyBieshaar (78 views)
2014-12-05 12:39:02

SHC (89 views)
2014-12-03 16:27:13

CopyableCougar4 (94 views)
2014-11-29 21:32:03

toopeicgaming1999 (155 views)
2014-11-26 15:22:04

toopeicgaming1999 (151 views)
2014-11-26 15:20:36
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
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!