Collision Advice

Hello, I have been following ‘pancakes something’ voxel engine tutorial. And need some help with the collision. I have attempted to write a collision system, but can’t go from Chunk->World coordinates and/or AABB coordinates/checking.

If you could offer some help that would be great.

It would help if you posted some code, preferably in the form of an MCVE.

I believe you mean pancakesimone. (He made a youtube series a while back on voxel engines before for anyone who’s wondering…)

Here’s some good articles on what you’re looking for: http://www.miguelcasillas.com/?mcportfolio=collision-detection-c
As for “Chunk -> World Coordinates”, why can’t you? Is it that you can’t figure out the math? We need some details.

Is this the link that you have followed? https://www.youtube.com/playlist?list=PLXa65xzz2vplye7cn1HH4GyE6_FjnDSug

If that is the playlist, he is @opiop65 in this forum. Along with that, you need to give us some more details on what your problem actually is. In my opinion you have to separate the collision system from your chunks. Start by creating a AABB class and attaching an AABB for each chunk.

Sorry for the mistakes. I have a little code, but the problem is getting what chunk the player is in. What I am trying to do is to get what chunk the player is in, and use the AABB(s) for that chunk and check against player’s AABB.

Like I said I can’t find what chunk the player is in, the size of chunks is 16x16x16. Here’s my AABB code:

package io.github.SolidStudiosTeam.Flicker.engine.physics;

public class AABB
{
  private float epsilon = 0.0F;
  public float x0;
  public float y0;
  public float z0;
  public float x1;
  public float y1;
  public float z1;

  public AABB(float x0, float y0, float z0, float x1, float y1, float z1)
  {
    this.x0 = x0;
    this.y0 = y0;
    this.z0 = z0;
    this.x1 = x1;
    this.y1 = y1;
    this.z1 = z1;
  }

  public AABB expand(float xa, float ya, float za)
  {
    float _x0 = this.x0;
    float _y0 = this.y0;
    float _z0 = this.z0;
    float _x1 = this.x1;
    float _y1 = this.y1;
    float _z1 = this.z1;

    if (xa < 0.0F) _x0 += xa;
    if (xa > 0.0F) _x1 += xa;

    if (ya < 0.0F) _y0 += ya;
    if (ya > 0.0F) _y1 += ya;

    if (za < 0.0F) _z0 += za;
    if (za > 0.0F) _z1 += za;

    return new AABB(_x0, _y0, _z0, _x1, _y1, _z1);
  }

  public AABB grow(float xa, float ya, float za)
  {
    float _x0 = this.x0 - xa;
    float _y0 = this.y0 - ya;
    float _z0 = this.z0 - za;
    float _x1 = this.x1 + xa;
    float _y1 = this.y1 + ya;
    float _z1 = this.z1 + za;

    return new AABB(_x0, _y0, _z0, _x1, _y1, _z1);
  }

  public float clipXCollide(AABB c, float xa)
  {
    if ((c.y1 <= this.y0) || (c.y0 >= this.y1)) return xa;
    if ((c.z1 <= this.z0) || (c.z0 >= this.z1)) return xa;

    if ((xa > 0.0F) && (c.x1 <= this.x0))
    {
      float max = this.x0 - c.x1 - this.epsilon;
      if (max < xa) xa = max;
    }
    if ((xa < 0.0F) && (c.x0 >= this.x1))
    {
      float max = this.x1 - c.x0 + this.epsilon;
      if (max > xa) xa = max;
    }

    return xa;
  }

  public float clipYCollide(AABB c, float ya)
  {
    if ((c.x1 <= this.x0) || (c.x0 >= this.x1)) return ya;
    if ((c.z1 <= this.z0) || (c.z0 >= this.z1)) return ya;

    if ((ya > 0.0F) && (c.y1 <= this.y0))
    {
      float max = this.y0 - c.y1 - this.epsilon;
      if (max < ya) ya = max;
    }
    if ((ya < 0.0F) && (c.y0 >= this.y1))
    {
      float max = this.y1 - c.y0 + this.epsilon;
      if (max > ya) ya = max;
    }

    return ya;
  }

  public float clipZCollide(AABB c, float za)
  {
    if ((c.x1 <= this.x0) || (c.x0 >= this.x1)) return za;
    if ((c.y1 <= this.y0) || (c.y0 >= this.y1)) return za;

    if ((za > 0.0F) && (c.z1 <= this.z0))
    {
      float max = this.z0 - c.z1 - this.epsilon;
      if (max < za) za = max;
    }
    if ((za < 0.0F) && (c.z0 >= this.z1))
    {
      float max = this.z1 - c.z0 + this.epsilon;
      if (max > za) za = max;
    }

    return za;
  }

  public boolean intersects(AABB c)
  {
    if ((c.x1 <= this.x0) || (c.x0 >= this.x1)) return false;
    if ((c.y1 <= this.y0) || (c.y0 >= this.y1)) return false;
    if ((c.z1 <= this.z0) || (c.z0 >= this.z1)) return false;
    return true;
  }

  public void move(float xa, float ya, float za)
  {
    this.x0 += xa;
    this.y0 += ya;
    this.z0 += za;
    this.x1 += xa;
    this.y1 += ya;
    this.z1 += za;
  }
}

And my method to get AABBs near player:

public ArrayList<AABB> getAABBs(AABB p){
		ArrayList<AABB> aabbs = new ArrayList<AABB>();
		
		int x0 = (int)p.x0;
		int x1 = (int)(p.x0 + 1.0f);
		int y0 = (int)p.y0;
		int y1 = (int)(p.y0 + 1.0f);
		int z0 = (int)p.z0;
		int z1 = (int)(p.z0 + 1.0f);
		
		if(x0 < 0) x0 = 0;
		if(y0 < 0) y0 = 0;
		if(z0 < 0) z0 = 0;
		if(x1 > activeChunks.size() * 16) x1 = (int)(activeChunks.size() * 16);
		if(y1 > activeChunks.size() * 16) y1 = (int)(activeChunks.size() * 16);
		if(z1 > activeChunks.size() * 16) z1 = (int)(activeChunks.size() * 16);
		
		for(int x = 0; x < x1; x++){
			for(int y = 0; y < y1; y++){
				for(int z = 0; z < z1; z++){
					if(playerIn.getBlockID(x, y, z) != Block.AIR_BLOCK.getID()){
						aabbs.add(new AABB(x, y, z, x + 1, y + 1, z + 1));
					}
				}
			}
		}
		
		return aabbs;
	}

Just to let you know, I did receive your messages and I might be willing to put some time into making some new videos about collision detection/response, but I won’t tie them into the old voxel series because it was a less than stellar series… I can make a separate video just for the theory and math behind it, if that would help at all?

That would be amazing! I have a little collision working now, but it’s not detecting whole chunks. If you could make one that would be great!

Thasnk ;D