Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (495)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
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  
  Checking collisions on hex  (Read 528 times)
0 Members and 1 Guest are viewing this topic.
Offline re4der

Innocent Bystander





« Posted 2013-03-17 16:15:45 »

Salve!
I've recently been working on java game with my friend. Last few days we've spent trying to make a formula checking collisions. Prototype in java 2D worked well, but after porting it to our game we ended with hex-throwing simulator. We're unable to find a miastake. Mouse input is surely correct.
Code is:
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  
package Objects.Ships;

import Objects.Entity;
import Objects.Ships.Parts.Functionals.Functional;
import Objects.Ships.Parts.Hex;
import java.util.ArrayList;

public abstract class Ship extends Entity {
   
    public Ship(float x, float y, float scaleX, float scaleY, float rotate, int xHexes, int yHexes) {
        this.x = x;
        this.y = y;
        this.scaleX = scaleX;
        this.scaleY = scaleY;
        this.rotate = rotate;
        this.xHexes = xHexes;
        this.yHexes = yHexes;
        hex = new Hex[xHexes][yHexes];
        setCenters();
    }
   
    protected void setCenters() {
        float xHex = xHexes;
        float yHex = yHexes;
        centerX = scaleX*(float)Math.floor(yHex/2f)*2;
        centerY = scaleY*(float)Math.floor(xHex/2f)*4f/3f;
    }
   
    public Hex[][] hex;
    protected ArrayList<Functional> parts;
    public float speedX=0;
    public float speedY=0;
    public float centerX;
    public float centerY;
    public int xHexes;
    public int yHexes;
   
}

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  
package Interface.GameInterface.DesignStudio;

import Core.Graphics.SL;
import Core.Graphics.TL;
import Core.Input.In;
import Core.Switch;
import Lib.Graphics.GCore;
import Objects.Ships.Parts.StructuralHex;
import Objects.Ships.PlayerShip;
import Objects.Ships.Ship;
import org.lwjgl.opengl.Display;

public class ModifyStructuralHex {
   
    public void add(Ship ship){
       
        int x = In.mouse.x+(int)GCore.renderer.getCameraX()-Display.getWidth()/2;
        int y = In.mouse.y+(int)GCore.renderer.getCameraY()-Display.getHeight()/2;
       
        for(int xCount = 0; xCount<ship.xHexes; xCount++){
            for (int yCount = 1; yCount<ship.yHexes; yCount++) {                
                if(ship.hex[xCount][yCount].checkCollisions(x, y))
                    ship.hex[xCount][yCount] = new StructuralHex(ship, xCount, yCount, 10);
                Switch.blankHexCursor = false;
            }
        }    
       
       
       
    }
}

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  
45  
package Objects.Ships.Parts;

import Core.Input.In;
import Objects.Ships.PlayerShip;
import Objects.Ships.Ship;

public abstract class Hex {
   
    protected int posX;
    protected int posY;
    protected Ship father;
   
    public Hex(Ship father, int posX, int posY) {
        this.father = father;
        this.posX = posX;
        this.posY = posY;
    }
   
    public abstract void update();
   
    public boolean checkCollisions(int inputX, int inputY){
        boolean collision = true;
       
        int sX = (int)(((posY * father.scaleY*2) * Math.cos(father.rotate))-((posX * father.scaleX*4f/3f) * Math.sin(father.rotate)));
        int sY = (int)(((posY * father.scaleY*2) * Math.sin(father.rotate))+((posX * father.scaleX*4f/3f) * Math.cos(father.rotate)));
        for (int i=0; i<3; i++) {
            int x1 = (int)(sX + father.scaleX * Math.cos(i * 2 * Math.PI / 6 + father.rotate)-father.centerX);
            int y1 = (int)(sY + father.scaleY * Math.sin(i * 2 * Math.PI / 6 + father.rotate)-father.centerY);
            int x2 = (int)(sX + father.scaleX * Math.cos((i+1) * 2 * Math.PI / 6 + father.rotate)-father.centerX);
            int y2 = (int)(sY + father.scaleY * Math.sin((i+1) * 2 * Math.PI / 6 + father.rotate)-father.centerY);
            int x3 = (int)(sX + father.scaleX * Math.cos((i+3) * 2 * Math.PI / 6 + father.rotate)-father.centerX);
            int y3 = (int)(sY + father.scaleY * Math.sin((i+3) * 2 * Math.PI / 6 + father.rotate)-father.centerY);
            int x4 = (int)(sX + father.scaleX * Math.cos((i+4) * 2 * Math.PI / 6 + father.rotate)-father.centerX);
            int y4 = (int)(sY + father.scaleY * Math.sin((i+4) * 2 * Math.PI / 6 + father.rotate)-father.centerY);

            int calc1 = (x2-x1)*(inputY-y1)-(y2-y1)*(inputX-x1);
            int calc2 = (x4-x3)*(inputY-y3)-(y4-y3)*(inputX-x3);
            if (calc1*calc2<0) {
                collision=false;
            }
        }
        return collision;
    }
   
}

And there's how we're filling ship array with hexs:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
public class ShipUtils {

        public static void collisionCheckFill(Ship ship){
            for(int xCount = 0; xCount<ship.xHexes; xCount++){
                for (int yCount = 0; yCount<ship.yHexes; yCount++) {
                    ship.hex[xCount][yCount] = new CollisionHex(ship, xCount, yCount);
                }
            }

            ship.hex[11][7] = new StructuralHex(ship, 11, 7, 10);
           
        }
       
}
Offline jonjava
« Reply #1 - Posted 2013-03-18 05:27:51 »

Math problem :V

http://www.playchilla.com/how-to-check-if-a-point-is-inside-a-hexagon

Offline NegativeZero

JGO Knight


Medals: 30
Projects: 2
Exp: 3 years


Zero but not.


« Reply #2 - Posted 2013-03-18 08:27:58 »

Off-topic:

miastake

I really like this.
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.

Dwinin (28 views)
2014-09-12 09:08:26

Norakomi (57 views)
2014-09-10 13:57:51

TehJavaDev (74 views)
2014-09-10 06:39:09

Tekkerue (37 views)
2014-09-09 02:24:56

mitcheeb (57 views)
2014-09-08 06:06:29

BurntPizza (45 views)
2014-09-07 01:13:42

Longarmx (28 views)
2014-09-07 01:12:14

Longarmx (34 views)
2014-09-07 01:11:22

Longarmx (34 views)
2014-09-07 01:10:19

mitcheeb (40 views)
2014-09-04 23:08:59
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

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

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!