Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (407)
games submitted by our members
Games in WIP (293)
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  
  [SOLVED] Calculating angle to mouse  (Read 2193 times)
0 Members and 1 Guest are viewing this topic.
Offline DDDBOMBER

Junior Member


Projects: 1



« Posted 2012-10-30 15: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?
Offline deepthought
« Reply #1 - Posted 2012-10-30 18: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
Offline h3ckboy
« Reply #2 - Posted 2012-10-30 19:02:44 »

Can you be more specific in "doesn't work"?
Games published by our own members! Check 'em out!
Play the free demo of Revenge of the Titans!
Offline kinaite

Senior Newbie


Projects: 1



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

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

Junior Member


Projects: 1



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

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

Senior Newbie


Projects: 1



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

to move: (Worked in my game. see Walking sprites in show case)
       x += delta * Math.sin(Math.toRadians(entity.angle));
       y += delta * Math.cos(Math.toRadians(entity.angle));
Offline deepthought
« Reply #6 - Posted 2012-10-30 19: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
Offline DDDBOMBER

Junior Member


Projects: 1



« Reply #7 - Posted 2012-10-30 21: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?
Offline kinaite

Senior Newbie


Projects: 1



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

i have made a mistake
this is y coord code:
y -= delta * Math.cos(Math.toRadians(entity.angle));

and try GL11.Rotatef with

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

Junior Member


Projects: 1



« Reply #9 - Posted 2012-10-30 21: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);
Games published by our own members! Check 'em out!
Play the free demo of Revenge of the Titans!
Offline theagentd
« Reply #10 - Posted 2012-10-31 01: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.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Browse for soundtracks for your game!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (88 views)
2013-05-17 21:29:12

alaslipknot (96 views)
2013-05-16 21:24:48

gouessej (128 views)
2013-05-16 00:53:38

gouessej (123 views)
2013-05-16 00:17:58

theagentd (131 views)
2013-05-15 15:01:13

theagentd (119 views)
2013-05-15 15:00:54

StreetDoggy (161 views)
2013-05-14 15:56:26

kutucuk (184 views)
2013-05-12 17:10:36

kutucuk (185 views)
2013-05-12 15:36:09

UnluckyDevil (191 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.212 seconds with 21 queries.