Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (575) Games in Android Showcase (154) games submitted by our members Games in WIP (623) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 [SOLVED] Calculating angle to mouse  (Read 3557 times) 0 Members and 1 Guest are viewing this topic.
DDDBOMBER

Junior Devvie

Projects: 1

 « Posted 2012-10-30 14:47:01 »

I need help calculating the angle to the mouse from entities,
 1  2  3  4  5  6  7  8  9 `if(orders.get(0) instanceof MoveOrder){              MoveOrder mo = (MoveOrder)orders.get(0);              double xd = mo.x - x;              double yd = mo.y - y;              double rotation = Math.toDegrees(Math.atan2(yd,xd))+270;              rot = rotation;              System.out.println(rot);              orders.remove(0);           }`

This is my current code, but it doesn't seem to work, and the movement code is:
 1  2  3  4  5 `while(rot > 360)rot-= 360;      while(rot < 0)rot+= 360;      double movementAngle = Math.toRadians(rot);        x += Math.sin(movementAngle) * 0.5;        y += Math.cos(movementAngle) * 0.5;`

can anyone help?
deepthought
 « Reply #1 - Posted 2012-10-30 17:19:21 »

everything you have looks correct to me. I assume it's messing up at the stage of getting the angle from the mouse, right? make sure you're getting the right positions in your MoveOrder and entity position.

jocks rule the highschools. GEEKS RULE THE WORLD MWAHAHAHA!!
captain failure test game
h3ckboy

JGO Coder

Medals: 5

 « Reply #2 - Posted 2012-10-30 18:02:44 »

Can you be more specific in "doesn't work"?
kinaite

Junior Devvie

Projects: 3

 « Reply #3 - Posted 2012-10-30 18:26:20 »

this should help:
angle = Math.toDegrees(Math.atan2(MouseY - entity.y, MouseX - entity.x));
DDDBOMBER

Junior Devvie

Projects: 1

 « Reply #4 - Posted 2012-10-30 18:29:54 »

It seems to go in completely random directions, and doesn't move to the mouse at all...
kinaite

Junior Devvie

Projects: 3

 « Reply #5 - Posted 2012-10-30 18:34:31 »

to move: (Worked in my game. see Walking sprites in show case)
deepthought
 « Reply #6 - Posted 2012-10-30 18:43:29 »

you need to find out where you are going wrong. are you getting the correct angle from your first segment?

jocks rule the highschools. GEEKS RULE THE WORLD MWAHAHAHA!!
captain failure test game
DDDBOMBER

Junior Devvie

Projects: 1

 « Reply #7 - Posted 2012-10-30 20:07:51 »

Okay, i've done some testing, and it seems to work fine x Axis, but messes up somewhere on the y Axis, as in, if i tell it to go from (0,0) to (10,0) it works fine, if i want to move just in the x Axis it has to be
 1  2  3  4 `MoveOrder mo = (MoveOrder)orders.get(0);              double xd = mo.x - x;              double yd = mo.y - y;              double rotation = Math.toDegrees(Math.atan2(yd,xd))+90;`

However, if i want to move in the y Axis it has to be
 1  2  3  4 `MoveOrder mo = (MoveOrder)orders.get(0);              double xd = mo.x - x;              double yd = mo.y - y;              double rotation = Math.toDegrees(Math.atan2(yd,xd))+270;`

Which even further confuses me...

I've got it to move in the right direction by doing it like this
 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15 `double movementAngleX = Math.toRadians(rot);      double movementAngleY = Math.toRadians(rot+180);        x += Math.sin(movementAngleX) * 0.5;        y += Math.cos(movementAngleY) * 0.5;        if(orders.size() > 0){           if(orders.get(0) instanceof MoveOrder){              MoveOrder mo = (MoveOrder)orders.get(0);              double xd = mo.x - x;              double yd = mo.y - y;              double rotation = Math.toDegrees(Math.atan2(yd,xd))+90;              rot = rotation;              System.out.println(rot);              orders.remove(0);           }        }`

But then the rotation on the image is messed up, the rendering code is (im using LWJGL)
 1  2  3  4  5  6  7  8  9 `                GL11.glTranslated(x+32, y+32, 0);        GL11.glRotated(rot, 0, 0, -1);        GL11.glBegin(GL11.GL_QUADS);        GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex2f(-32, -32);        GL11.glTexCoord2f(1.0f, 0.0f); GL11.glVertex2f(32, -32);        GL11.glTexCoord2f(1.0f, 1.0f); GL11.glVertex2f(32, 32);        GL11.glTexCoord2f(0.0f, 1.0f); GL11.glVertex2f(-32, 32);         GL11.glEnd();`

Anyone got any ideas?
kinaite

Junior Devvie

Projects: 3

 « Reply #8 - Posted 2012-10-30 20:29:01 »

this is y coord code:

and try GL11.Rotatef with

angle = angle % 360.0f;
GL11.Rotatef((float)angle...)
DDDBOMBER

Junior Devvie

Projects: 1

 « Reply #9 - Posted 2012-10-30 20:37:33 »

Thanks! Got it to work,
 1  2  3  4  5  6  7  8  9  10  11  12  13 `double movementAngle = Math.toRadians(rot);        x += Math.sin(movementAngle) * 0.5;        y -= Math.cos(movementAngle) * 0.5;        if(orders.size() > 0){           if(orders.get(0) instanceof MoveOrder){              MoveOrder mo = (MoveOrder)orders.get(0);              double xd = mo.x - x;              double yd = mo.y - y;              double rotation = Math.toDegrees(Math.atan2(yd,xd))+90;              rot = rotation;              orders.remove(0);           }        }`

 1  2 `        float angle = (float) (rot % 360.0f);        GL11.glRotated(angle+180.0f, 0, 0, 1);`
theagentd

« JGO Bitwise Duke »

Medals: 432
Projects: 2
Exp: 8 years

 « Reply #10 - Posted 2012-10-31 00:50:39 »

 1  2 `        float angle = (float) (rot % 360.0f);        GL11.glRotated(angle+180.0f, 0, 0, 1);`

glRotate() can handle angles outside 360 degrees, but you should keep the actual stored rotation (rot) to 0-360 degrees.

Myomyomyo.
Pages: [1]
 ignore  |  Print

You cannot reply to this message, because it is very, very old.

 BurntPizza (29 views) 2015-04-23 03:42:11 theagentd (35 views) 2015-04-22 16:23:07 Riven (48 views) 2015-04-16 10:48:47 Duke0200 (59 views) 2015-04-16 01:59:01 Fairy Tailz (41 views) 2015-04-14 20:13:12 Riven (43 views) 2015-04-12 21:36:37 bus hotdog (60 views) 2015-04-10 02:39:32 CopyableCougar4 (66 views) 2015-04-10 00:51:04 BurntPizza (69 views) 2015-04-06 22:06:58 ags1 (67 views) 2015-04-02 10:58:48
 theagentd 23x BurntPizza 16x wessles 15x 65K 11x kingroka123 11x alwex 11x Rayvolution 8x kevglass 8x Roquen 7x chrislo27 7x Hanksha 7x Riven 7x Olo 7x Ecumene 7x ra4king 7x KevinWorkman 6x
 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