Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (552)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Calculating certain points after rotating an image  (Read 2939 times)
0 Members and 1 Guest are viewing this topic.
Offline 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 Smiley

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
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 128
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 Wink
Remember that the x/y never actually changes.
Offline 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
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline 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  Smiley

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
Offline 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.

Offline 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.
Offline 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
Offline DrZoidberg

Senior Member


Medals: 15



« 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
Offline 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.

CopyableCougar4 (23 views)
2014-08-22 19:31:30

atombrot (34 views)
2014-08-19 09:29:53

Tekkerue (30 views)
2014-08-16 06:45:27

Tekkerue (28 views)
2014-08-16 06:22:17

Tekkerue (18 views)
2014-08-16 06:20:21

Tekkerue (27 views)
2014-08-16 06:12:11

Rayexar (65 views)
2014-08-11 02:49:23

BurntPizza (41 views)
2014-08-09 21:09:32

BurntPizza (31 views)
2014-08-08 02:01:56

Norakomi (41 views)
2014-08-06 19:49:38
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!