Efekt rotacji obiektu względem położenia(X,Y) myszki

0

Hej, słuchajcie, kto mi mógłby trochę czasu poświęcić i oświecić mnie jak zrobić efekt ustawiania kąta obrazka względem pozycji kursora? W sensie mam napisać grę okienkową, chciałbym by na dole był obraz działa, i ten obraz żeby ustawiał się górą(lufą) w kierunku celownika. Póki co to mam załadowany obrazek i mniej więcej pojęcie jak funkcja rotate działa, ale dalej to czarna magia...

0

wygoogluj funkcję atan2 (nie wiem jak w Javie się ją wywołuje, ale pewnie podobnie jak w reszcie języków)

0

Hm. Sory że tak na bieżąco, ale po prostu się pozastanawiam i rezygnuje z jednego pomysłu na rzecz innego co chwila...

0

Kurwsza mać.. Ja już nie wiem co źle robię, czy to aż tak trudne zadanie by stworzyć rotację obiektu, tak żeby wskazywał na pozycję myszki?

Zobaczcie, ja już ocipiałem:

Sekcja rysowania:

protected void paintComponent(Graphics gs)
    {
    	poblicz = new oblicz();
        Graphics2D g=(Graphics2D)gs,g1=(Graphics2D)gs,g3=(Graphics2D)gs;
        //Ustaw tryb lepszej jakości grafiki (wygładzanie/antyaliasing)
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        // Narysuj tło
        g.drawImage(param.bgImage, 0, 0, null);
        g.drawImage(param.bgImage,0,0, new ImageObserver() 
        {
        	public boolean imageUpdate(Image img,int infoflags, int x, int y, int width, int height) 
        	{
        		return true; 
        	}
        } );
        
        int pozx=425,pozy=575;
        double kat = poblicz.kat(katx,katy,pozx,pozy);
    	double starykat=kat;
        if(kat != starykat)
        {
        	kat = poblicz.kat(katx,katy,pozx,pozy);
        }
        //w klasie param jest cannon zapisany(w sensie ta armata co ma się obracać tam gdzie jest myszka) jako BufferedImage.
        g1.drawImage(pcannon.rotate(param.cannon, kat,null), pozx, pozy, new ImageObserver() 
        {
        	public boolean imageUpdate(Image img,int infoflags, int x, int y, int width, int height) 
        	{
        		return true; 
        	}
        } );

        System.out.println(kat);
 
    }
	

Dalej, algorytm do liczenia kąta:

package pakiet;

public class oblicz
{
	 public double kat(double posx, double posy, double imgposx, double imgposy) 
	    {
	    		double Ax= imgposx;
	    		double Ay= imgposy;
	    		double Bx=posx;
	    		double By=posy;
				double Cx = 1024;
				double Cy = 768;
	    		if(posx<imgposx)
	    		{
	    			Cx = 1024;
	    			Cy = 768;
	    		}
	    		else
	    		{
	    			Cx=0;
	    			Cy=0;
	    		}
	    		double AB2=Math.pow(Ax-Bx,2)+Math.pow(Ay-By,2),AB=Math.sqrt(AB2);
	    		double BC2=Math.pow(Bx-Cx,2)+Math.pow(By-Cy,2),BC=Math.sqrt(BC2);
	    		@SuppressWarnings("unused")
				double AC2=Math.pow(Ax-Cx,2)+Math.pow(Ay-Cy,2),AC=Math.sqrt(AC2);
	    		double alfa=Math.atan((AB2+BC2-AC2)/(2*AB*BC))*360/Math.PI;
	    		if(posx<1024/2)
	    			alfa=-alfa;
	    		return alfa;
	    }

}

Dalej, klasa obracająca obrazek:

package pakiet;
import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.lang.Math;


public class cannon
{		
		public static BufferedImage rotate(BufferedImage inputImage, double angle, Color backgroundColor) 
		{
			  Rectangle outputImageBounds = calculateBoundsOfRotatedImage(inputImage, angle);
			   
			  AffineTransform transform = new AffineTransform();
			  transform.translate(-outputImageBounds.x, -outputImageBounds.y);
			  transform.rotate(Math.toRadians(angle));
			  BufferedImage outputImage = new BufferedImage(outputImageBounds.width, outputImageBounds.height, BufferedImage.TYPE_INT_RGB);
			  Graphics2D g = outputImage.createGraphics();
			  g.setColor(null);
			  g.fillRect(0, 0, outputImageBounds.width, outputImageBounds.height);
			  g.drawImage(inputImage, transform, null);
			  g.dispose();
			   
			  return outputImage;
		}
			 
		private static Rectangle calculateBoundsOfRotatedImage(BufferedImage image, double angle) 
		{
			  AffineTransform transform = new AffineTransform();
			  transform.rotate(Math.toRadians(angle));
			  return transform.createTransformedShape(new Rectangle(image.getWidth(), image.getHeight())).getBounds();
		}
	
		
}

Próbowałem też z konwersją z SWT na AWT żeby najpierw obrócić w bufferedImage a później za pomocą drawImage po prostu narysować bez białego tła, ale to jakieś jehowe, same błędy wyskakują...

package pakiet;

import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.DirectColorModel;
import java.awt.image.IndexColorModel;
import java.awt.image.WritableRaster;
import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.graphics.*;

public class convert
{
	static BufferedImage convertToAWT(ImageData data) 
	{
	    ColorModel colorModel = null;
	    PaletteData palette = data.palette;
	    if (palette.isDirect) {
	        BufferedImage bufferedImage = new BufferedImage(data.width,
	                data.height, BufferedImage.TYPE_INT_ARGB);
	        for (int y = 0; y < data.height; y++) {
	            for (int x = 0; x < data.width; x++) {
	                int pixel = data.getPixel(x, y);
	                RGB rgb = palette.getRGB(pixel);
	                bufferedImage.setRGB(x, y, data.getAlpha(x, y) << 24
	                        | rgb.red << 16 | rgb.green << 8 | rgb.blue);
	            }
	        }
	        return bufferedImage;
	    } 
	    else 
	    {
	        RGB[] rgbs = palette.getRGBs();
	        byte[] red = new byte[rgbs.length];
	        byte[] green = new byte[rgbs.length];
	        byte[] blue = new byte[rgbs.length];
	        for (int i = 0; i < rgbs.length; i++) 
	        {
	            RGB rgb = rgbs[i];
	            red[i] = (byte) rgb.red;
	            green[i] = (byte) rgb.green;
	            blue[i] = (byte) rgb.blue;
	        }
	        if (data.transparentPixel != -1) 
	        {
	            colorModel = new IndexColorModel(data.depth, rgbs.length, red,
	            green, blue, data.transparentPixel);
	        } 
	        else 
	        {
	            colorModel = new IndexColorModel(data.depth, rgbs.length, red, green, blue);
	        }
	        BufferedImage bufferedImage = new BufferedImage(colorModel, colorModel.createCompatibleWritableRaster(data.width, data.height), false, null);
	        WritableRaster raster = bufferedImage.getRaster();
	        int[] pixelArray = new int[1];
	        for (int y = 0; y < data.height; y++) 
	        {
	            for (int x = 0; x < data.width; x++) 
	            {
	                int pixel = data.getPixel(x, y);
	                pixelArray[0] = pixel;
	                raster.setPixel(x, y, pixelArray);
	            }
	        }
	        return bufferedImage;
	    }
	}
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1