Java-Gaming.org Hi !
 Featured games (90) games approved by the League of Dukes Games in Showcase (753) Games in Android Showcase (228) games submitted by our members Games in WIP (842) 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 4956 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.

Lethal Running - a RPG about a deadly game show held in a futuristic dysoptian society.
 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.

Lethal Running - a RPG about a deadly game show held in a futuristic dysoptian society.
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

 nelsongames (17 views) 2018-04-24 18:15:36 nelsongames (13 views) 2018-04-24 18:14:32 ivj94 (587 views) 2018-03-24 14:47:39 ivj94 (50 views) 2018-03-24 14:46:31 ivj94 (400 views) 2018-03-24 14:43:53 Solater (65 views) 2018-03-17 05:04:08 nelsongames (110 views) 2018-03-05 17:56:34 Gornova (175 views) 2018-03-02 22:15:33 buddyBro (729 views) 2018-02-28 16:59:18 buddyBro (93 views) 2018-02-28 16:45:17
 Java Gaming Resourcesby philfrei2017-12-05 19:38:37Java Gaming Resourcesby philfrei2017-12-05 19:37:39Java Gaming Resourcesby philfrei2017-12-05 19:36:10Java Gaming Resourcesby philfrei2017-12-05 19:33:10List of Learning Resourcesby elect2017-03-13 14:05:44List of Learning Resourcesby elect2017-03-13 14:04:45SF/X Librariesby philfrei2017-03-02 08:45:19SF/X Librariesby philfrei2017-03-02 08:44:05
 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