Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (753)
Games in Android Showcase (228)
games submitted by our members
Games in WIP (842)
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  
  Custom Collisions  (Read 629 times)
0 Members and 1 Guest are viewing this topic.
Offline SonicPixelation

Senior Newbie





« Posted 2013-09-17 21:32:07 »

for the longest time I have been using just rectangles to handle collision, and I was happy with that but I wanted to branch out in to something more complex with polygons using slick2d as the base system.

after some working with it I realized how easy is was compared to what I was doing before, but I ran into a problem.  What I want is the ability to slide up slopes with little to no customization of the engine to handle certain shapes.

this is the basis of what I have so far

Entity.class
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  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
package Pheniox.Entity;

import java.util.ArrayList;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.geom.Shape;
import org.newdawn.slick.geom.Vector2f;
import org.newdawn.slick.state.StateBasedGame;

import Pheniox.World.World;

public abstract class Entity {
   protected Vector2f position = new Vector2f(0,0);
   protected Vector2f velocity = new Vector2f(0,0);
   protected World world;
   public boolean removed = false;
   private Object entities;
   
   //collision booleans
   protected boolean collideTop,collideLeft, collideBottom,collideRight;
   
   //constructors

   public Entity(float x, float y){
      position.x = x;
      position.y = y;
   }
   
   public void init(World world){
      this.world = world;
      init();
   }
   //removes the player from the world
   public void remove(){
      removed = true;
   }
   
   //Movement methods
   public void move(Vector2f vel, int delta){
      move(vel.x, vel.y, delta);
   }
   //takes 2 floats and moves
   public void move(float xa, float ya, int delta){
      if(xa != 0 && ya != 0){
         move(xa, 0,delta);
         move(0,ya,delta);
         return;
      }
     
      boolean canPass = true;
     
      collideTop = false;
      collideLeft = false;
      collideBottom = false;
      collideRight = false;
     
     
      ArrayList<Entity> entities = world.getEntities();
     
      for(Entity e: entities){
         if(e != this){
            if(this.getCollisionBox(xa * delta, ya * delta).intersects(e.getCollisionBox(e.velocity.x * delta,e.velocity.y * delta))){
               canPass = false;
           
         
      }
      if(xa > 0 && !canPass){
         collideLeft = true;
      }
      if(xa < 0 && !canPass){
         collideRight = true;
      }
      if(ya > 0 && !canPass){
         collideBottom = true;
      }
      if(ya < 0 && !canPass){
         collideTop = true;
      }
   }
}
     
     
     
      if(canPass){
      this.position.x += delta * xa;
      this.position.y += delta * ya;
      }
     
   }
   //render methods
   public void render(GameContainer gc, Graphics g){
      renderEntity(gc,g);
   }
   //update methods
   public void update(GameContainer gc, StateBasedGame sbg , int delta){
      move(velocity.x,velocity.y,delta);
      updateEntity(gc,sbg,delta);
   }
   
   //abstract classes
   public abstract void init();
   public abstract void renderEntity(GameContainer gc,Graphics g);
   public abstract void updateEntity(GameContainer gc, StateBasedGame sbg, int delta);
   public abstract Shape getCollisionBox(float xa, float ya);
}


the collision box shape is declared in "getCollisionBox(float xa, float ya)"
when being extends by other classes

basicaly what I want is say in a 2d platformer the ability to go up slopes, and I want it to just inheret to the entity class and not have certain cases that make it happen.

any help would be appreciated.

Offline Opiop
« Reply #1 - Posted 2013-09-17 21:41:36 »

Ok first off I'd recommend not creating that list of entities every single time you call the move method. Its going to create unnecessary slow downs since you have to create the object every single time.

For your slope problem, I'd recommend just getting the theta of the slope using the cosine if its a right triangle, and then using that angle and the opposite side of the triangle to see how much you need to rotate the entity for it to look realistic. Now, I've never actually tried this so i have no idea if that would work.

For none right triangle slopes, I have no idea but maybe you should use pixel perfect collision. Maybe it would work.
Pages: [1]
  ignore  |  Print  
 
 

 
ivj94 (579 views)
2018-03-24 14:47:39

ivj94 (42 views)
2018-03-24 14:46:31

ivj94 (346 views)
2018-03-24 14:43:53

Solater (59 views)
2018-03-17 05:04:08

nelsongames (104 views)
2018-03-05 17:56:34

Gornova (122 views)
2018-03-02 22:15:33

buddyBro (665 views)
2018-02-28 16:59:18

buddyBro (89 views)
2018-02-28 16:45:17

xxMrPHDxx (491 views)
2017-12-31 17:17:51

xxMrPHDxx (727 views)
2017-12-31 17:15:51
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05
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!