Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (563) Games in Android Showcase (151) games submitted by our members Games in WIP (604) 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 3322 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: 158
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.

 wxwsk8er (48 views) 2015-03-20 15:39:46 Fairy Tailz (42 views) 2015-03-15 21:52:20 Olo (26 views) 2015-03-13 17:51:59 Olo (27 views) 2015-03-13 17:50:51 Olo (33 views) 2015-03-13 17:50:16 Olo (40 views) 2015-03-13 17:47:07 ClaasJG (29 views) 2015-03-10 11:36:42 ClaasJG (36 views) 2015-03-10 11:33:01 Pippogeek (45 views) 2015-03-05 14:36:23 Pippogeek (37 views) 2015-03-05 13:56:12
 KevinWorkman 23x LiquidNitrogen 23x BurntPizza 22x basil_ 21x Riven 20x theagentd 19x EgonOlsen 17x Roquen 14x princec 12x Slyth2727 9x NegativeZero 9x Ecumene 9x SHC 9x Varkas 9x lcass 7x ClaasJG 7x
 How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27
 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