Java-Gaming.org Java4K winners: [ by our judges | by the community ]         
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  'Follow' problem.  (Read 614 times)
0 Members and 1 Guest are viewing this topic.
Offline Bloodraid

JGO n00b
*

Posts: 4



« on: 2010-02-10 13: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

Full Member
**

Posts: 234
Medals: 7



« Reply #1 on: 2010-02-10 14: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

JGO Ninja
***

Posts: 517
Medals: 19



« Reply #2 on: 2010-02-10 14: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! Go get 'em!
Offline Bloodraid

JGO n00b
*

Posts: 4



« Reply #3 on: 2010-02-10 15: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]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.219 seconds with 20 queries.