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 { } } |