Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (565) Games in Android Showcase (151) games submitted by our members Games in WIP (607) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Shooting/Moving Towards coordinates?  (Read 2756 times) 0 Members and 1 Guest are viewing this topic.
Quentin

Senior Newbie

Big newb

 « Posted 2012-06-29 11: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!
ontaiwolf

Senior Newbie

 « Reply #1 - Posted 2012-06-29 12: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.
65K
 « Reply #2 - Posted 2012-06-29 12: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.

Quentin

Senior Newbie

Big newb

 « Reply #3 - Posted 2012-06-29 14: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?
65K
 « Reply #4 - Posted 2012-06-29 14: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.

ontaiwolf

Senior Newbie

 « Reply #5 - Posted 2012-06-29 16: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.

 Riven (9 views) 2015-04-01 18:27:05 ags1 (25 views) 2015-03-31 10:55:12 theagentd (13 views) 2015-03-27 23:08:20 wxwsk8er (54 views) 2015-03-20 15:39:46 Fairy Tailz (48 views) 2015-03-15 21:52:20 Olo (29 views) 2015-03-13 17:51:59 Olo (33 views) 2015-03-13 17:50:51 Olo (40 views) 2015-03-13 17:50:16 Olo (44 views) 2015-03-13 17:47:07 ClaasJG (61 views) 2015-03-10 11:36:42
 BurntPizza 21x LiquidNitrogen 21x KevinWorkman 18x EgonOlsen 17x theagentd 16x basil_ 16x Roquen 15x Varkas 12x wessles 11x 65K 11x Riven 9x Rayvolution 9x phu004 8x SHC 8x princec 8x Ashedragon 8x
 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