Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (121)
games submitted by our members
Games in WIP (577)
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  
  A mathematical question about firing projectiles  (Read 2187 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-08-10 03:40:34 »

Hello!

I've been working very hard at creating a 2D platform side-scroller where the player can shoot. Everything is going well, except I can't figure out an answer to this one problem, and I've been tearing my brain apart for a week now.

The thing is: right now I have an offset for the shoulder of the player, and at the moment the gun is (quite by chance) almost the same Y-position as the shoulder, so my code below works. It basically calculates the angle of the line between the shoulder and the cursor, and adds the distance from the shoulder to the muzzle to get the spawn-point for the projectile. I thought I had made it so the muzzleoffset could be used to move the spawn-point of the bullets in 2D, but all it does is add the distance from the shoulder to the muzzle to the spawn-point. It doesn't actually work as a 2D offset.

What I'd like, is to be able to move the spawn-point of the projectiles upwards or downwards or closer/farther from the player, because I'm going to have many different guns with varying muzzle-offsets. I've tried a good deal of things to move the spawn-point of the bullets, but I'm having a hard time making it "count" when I move it around in different angles. It's easy enough to just add +2 to Y, but if it isn't taken into account in the calculations, it obviously won't work in all angles.



So, what can I do to offset the spawn-point of the projectiles by some simple X,Y values, so the offset will be taken into account when I rotate the arm?

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  
28  
29  
30  
31  
32  
33  
34  
35  
int shoulderOffsetX = roundPlayerPosX, shoulderOffsetY = roundPlayerPosY-40;

// Calculate the radians for the angle the bullets should be rotated.
// Ignore PlayerRenderOffsets, as they are only important for the calculation, because it is the offset of
// where the player is rendered on the screen, and it just indicates a point to which everything is
// translated.
// I know, shoulderOffsetX is the same as roundedPlayerPosX, but the position of the shoulder might change,
// when I get some drawings from the artist.
double baseXcalc = control.getMc().getMouseX() - shoulderOffsetX + roundPlayerPosX - playerRenderOffsetX;
double baseYcalc = control.getMc().getMouseY() - shoulderOffsetY + roundPlayerPosY - playerRenderOffsetY;

double angleInRad = Math.atan2(baseYcalc, baseXcalc);

// The above works perfectly to find the correct angle between the shoulder and the mouse, but I would think
// I need to alter the calculations in some way to achieve what I've stated above, but since the guns all turn
// with the arm around the shoulder-point, I should be able to use this angle regardless, right?

// I don't know how I could've thought the method below would do what I wanted...it must've been late.
// I do understand what I've done, but I also see that it can never work the way I intended, as this just
// finds a distance between the two points and adds it to the projectile position. The +2 and +42 are the values
// I want the projectile offset by, so it would spawn 2 pixels lower than the shoulder, and 42 pixels away from it.

// Calculate the starting-positions of the bullets.
Point shoulder = new Point(shoulderOffsetX, shoulderOffsetY);
Point muzzleOffset = new Point(shoulder.x+42, shoulder.y+2);
       
// Find the distance from the shoulder to the muzzle;
double distanceFromShoulderToMuzzle = shoulder.distance(muzzleOffset);
       
// Calculate new start-positions for the bullets
double bulletX = Math.cos(angleInRad)*distanceFromShoulderToMuzzle + shoulderOffsetX;
double bulletY = Math.sin(angleInRad)*distanceFromShoulderToMuzzle + shoulderOffsetY;
     
// Calculate the angle the bullets should be traveling at; is also used to rotate the projectile images
double angle = (double) angleInRad*180/Math.PI;

- Jonas
Offline Roquen
« Reply #1 - Posted 2012-08-10 03:55:02 »

You simply need a reference offset from the shoulder to the end of the muzzle per weapon.  Rotate that offset by the angle of the shoulder and add the world position of the shoulder. done.
Offline Ultroman

JGO Knight


Medals: 25
Projects: 1


Snappin' at snizzes since '83


« Reply #2 - Posted 2012-08-10 05:08:43 »

You simply need a reference offset from the shoulder to the end of the muzzle per weapon.  Rotate that offset by the angle of the shoulder and add the world position of the shoulder. done.

Sounds easy, and I did have an idea that it would be something like that, but I can't figure out how to rotate a point or 2 integer-offsets. This doesn't work (where the weaponOffsets are 2 and 42, referring to the example above):
1  
2  
double bulletX = Math.cos(angleInRad)*weaponOffsetX + shoulderOffsetX;
double bulletY = Math.sin(angleInRad)*weaponOffsetY + shoulderOffsetY;


If I'm to do this, what would I use as the origin? The shoulder?

- Jonas
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline StumpyStrust
« Reply #3 - Posted 2012-08-10 06:35:47 »

I am by no means very good at any math stuff but I would make it so each weapon has its own spawn point. Then you adjust that spawn point for rotating around the character.

Example.

Weapon is a shot gun that is 64*16 spawn point would be the x cord of the weapon plus n pixels. The high n is, the further away the spawn point is. Then when ever you rotate the gun, you also adjust the spawn.

This I think would be more object oriented as each weapon keeps track of its own spawn point for projectiles.

Offline jmart

Junior Duke


Medals: 1



« Reply #4 - Posted 2012-08-10 17:16:45 »

I do not know enough about Java2D to help but this is very easy to do in JavaFX.

Just move the Pane that the Projectiles will be in to the spawn point (e.g. projectilePane.translateX(spanwPoint.getX()); projectilePane.translateY(spawnPoint.getY())Wink, then just shoot your projectile in the angle that it has to go. 

BTW, when I shoot projectiles I just have them go straight up then apply a rotation at pivot 0,0 (which is your spawn point after translating the projectilePane) to the desired angle.  This removes the trig operation that is normally done.


thanks
jose
Offline Ultroman

JGO Knight


Medals: 25
Projects: 1


Snappin' at snizzes since '83


« Reply #5 - Posted 2012-08-10 17:41:21 »

I must've been really tired. Note to self: do not post mathematical questions at 5:30 in the morning ^^
Roquen is right.

I need for each weapon to be able to calculate its own spawnpoint, as they can get upgrades which alter the spawnpoint. What I'm gonna do, is rotate a muzzleoffset X,Y around 0,0 using my shooting-angle. Then I can have an offset for each weapon (with the shoulderpoint as origin), and then add the results to the shoulderpoint. It's so simple. Will post code when I've made it work.

- Jonas
Offline Ultroman

JGO Knight


Medals: 25
Projects: 1


Snappin' at snizzes since '83


« Reply #6 - Posted 2012-08-11 00:07:59 »

This is what I ended up with. I have a shoulderPoint/rotationPoint. Then I have a muzzleOffsetPoint. I have the angle from the shoulderPoint to the mouse as angle, because the arm and attached gun rotate around the shoulder.

double translatedMuzzleOffsetX = (muzzleOffsetX*Math.cos(angleInRad))-(muzzleOffsetY*Math.sin(angleInRad));
double translatedMuzzleOffsetY = (muzzleOffsetX*Math.sin(angleInRad))+(muzzleOffsetY*Math.cos(angleInRad));

Factoring in the size of the bullets afterwards proved a terrible curse, but I got it done.

- Jonas
Offline DrZoidberg

Senior Duke


Medals: 15



« Reply #7 - Posted 2012-08-12 12:40:41 »

What's wrong with using a vector class as I suggested in your other thread.
http://www.java-gaming.org/topics/calculating-certain-points-after-rotating-an-image/26744/msg/237480/view.html#msg237480
Offline Ultroman

JGO Knight


Medals: 25
Projects: 1


Snappin' at snizzes since '83


« Reply #8 - Posted 2012-08-12 16:55:39 »

Nothing at all. I should. And I will for the next project. I've simply done too much on this project to change it all at this point. I really like the idea that the vector can rotate itself, and even around an anchorpoint. It makes a lot of things easier.
For this project, though, I'm done with the calculations, now that this is done Smiley

- Jonas
Offline Ultroman

JGO Knight


Medals: 25
Projects: 1


Snappin' at snizzes since '83


« Reply #9 - Posted 2012-08-21 17:08:09 »

This is what I ended up with. I have a shoulderPoint/rotationPoint. Then I have a muzzleOffsetPoint. I have the angle from the shoulderPoint to the mouse as angle, because the arm and attached gun rotate around the shoulder.

double translatedMuzzleOffsetX = (muzzleOffsetX*Math.cos(angleInRad))-(muzzleOffsetY*Math.sin(angleInRad));
double translatedMuzzleOffsetY = (muzzleOffsetX*Math.sin(angleInRad))+(muzzleOffsetY*Math.cos(angleInRad));

Factoring in the size of the bullets afterwards proved a terrible curse, but I got it done.
Well, it turns out I didn't get it done. It only works with one size of bullets. When I use one that is another width and height, I get displacement errors.

My bullets always point to the right initially, and are positioned at the shoulder. Then retrieve the muzzleOffset from where they would go if the guy is pointing his gun straight to the right, and translate it using the functions in the quote above. I've tried numerous ways to go about this, and none seem to work all the way around. I've even tried to have different projectile-height and -width offsets for each quadrant, but nothing translates to more than one size of bullet.

Question: It should be possible, using the functions in the quote, to translate the projectiles correctly all the way around the character, without doing different calculations for each quadrant, right?

I would think it should be done by placing the projectile correctly according to when he's pointing the gun directly to the right, subtract half the height of the projectile from Y, and then translate the derived point using the angle. But it doesn't seem to work. Can any math-savvy person confirm this is the way to go?

- Jonas
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 #10 - Posted 2012-08-22 01:59:50 »

Nevermind...I made the stupid error of not displacing my rendering of projectile images around their center, which means the positions have always been correct, but the rendering of the images was wrong.

- Jonas
Offline Damocles
« Reply #11 - Posted 2012-08-22 06:27:41 »

I would program it very simple: spawn the bullet at the shoulder, then move it a certain distance (invisible, no collisions detection)
until it reaches the position of the muzzle.

Then the bullet continues normally.

Offline Roquen
« Reply #12 - Posted 2012-08-22 09:49:47 »

My thinking here is that starting to understand how coordinate frames work 'tie' things together is a good thing in the long run.
Offline Ultroman

JGO Knight


Medals: 25
Projects: 1


Snappin' at snizzes since '83


« Reply #13 - Posted 2012-08-22 12:14:20 »

Yeah, I think so to, Roquen. At least now I'll never make that mistake again when making projectile-code.
Thanks for the suggestion, Damocles, but I feel it would be an unnecessarily weird workaround to a simple problem.

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

theagentd (20 views)
2014-10-25 15:46:29

Longarmx (53 views)
2014-10-17 03:59:02

Norakomi (47 views)
2014-10-16 15:22:06

Norakomi (35 views)
2014-10-16 15:20:20

lcass (39 views)
2014-10-15 16:18:58

TehJavaDev (70 views)
2014-10-14 00:39:48

TehJavaDev (69 views)
2014-10-14 00:35:47

TehJavaDev (61 views)
2014-10-14 00:32:37

BurntPizza (74 views)
2014-10-11 23:24:42

BurntPizza (47 views)
2014-10-11 23:10:45
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!