Java-Gaming.org Hi !
 Featured games (83) games approved by the League of Dukes Games in Showcase (538) Games in Android Showcase (132) games submitted by our members Games in WIP (600) 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 2508 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.

 Games published by our own members! Check 'em out!
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.

 Add your game by posting it in the WIP section, or publish it in Showcase. The first screenshot will be displayed as a thumbnail.
 rwatson462 (28 views) 2014-12-15 09:26:44 Mr.CodeIt (19 views) 2014-12-14 19:50:38 BurntPizza (35 views) 2014-12-09 22:41:13 BurntPizza (70 views) 2014-12-08 04:46:31 JscottyBieshaar (32 views) 2014-12-05 12:39:02 SHC (44 views) 2014-12-03 16:27:13 CopyableCougar4 (40 views) 2014-11-29 21:32:03 toopeicgaming1999 (108 views) 2014-11-26 15:22:04 toopeicgaming1999 (94 views) 2014-11-26 15:20:36 toopeicgaming1999 (29 views) 2014-11-26 15:20:08
 LiquidNitrogen 33x basil_ 29x HeroesGraveDev 29x Rayvolution 26x BurntPizza 18x appel 17x kevglass 16x Gibbo3771 14x kpars 14x KevinWorkman 13x Riven 13x gouessej 13x NegativeZero 11x princec 11x Spasi 11x Ecumene 11x
 Understanding 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:27Resources for WIP games2014-08-01 16:20:17Resources for WIP games2014-08-01 16:19:50List of Learning Resources2014-07-31 16:29:50List of Learning Resources2014-07-31 16:26:06
 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