Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (582)
Games in Android Showcase (163)
games submitted by our members
Games in WIP (633)
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 at the mouse  (Read 393 times)
0 Members and 1 Guest are viewing this topic.
Offline PocketCrafter7
« Posted 2014-06-23 08:32:35 »

I am making a top down shooter. The player needs to shoot where the mouse is. This is what I got, but it isn't working very well.

double opp = player.gety() - mousePoint.getY();
double adj = player.getx() - mousePoint.getX();
double rad = Math.atan2(opp, adj);

I do realize I am doing something terribly wrong. Any helps ?

I just want to know how to get the angle between the player and the mouse.

Nothing is difficult in this world. It is just how you look at it.
Offline superminer362

Senior Newbie

Medals: 3
Exp: 1 year

« Reply #1 - Posted 2014-06-23 08:48:41 »

Try this:
Math.atan2(opp, -adj);
Offline pjt33

« JGO Spiffy Duke »

Medals: 40
Projects: 4
Exp: 7 years

« Reply #2 - Posted 2014-06-23 08:49:24 »

Why do you have a shootAtRad method? Isn't it just going to do a sin and cos to get back to an (x, y) vector?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline PocketCrafter7
« Reply #3 - Posted 2014-06-23 08:53:27 »

Yes, the bullet takes the rad and then vector x is cos of rad and vector y is sin of rad. Then in the update method, the vectors are added to the position. That is how the bullets move. I didn't understand what you find wrong.

Nothing is difficult in this world. It is just how you look at it.
Offline pjt33

« JGO Spiffy Duke »

Medals: 40
Projects: 4
Exp: 7 years

« Reply #4 - Posted 2014-06-23 09:04:13 »

You have x and y. You convert them to an angle. Then you convert back to x and y. It's a lot simpler to just normalise the vector and not do any trigonometry at all. In fact, most 2D games have little or no need for trig.
Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

Ralphanese (19 views)
2015-05-29 13:07:55

Gibbo3771 (17 views)
2015-05-29 06:54:17

Ralphanese (23 views)
2015-05-28 16:52:52

theagentd (31 views)
2015-05-27 22:21:17

theagentd (37 views)
2015-05-27 22:21:08

CopyableCougar4 (27 views)
2015-05-27 19:24:50

MrMapcom (24 views)
2015-05-23 20:26:16

MrMapcom (33 views)
2015-05-23 20:23:34

Waterwolf (38 views)
2015-05-20 15:01:45

chrislo27 (44 views)
2015-05-20 03:42:21
Intersection Methods
by Roquen
2015-05-29 08:19:33

List of Learning Resources
by SilverTiger
2015-05-05 10:20:32

How to: JGO Wiki
by Mac70
2015-02-17 20:56:16

2D Dynamic Lighting
by ThePixelPony
2015-01-01 20:25:42

How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21

Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02 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‑
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!