Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (581)
games submitted by our members
Games in WIP (500)
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  
  Shooting/Moving Towards coordinates?  (Read 1913 times)
0 Members and 1 Guest are viewing this topic.
Offline Quentin

Senior Newbie




Big newb


« Posted 2012-06-29 13:46:09 »

Hey, I was wondering if anyone could help me with a small problem.

I want to be able to shoot/walk towards a point(in a top-down perspective, as nooby "AI" and to shoot with the mouse), but I'm not sure how to do it.

Could someone please explain it to me? That'd be amazing.

Thanks!
Offline ontaiwolf

Senior Newbie





« Reply #1 - Posted 2012-06-29 14:40:47 »

1  
2  
bullet.setX(bullet.getX() + stepSizePerFrame * Math.cos(Math.toRadians(bullet.getAngle())));
bullet.setY(bullet.getY() + stepSizePerFrame * Math.sin(Math.toRadians(bullet.getAngle())));


This shows you how can you can move a bullet with a fixed angle. Same for your character, you just need a dynamic angle, you can change the angle of the character by rotating him (for example let him look at the mouse pointer) then just calculate the angle. For the angle of a bullet you can take the angle of the character, when you press the shoot button.
Offline 65K
« Reply #2 - Posted 2012-06-29 14:58:44 »

I use Pythagoras:
1  
2  
3  
4  
5  
6  
float dx = targetX - startX;
float dy = targetY - startY;
double h = Math.sqrt(dx * dx + dy * dy);
float dn = (float)(h / sqrt2);
     
Vector bulletHeading = new Vector(dx / dn, dy / dn, 0);

bulletHeading is the ratio between x- and y-movement for the bullet to approach the target from the start position.

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

Senior Newbie




Big newb


« Reply #3 - Posted 2012-06-29 16:49:02 »

I use Pythagoras:
1  
2  
3  
4  
5  
6  
float dx = targetX - startX;
float dy = targetY - startY;
double h = Math.sqrt(dx * dx + dy * dy);
float dn = (float)(h / sqrt2);
     
Vector bulletHeading = new Vector(dx / dn, dy / dn, 0);

bulletHeading is the ratio between x- and y-movement for the bullet to approach the target from the start position.

Thanks, this seems semi-simple, except I have no idea how to use a vector. How would one use a vector for movement?
Offline 65K
« Reply #4 - Posted 2012-06-29 16:55:13 »

You don't need no vector.

Important are the ratios or how its called the vector gets.
For horizonzal movement the x-fraction is +1/-1, y-fraction 0, for vertical movement vice versa.
For diagonal movement you get ~0.7 = sqrt(2) for both.

These values are then used to calculate the x and y values for each game loop turn.

Offline ontaiwolf

Senior Newbie





« Reply #5 - Posted 2012-06-29 18:00:20 »

Or he can use my approach. He just needs to put the code lines to the game update loop to set the position, and then to read it in the rendering loop.

Well, he needs 2 classes. One for the bullet, one for the character. Each of them needs the x and y value to set the position and the angle. The angle of a bullet is fixed, just take the direction of the character when you shoot. The angle of the character is dynamic. I don't know how you want to move the character but that's easy. Just rotate the character by pressing left or right keys or let rotate him by the mouse and calculate the angle. That's it.
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.

xsi3rr4x (57 views)
2014-04-15 18:08:23

BurntPizza (55 views)
2014-04-15 03:46:01

UprightPath (68 views)
2014-04-14 17:39:50

UprightPath (51 views)
2014-04-14 17:35:47

Porlus (68 views)
2014-04-14 15:48:38

tom_mai78101 (93 views)
2014-04-10 04:04:31

BurntPizza (153 views)
2014-04-08 23:06:04

tom_mai78101 (249 views)
2014-04-05 13:34:39

trollwarrior1 (205 views)
2014-04-04 12:06:45

CJLetsGame (213 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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!