Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (109)
games submitted by our members
Games in WIP (536)
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  
  Particle X:Y Spawn Coords Based on Rotation  (Read 1608 times)
0 Members and 1 Guest are viewing this topic.
Offline Slyth2727
« Posted 2013-03-02 00:24:12 »

Alright, I got a good one for you guys here. I have worked on this for a while to no avail so I thought some more math savvy people on here could point me in the right direction. So I've got a missile for my TD game that, when shot, rotates and follows it's target like a real missile. Now I want to use my particle system to spawn particles in at the base of the rocket so it looks like a nice smoke trail. I already have all the particle and color fade coded, but I have no clue how to get the X and Y to pass it to spawn based on the rotation of the missile. Here is the code for it:

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  
36  
37  
38  
39  
40  
41  
42  
43  
44  
   public void move(Graphics g) {
      if (inGame) {
         if ((xBullet >= targetX - 4 && xBullet <= targetX + 4) && (yBullet <= targetY + 4 && yBullet >= targetY - 4)) {
            inGame = false;
            Screen.currLevel.mobs[shotMob].looseHealth(damage);
            new Explosion((int) xBullet - 26, (int) yBullet - 12);
            xBullet = 900;
            yBullet = 900;
         }
         targetX = Screen.currLevel.mobs[shotMob].x + Screen.currLevel.mobs[shotMob].width / 4;
         targetY = Screen.currLevel.mobs[shotMob].y + Screen.currLevel.mobs[shotMob].width / 4;
         distanceX = (int) ((double) targetX - xBullet);
         distanceY = (int) ((double) targetY - yBullet);
         sep = Math.sqrt((distanceX * distanceX) + (distanceY * distanceY));
         scale = 3;
         xBullet += (distanceX / sep) * scale;
         yBullet += (distanceY / sep) * scale;
         slope = (targetY - yBullet) / (targetX - xBullet);
         damage = tower.damage;

         Graphics2D g2d = (Graphics2D) g.create();
         double correction = 270;
         float rotation;
         rotation = (float) (Math.atan2(yBullet - targetY, xBullet - targetX) * (180 / Math.PI));
         Screen.room.block[y][x].drawImage = false;
         g2d.setTransform(AffineTransform.getRotateInstance(Math.toRadians(rotation + correction), xBullet + Screen.room.blockSize / 2, yBullet + Screen.room.blockSize / 2));
         g2d.drawImage(Screen.tileset_air[Value.missile], (int) xBullet, (int) yBullet, Screen.room.blockSize, Screen.room.blockSize, null);
         Screen.addParticle(XCOORDINATE, YCOORDINATE, .2, 4, 80, 100, false, Color.orange, Color.BLACK, 20);
         g2d.dispose();
      }
   }

   public void draw(Graphics g) {
      if (!Screen.currLevel.mobs[shotMob].isDead() && Screen.inGame) {
         if (timeF >= timeS) {
            move(g);
            timeF = 0;
         } else {
            timeF++;
         }
      } else {
         // inGame = false;
     }
   }

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Offline jonjava
« Reply #1 - Posted 2013-03-02 01:27:25 »

Basically we need a bit of trigonometry

http://mathworld.wolfram.com/images/eps-gif/Trigonometry_1001.gif

Where the distance should be the distance from the missiles rotation point to the point you want the missile to appear.



Something like that.

Offline relminator
« Reply #2 - Posted 2013-03-02 01:45:39 »

You could use atan2(diry,dirx) to get the angle.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Slyth2727
« Reply #3 - Posted 2013-03-02 02:20:32 »

what would be dirx and diry? I mean I know it is short for direction but I am not sure which variables you mean by it.

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Offline jonjava
« Reply #4 - Posted 2013-03-02 02:23:06 »

1  
rotation = (float) (Math.atan2(yBullet - targetY, xBullet - targetX) * (180 / Math.PI));

Offline jonjava
« Reply #5 - Posted 2013-03-02 02:29:56 »

dirx would actually be delta x and diry you delta y or dx and dry. Whatever you call them it should be the the distance between point A and point B in the X lane and the distance between point A and point B in the Y lane. Because we can imagine that there is a 90 degree triangle between the two points.

Basic trigonometry for 90 degree angled triangles.


Offline Slyth2727
« Reply #6 - Posted 2013-03-02 02:43:37 »

Ah ok I understand.. I have already done this to get the angle, look up in the code I posted. For your suggestion on the spawn position for the particles, I have tried and failed to use it many different ways. For some reason none of them seem to work.

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Offline Slyth2727
« Reply #7 - Posted 2013-03-02 20:58:57 »

Aaannd I slept on it and figured it out the next day (with the help you guys gave me Wink ). So for anyone viewing this thread in the future, what I realized I had to do was plug in half the width and height of the image where missile_x and missile_y was so the final equations were these:
 
1  
2  
double particleX = (xBullet + Screen.room.blockSize / 2) - Math.toRadians(Math.cos((rotation) + correction)) * (Screen.room.blockSize / 2);
double particleY = (yBullet + Screen.room.blockSize / 2) - Math.toRadians(Math.sin((rotation) + correction)) * (Screen.room.blockSize / 2);

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
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.

CogWheelz (18 views)
2014-07-30 21:08:39

Riven (27 views)
2014-07-29 18:09:19

Riven (16 views)
2014-07-29 18:08:52

Dwinin (14 views)
2014-07-29 10:59:34

E.R. Fleming (35 views)
2014-07-29 03:07:13

E.R. Fleming (13 views)
2014-07-29 03:06:25

pw (44 views)
2014-07-24 01:59:36

Riven (46 views)
2014-07-23 21:16:32

Riven (30 views)
2014-07-23 21:07:15

Riven (32 views)
2014-07-23 20:56:16
Resources for WIP games
by CogWheelz
2014-08-01 18:20:17

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

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

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

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

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22
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!