Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (495)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
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  
  'Follow' problem.  (Read 992 times)
0 Members and 1 Guest are viewing this topic.
Offline Bloodraid

Junior Newbie





« Posted 2010-02-10 18:15:49 »

What a great first post, eh?

Well, I started working on a simple top-down shooter yesterday (Using the Slick library). I've made the enemies follow the player, but it's somewhat buggy.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
   @Override
   public void update(GameContainer gc, int delta) {
      float dx = ((Engine.getPlayer().getLocation().x + 10) - this.location.x);
      float dy = ((Engine.getPlayer().getLocation().y + 10) - this.location.y);
      this.rotation = (float) Math.toDegrees(Math.atan2(dy, dx) + 89.65f);
     
      final float SPEED = 1.0f;
           
      this.location.x += Math.cos(this.rotation) * SPEED;
      this.location.y += Math.sin(this.rotation) * SPEED;
   }

   @Override
   public void render(GameContainer gc, Graphics g) {
      sprite.setRotation(Math.round(this.rotation));
      sprite.draw(this.location.x - (sprite.getWidth() / 2), this.location.y - (sprite.getHeight() / 2));
   }


It works fine when the player isn't moving, but when the player starts moving, the enemy starts doing all of these little circles (not rotation, but location) until the Player stops moving.

Anyone know what the issue might be?
Offline zoto

Senior Member


Medals: 4



« Reply #1 - Posted 2010-02-10 19:27:35 »

Changing the 3rd line of update to:
1  
this.rotation = (float) Math.toDegrees(Math.atan2(dy, dx)) + 89.65f;

should fix the problem.

I would recomend adding a member variable like:
1  
final static float ROTATION_OFFSET = Math.toRadians(89.65f);

and change the 3rd line of the update function to:
1  
this.rotation = (float) (Math.atan2(dy, dx) + ROTATION_OFFSET);

Always keeping the rotation variable in radians will help a lot with avoiding errors like this. If you ever want the rotation in degrees, to show the player or something, just convert it on the fly.
Offline dishmoth
« Reply #2 - Posted 2010-02-10 19:31:40 »

What zoto said, and also make sure that the angle you're applying cos and sin to is in radians.
Simon

Quote from: Bloodraid
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
        public void update(GameContainer gc, int delta) {
      float dx = ((Engine.getPlayer().getLocation().x + 10) - this.location.x);
      float dy = ((Engine.getPlayer().getLocation().y + 10) - this.location.y);
      float radians = Math.atan2(dy, dx);
      this.rotation = (float) Math.toDegrees(radians); // + 90.0f; ???
     
      final float SPEED = 1.0f;
           
      this.location.x += Math.cos(radians) * SPEED;
      this.location.y += Math.sin(radians) * SPEED;
   }


Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Bloodraid

Junior Newbie





« Reply #3 - Posted 2010-02-10 20:48:23 »

Changing the 3rd line of update to:
1  
this.rotation = (float) Math.toDegrees(Math.atan2(dy, dx)) + 89.65f;

should fix the problem.

I would recomend adding a member variable like:
1  
final static float ROTATION_OFFSET = Math.toRadians(89.65f);

and change the 3rd line of the update function to:
1  
this.rotation = (float) (Math.atan2(dy, dx) + ROTATION_OFFSET);

Always keeping the rotation variable in radians will help a lot with avoiding errors like this. If you ever want the rotation in degrees, to show the player or something, just convert it on the fly.

The problem isn't the rotation (Although I did change my method to fit yours a bit more). It faces the player just fine.

The problem is when the player is moving, the enemy will stop moving towards the enemy, and instead do these weird little circles, causing him to be nearly stationary.

What zoto said, and also make sure that the angle you're applying cos and sin to is in radians.

I noticed when you quoted me, you had commented out the rotation offset.

The reason I've got that is because my sprite wouldn't face the right direction (he'd face 90 degrees counterclockwise of the direction he's supposed to face.

Edit: I fixed it, I figured out what my problem was. Thanks to the two of your for your help. Much appreciated.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

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

The first screenshot will be displayed as a thumbnail.

Dwinin (28 views)
2014-09-12 09:08:26

Norakomi (57 views)
2014-09-10 13:57:51

TehJavaDev (74 views)
2014-09-10 06:39:09

Tekkerue (38 views)
2014-09-09 02:24:56

mitcheeb (57 views)
2014-09-08 06:06:29

BurntPizza (45 views)
2014-09-07 01:13:42

Longarmx (28 views)
2014-09-07 01:12:14

Longarmx (34 views)
2014-09-07 01:11:22

Longarmx (34 views)
2014-09-07 01:10:19

mitcheeb (40 views)
2014-09-04 23:08:59
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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
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!