1. Member
Join Date
Dec 2011
Posts
4
Rep Power
0

## 2 ploygons colliding

Hi there!

I'm recently new to java and i have two questions for u guys.

I'm creating a game in which we have one polygon (our 'ship') and two monsters. Both of the monsters and the ship are triangles like my code show down below
Java Code:
```    void buildmonster(){
m_monster = new Polygon();
m_monster.addPoint(10,0);        /* fecha o ciclo */
m_monster_angle = 0;
spawnarmonster();

m_monster_speed = 0;

/**
* constroi o norris
*/
void buildnorris(){
m_norris = new Polygon();
m_norris_angle = 0;
spawnarnorris();

m_norris_speed = 0;

m_norris_max_forward_speed = param_norris_max_forward_speed;
m_norris_max_back_speed = param_norris_max_back_speed;
m_norris_acceleration_speed =param_norris_acceleration_speed;
m_norris_turn_speed = param_norris_turn_speed;

}```
First i would like to ask how can i put the monsters moving randomly through the screen (There are no borders, example, if one monster hits the top it appears on the bot going top).
Last i would like to know what's the way to detect the collision between the ship and the monster and
• if the ship hits the monster back, destroys them
• if not it destroys the ship (in my game loses 1 up)

Thanks
Last edited by Norm; 12-22-2011 at 06:52 PM. Reason: Moved out of Applets

2. Member
Join Date
Oct 2011
Posts
92
Rep Power
0

## Re: 2 polygons colliding

... Riiiight. Well it seems to be that you'll need to be comparing co-ordinates. If you thread this, and have one thread analysing the co-ordinates of the different objects, or for every movement, you can have each object announce it's co-ordinates and if there are any similarities then a collision is detected. If you want to know which side, then you'll have to split the object up into different sections that are governed differently. you could say, if the ship is hit by a monster then boolean shipHit = true, and if at one point, their co-ordinates are different, then at another point, within a time frame, the co-ordinates are the same or similar, then shipHitBack = true. Then at the end of all of it, a little if statement calling the "destroy()" method. Hope this helps.

3. Member
Join Date
Dec 2011
Posts
4
Rep Power
0

## Re: 2 ploygons colliding

Any about the monster random movement? I've my ship moving with this code
Java Code:
```void handlenorrisMotion(){
double sin = -Math.sin(m_norris_angle);
double cos = Math.cos(m_norris_angle);
/* move x */
m_norris_x += cos * m_norris_speed;
/* wrap x  */
if(m_norris_x > m_width)
m_norris_x = 0;
if(m_norris_x < 0)
m_norris_x = m_width;
/* move y */
m_norris_y += sin * m_norris_speed;
/* wrap y  */
if(m_norris_y > m_height)
m_norris_y = 0;
if(m_norris_y < 0)
m_norris_y = m_height;

}```

4. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
15

## Re: 2 ploygons colliding

what's the way to detect the collision between the ship and the monster
Perhaps one of the contains() methods could help. More reliable would be to create Area instances from them. Area has intersect() and isEmpty() methods.

Or, (and this could be faster) Line2D has a intersectsLine() method which tests for line segment intersection. Two triangles intersect iff no pair of sides intersect and no vertex of one is within the other. (I think)

how can i put the monsters moving randomly through the screen
Paint them onto the screen with the appropriate Graphics2D method.

Define "randomly". Most likely you want their movement to be continuous, so they should have a velocity which lets you determine their position from one instant to the next. There is rather more freedom with respect to how their velocity changes. Keep it simple to start with, you can always tweak it afterwards if it doesn't look realistic. As regards the "no borders" business (it sounds impressive to describe it as a "toroidal topology": in effect the world is shaped like a torus), the % operator is your friend.

5. ## Re: 2 ploygons colliding

Doing efficient collision detection also depends on the number of objects. If you only ever have 20 - 50 or so objects on the screen which you are checking, then a simple brute force might work just fine. However, if you intend to vomit particles all over the place like some of the old school shoot 'em ups, you'll need something better than a bunch of nested for-loops. Quad trees are often used for this, but this is a more advanced topic. I advise you keep your particle count low while you're learning this.

6. Member
Join Date
Dec 2011
Posts
4
Rep Power
0

## Re: 2 ploygons colliding

Hm and how do i create Separate areas from a triangle?
I need to create 2 separate cases
• If my ship hits the monster back it destroys the ship
• else it turns otherway

And i thought i could create 2 areas, one the side that causes destruction , the other its the rest of the triangle.

Can u sample me some code for this?

7. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
15

## Re: 2 ploygons colliding

Hm and how do i create Separate areas from a triangle?
Area has a constructor that takes any shape (including a polygon). You can test two polygons for intersection by forming their intersection and checking to see if it is empty.

Java Code:
```Polygon monster;
Polygon norris;

// later
Area mArea = new Area(monster);
Area nArea = new Area(norris);
boolean hit = mArea.intersect(nArea).isEmpty();```

8. Member
Join Date
Dec 2011
Posts
4
Rep Power
0

## Re: 2 ploygons colliding

Its something like that, but i want to check a collision from behind the monster and that includes all points

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•