Java-Gaming.org Hi !
 Featured games (83) games approved by the League of Dukes Games in Showcase (522) Games in Android Showcase (127) games submitted by our members Games in WIP (591) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Calculating certain points after rotating an image  (Read 3108 times) 0 Members and 1 Guest are viewing this topic.
Ultroman

JGO Knight

Medals: 25
Projects: 1

Snappin' at snizzes since '83

 « Posted 2012-06-27 21:16:46 »

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

At least you get an image

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

- Jonas
Jimmt
« League of Dukes »

JGO Kernel

Medals: 138
Projects: 4
Exp: 3 years

 « Reply #1 - Posted 2012-06-28 01:27:00 »

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
Remember that the x/y never actually changes.
SkyAphid
 « Reply #2 - Posted 2012-06-28 04:19:07 »

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.

“Life is pretty simple: You do some stuff. Most fails. Some works. You do more of what works. If it works big, others quickly copy it. Then you do something else. The trick is the doing something else.” ~Leonardo da Vinci
Ultroman

JGO Knight

Medals: 25
Projects: 1

Snappin' at snizzes since '83

 « Reply #3 - Posted 2012-07-10 05:30:39 »

Hi. Thanks for the replies

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?

- Jonas
65K
 « Reply #4 - Posted 2012-07-10 06:15:39 »

Any suggestions as to what I have to read up on?
Trigonometry, unit circle, sine, cosine, arcsine, Pythagoras, etc.

Roquen
 « Reply #5 - Posted 2012-07-10 06:35:03 »

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

JGO Knight

Medals: 25
Projects: 1

Snappin' at snizzes since '83

 « Reply #6 - Posted 2012-07-10 22:13:02 »

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

- Jonas
DrZoidberg

Senior Devvie

Medals: 17

 « Reply #7 - Posted 2012-07-13 14:42:32 »

You could just define a Vector class like this
 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27 `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
 1  2  3  4 `Vector anchorPoint = ...Vector spawnPosition = ...double angle = ...Vector newSpawnPosition = spawnPosition.rotateAround(anchorPoint, angle);`

I don't think I can "attach" an AffineTransform to a Vector or Point

Actually you can. With AffineTransform.transform
http://docs.oracle.com/javase/6/docs/api/java/awt/geom/AffineTransform.html
Ultroman

JGO Knight

Medals: 25
Projects: 1

Snappin' at snizzes since '83

 « Reply #8 - Posted 2012-07-13 18:34:11 »

Wow...why the hell didn't I think of that?!

Thanks, Zoidberg!

- Jonas
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.
 trollwarrior1 (34 views) 2014-11-22 17:13:56 xFryIx (73 views) 2014-11-13 17:34:49 digdugdiggy (52 views) 2014-11-13 02:11:50 digdugdiggy (46 views) 2014-11-13 02:10:15 digdugdiggy (40 views) 2014-11-13 02:09:33 kovacsa (66 views) 2014-11-08 00:57:14 TehJavaDev (70 views) 2014-11-04 03:04:50 BurntPizza (68 views) 2014-11-03 23:54:52 moogie (83 views) 2014-11-03 11:22:04 CopyableCougar4 (82 views) 2014-11-02 04:36:41
 basil_ 29x theagentd 28x HeroesGraveDev 25x BurntPizza 20x Riven 19x Spasi 19x KevinWorkman 15x princec 14x SHC 12x Gibbo3771 11x gouessej 11x CopyableCougar4 10x kpars 10x LiquidNitrogen 10x Nate 9x Grunnt 9x
 Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-10 03:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-03 03:36:02List of Learning Resources2014-08-16 15:40:00List of Learning Resources2014-08-06 00:33:27Resources for WIP games2014-08-01 21:20:17Resources for WIP games2014-08-01 21:19:50List of Learning Resources2014-07-31 21:29:50List of Learning Resources2014-07-31 21:26:06
 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