Slick Forums

Discuss the Slick 2D Library
It is currently Fri May 24, 2019 6:46 am

All times are UTC




Post new topic Reply to topic  [ 7 posts ] 
Author Message
PostPosted: Sat May 22, 2010 11:33 am 
Offline

Joined: Sat May 22, 2010 11:26 am
Posts: 2
Hello,

just discovered Slick a few days ago and I'm trying to make a little game. I don't know Java very much - I'm using C++ primarily.

Anyway, my problem's not really Java or Slick, but Math. We did not have vectors and matrices and things like that (gonna do it next year), which is why I need your help. I want to rotate an image while using the mouth. So, for example, If I move the mouse to the upper right of the window, I want the image to look at the upper right. If I move the mouse at position (0, 0), I want the image to look at (0, 0). The image always stays at the bottom middle, if that helps.

I already use the search of this site, but it didn't help me, because I just found rotation with keyboard.

Greetings,
Luinechor


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 22, 2010 12:10 pm 
Offline
Regular
User avatar

Joined: Thu May 21, 2009 10:25 am
Posts: 161
Location: France
I have an old example code about this, here we go:

To make it works you need an image with the face turn north (assume that the start angle = 0)

Code:
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;

public class Test extends BasicGame {

   public static void main(String[] args) {
      try {
         AppGameContainer container = new AppGameContainer(new Test());
         container.setDisplayMode(800, 600, false);
         container.setTargetFrameRate(100);
         container.start();
      } catch (SlickException e) {
         e.printStackTrace();
      }
   }

   private int x;
   private int y;
   private Image image;

   public Test() {
      super("test");
   }

   @Override
   public void init(GameContainer container) throws SlickException {
      image = new Image("image.png");
      x = container.getWidth() / 2;
      y = container.getHeight() / 2;
   }

   @Override
   public void update(GameContainer container, int delta) throws SlickException {

   }

   public void render(GameContainer container, Graphics g) throws SlickException {
      float targetAng = (float) getTargetAngle(x, y, container.getInput().getMouseX(), container.getInput().getMouseY());
      int tx = (image.getWidth() / 2);
      int ty = (image.getHeight() / 2);
      g.rotate(x + tx, y + ty, targetAng);
      g.drawImage(image, x, y);
      g.rotate(x + tx, y + ty, -targetAng);
   }

   // Maths

   public double getDistanceBetween(float startX, float startY, float endX, float endY) {
      return Math.sqrt((Math.pow((endX - startX), 2)) + (Math.pow((endY - startY), 2)));
   }

   public double getTargetAngle(float startX, float startY, float targetX, float targetY) {
      double dist = getDistanceBetween(startX, startY, targetX, targetY);
      double sinNewAng = (startY - targetY) / dist;
      double cosNewAng = (targetX - startX) / dist;
      double angle = 0;

      if (sinNewAng > 0) {
         if (cosNewAng > 0) {
            angle = 90 - Math.toDegrees(Math.asin(sinNewAng));
         } else {
            angle = Math.toDegrees(Math.asin(sinNewAng)) + 270;
         }
      } else {
         angle = Math.toDegrees(Math.acos(cosNewAng)) + 90;
      }
      return angle;
   }

}


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 22, 2010 12:50 pm 
Offline
Slick Zombie

Joined: Fri Jan 29, 2010 7:02 pm
Posts: 1242
using Math.pow to square a number is a bad idea. Use this to compute the distance between 2 points:
Code:
public float getDistanceBetween(float startX, float startY, float endX, float endY) {
   float dx = endX - startX;
   float dy = endY - startY;
   return (float)Math.sqrt(dx*dx + dy*dy);
}


And to compute the angle between 2 points use Math.atan2 like this:
Code:
public float getTargetAngle(float startX, float startY, float targetX, float targetY) {
   float dx = targetX - startX;
   float dy = targetY - startY;
   return (float)Math.toDegrees(Math.atan2(dy, dx));
}

_________________
TWL - The Themable Widget Library


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 22, 2010 1:08 pm 
Offline
Regular
User avatar

Joined: Thu May 21, 2009 10:25 am
Posts: 161
Location: France
Cool I'm not really good in maths (I do what I can to make things works). I will update my maths game code with your code.

Thanks Matthias :D

Just one thing, why using pow is a bad idea ? (just want to know why, question of performance I presume ?)


Last edited by Vince on Sat May 22, 2010 1:18 pm, edited 3 times in total.

Top
 Profile  
 
 Post subject:
PostPosted: Sat May 22, 2010 1:09 pm 
Offline

Joined: Sat May 22, 2010 11:26 am
Posts: 2
Thanks for those really fast answers.
Works great. :)


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 22, 2010 1:24 pm 
Offline
Slick Zombie

Joined: Fri Jan 29, 2010 7:02 pm
Posts: 1242
Math.pow is an expensive operation. If your exponent is a constant 2,3 or 4 then use multiplications.

_________________
TWL - The Themable Widget Library


Top
 Profile  
 
PostPosted: Fri Dec 21, 2018 1:14 am 
Offline
User avatar

Joined: Mon Oct 01, 2018 4:01 pm
Posts: 1
Location: Russia
hello from 2018, hehe, this question is still actual ;)
p.s. btw, it's really helped, thanks!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group