Java-Gaming.org Hi !
 Featured games (90) games approved by the League of Dukes Games in Showcase (775) Games in Android Showcase (230) games submitted by our members Games in WIP (856) 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 5434 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 dystopian society.
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 dystopian 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

 hadezbladez (25 views) 2018-11-16 13:46:03 hadezbladez (33 views) 2018-11-16 13:41:33 hadezbladez (16 views) 2018-11-16 13:35:35 hadezbladez (12 views) 2018-11-16 13:32:03 EgonOlsen (1890 views) 2018-06-10 19:43:48 EgonOlsen (1912 views) 2018-06-10 19:43:44 EgonOlsen (1267 views) 2018-06-10 19:43:20 DesertCoockie (1711 views) 2018-05-13 18:23:11 nelsongames (1398 views) 2018-04-24 18:15:36 nelsongames (2012 views) 2018-04-24 18:14:32
 Deployment and Packagingby mudlee2018-08-22 18:09:50Java Gaming Resourcesby gouessej2018-08-22 08:19:41Deployment and Packagingby gouessej2018-08-22 08:04:08Deployment and Packagingby gouessej2018-08-22 08:03:45Deployment and Packagingby philfrei2018-08-20 02:33:38Deployment and Packagingby philfrei2018-08-20 02:29:55Deployment and Packagingby philfrei2018-08-19 23:56:20Deployment and Packagingby philfrei2018-08-19 23:54:46
 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