Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
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  
  Projectile and Mob collision?  (Read 218 times)
0 Members and 1 Guest are viewing this topic.
Offline imber
« Posted 2014-04-01 15:02:58 »

I'm thinking about how to handle collision of projectile from towers to mobs, if I should at all.

What I've observed from other TD games is that every shot hits unless accuracy is implemented or mobs have some ability to avoid them. So far, I'm just using Pythagoras th(without sqrt) to determine the distance between the projectile and the mob but sometimes it will miss if the delta x and y is too fast, or too slow and the tower is far away.

Should I really be checking for collision at all, or should I interpolate the projectile to the next step(s) of the mob's path/mob's coords; or something different?

Thanks!
Offline trollwarrior1
« Reply #1 - Posted 2014-04-01 15:18:32 »

When you create a projectile keep a reference of the entity the projectile is shooting towards. Every update cycle, you need to do something like this.

Here is my rotation helper class for simple 2d rotation operations.
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  
public class Rotation {

   public static int angle(vec2 pivot, vec2 point) {

      float xdiff = pivot.x - point.x;
      float ydiff = pivot.y - point.y;
     
      float angle = (float) ((Math.atan2(xdiff, ydiff)) * 180 / Math.PI);
     
      return -(int)angle;
   }
   
   public static float angle(float x0, float y0, float x1, float y1) {
      return angle(new vec2(x0, y0), new vec2(x1, y1));
   }
   
   public static vec2 point(vec2 pivot, vec2 point, float rotation) {
     
      float rot = (float)(1f / 180 * rotation * Math.PI);
     
      float x = point.x - pivot.x;
      float y = point.y - pivot.y;
     
      float newx = (float)(x * Math.cos(rot) - y * Math.sin(rot));
      float newy = (float)(x * Math.sin(rot) + y * Math.cos(rot));
     
     
      newx += pivot.x;
      newy += pivot.y;
     
      return new vec2(newx, newy);
   }
   
}

And vec2 class
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
public class vec2 {

   public static vec2 NULL = new vec2(0, 0);
   
   
   public float x, y;
   
   public vec2(float x, float y) {
      this.x = x;
      this.y = y;
   }
   
   public int getX() {
      return (int) x;
   }
   
   public int getY() {
      return (int) y;
   }
   
}


Here is a short guide how to do what you want.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
public class Projectile {
   
   private Entity target;
   
   public void update() {
     
      // 1) Calculate rotation between target (entity shooting at) and this (projectile)
     float rotation = Rotation.angle(target.x, target.y, this.x, this.y);
     
      // 2) The speed at which the projectiles travels
     float speed = 2f;
     
      // 3) A 2d vector which will tell how much to increment projectile's x,y coordinates.
     // Rotate vector composed of [0,-speed] around point vec2.NULL( which is [0,0] vector) by rotation degrees.
     vec2 velocity = Rotation.point(vec2.NULL, new vec2(0, -speed), rotation);
     
      // 4) increment projectile's x,y coordinates.
     this.x+=velocity.x;
      this.y+=velocity.y;
   }
   
}
Online atombrot

Senior Member


Medals: 9
Projects: 1



« Reply #2 - Posted 2014-04-01 23:46:19 »

I think this depends entirely on how you want your game to function. It might be a nice tactical gameplay mechanic.

Proper collision detection would be important, if you have very slow projectiles and very fast enemies, but i think for most tower defense games, a simpler approach might work as well. I would simply do 2 checks, before firing.

1. Is the targetted enemy in weapons range?
2. Has the tower an unobstructed view on the target? (it may be that another tower is between the target and the shooting tower).

If one of those two criterias is not met, the tower does not shoot or looks for another target. If those criterias are met, the tower shoots and the projectile hits.
Pages: [1]
  ignore  |  Print  
 
 

 

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 (16 views)
2014-07-30 21:08:39

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

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

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

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

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

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

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

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

Riven (31 views)
2014-07-23 20:56:16
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

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24: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!