Calculating certain points after rotating an image

First of all, sorry! This will probably be a solution with a terrible amount of math in it.

At least you get an image :slight_smile:

I want the projectiles to spawn at the end of the gun-barrel. Each gun has an offset calculated from its attachments (silencer etc.) and the basic gun-length.
How would I go about figuring out the spawnpoint for the projectiles when I rotate the arms?

Note: good time to check out image before continuing

I was thinking I could save a variable with a fixed point in the gun-class (the calculation in bold above) for when it’s pointing directly right, so I know how long the gun is at all times. But with a rotation around an anchor, I can’t just use circle-calculations.

I might also just rotate around the top-left, and somehow calculate the offset between the shoulder-point and the junction-point on the rotated image, but how?

Have any of you guys done this before? Any suggestions? Maybe there’s some other way of doing the rotation of the arms and gun that would work, and still make sure they’re in the right place at his shoulders.

Note: This is using nothing but Java2D. I know. Next project I’ll use LWJGL

http://img577.imageshack.us/img577/2248/renderingman.jpg

I’ve done something like this before, yes. But I just drew a line from the weapon/arm (x / 2,y / 2) to the mouse, and covered it up using the image :wink:
Remember that the x/y never actually changes.

If you’re using LWJGL, you could send the bullet through the same Matrix settings as the rotated arms, then do like the guy above said and draw an actual line in the matrix the entities are in to calculate the actual collision.

There’s probably a better way to do it though, I’m not much of a pro with platform-game technique.

Hi. Thanks for the replies :slight_smile:

Well, I’m not using any libraries. Just Java2D with BufferedImages.

I’ve seen so many games using similar techniques to have rotating arms and the projectiles spawn right at the end of the gun. I’m really stumped on this one. Any suggestions as to what I have to read up on? I’m guessing there’ll be a some vector-calculations, mixed with radians and such, but I just can’t seem to wrap my head around it. How do they do it?

Trigonometry, unit circle, sine, cosine, arcsine, Pythagoras, etc.

You have an offset (vector) from the anchor point to the tip of the weapon. It rotates exactly with the image.

How do you mean? If I draw the weapon and arms on an image for themselves, and I know the exact offset from shoulder-point to projectile-spawn point when it is pointing directly to the right, how would I calculate where the new projectile-spawn point is after using an AffineTransform to rotate it? I don’t understand how that initial projectile-spawn point can be changed to match the new spot it is supposed to be on, after the image has been rotated.

Do I create a vector from shoulder-point to projectile-spawn point, and run that through the same AffineTransform? And how would I do that? I don’t think I can “attach” an AffineTransform to a Vector or Point

You could just define a Vector class like this

public class Vector {
    public final double x,y;
    
    public Vector(double x, double y) {
        this.x = x;
        this.y = y;
    }
    
    public Vector add(Vector v) {
        return new Vector(x+v.x, y+v.y);
    }
    public Vector sub(Vector v) {
        return new Vector(x-v.x, y-v.y);
    }
    public Vector mul(double d) {
        return new Vector(x*d, y*d);
    }
    public Vector rotate(double angle) {
        double sin = Math.sin(angle);
        double cos = Math.cos(angle);
        return new Vector(cos*x - sin*y,
                          sin*x + cos*y);
    }
    public Vector rotateAround(Vector anchorPoint, double angle) {
        return sub(anchorPoint).rotate(angle).add(anchorPoint);
    }
}

And then you can write

Vector anchorPoint = ...
Vector spawnPosition = ...
double angle = ...
Vector newSpawnPosition = spawnPosition.rotateAround(anchorPoint, angle);

[quote=“Ultroman,post:7,topic:39167”]
Actually you can. With AffineTransform.transform
http://docs.oracle.com/javase/6/docs/api/java/awt/geom/AffineTransform.html

Wow…why the hell didn’t I think of that?!

Thanks, Zoidberg!