Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

How to make Rotated Bounding boxes proper


thebest108
 Share

Recommended Posts

My ghetto version of rotated bounding boxes is too imprecise for me to keep using them. This is how crap they are:

[embed=425,349]<iframe width="560" height="315" src="https://www.youtube.com/embed/qM5l3-U7ZX4" frameborder="0" allowfullscreen></iframe>[/embed]

 

Hopefully showing I have something before asking will get me some sympathy, but anyway is there a library or something somewhere that can do rotated bounding boxes properly? I have no formal programming training so I dont know how to approach this

"you seem to be THE best modder I've seen imo."

~spynathan

 

ლ(́◉◞౪◟◉‵ლ

Link to comment
Share on other sites

1) Why are you doing this?

2) You posted no code.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

2. Im just wondering where I could get code for a bounding box thats able to rotate

Axis Aligned Bounding Boxes are aligned to the axis for a reason.

All entity bounding boxes are axis aligned as well.

You are having to delve deep into code that few people look at, much less touch, and do things that don't make a whole lot of sense. You're going to have to work on the problem by yourself for the most part.

 

Off hand though, it appears that whatever angle of rotation you're using isn't consistently calculated from one draw frame to the next.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Ive got something like this which kind of works but not yet. The collision detection is spot on but I think I screwed up the offsets

 
package FlyingFortress.BoundingBoxes;

import java.util.ArrayList;

import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.MathHelper;
import net.minecraft.util.MovingObjectPosition;
import net.minecraft.util.Vec3;
import FlyingFortress.Entities.Ship;

public class ShipBoundingBox extends AxisAlignedBB{

Ship parent;
private static AxisAlignedBB tempBB;

public ShipBoundingBox(double x1, double y1, double z1, double x2, double y2, double z2) {
	super(x1, y1, z1, x2, y2, z2);
}

public ShipBoundingBox(Ship ship){
	//Will be the ship min/max coords
	super(ship.posX-10,ship.posY-10,ship.posZ-10,ship.posX+10,ship.posY+10,ship.posZ+10);
	parent = ship;
}

@Override
public boolean intersectsWith(AxisAlignedBB other){
	AxisAlignedBB translated = transformToLocal(other);
	for(Object o:parent.region.getCollidingBoundingBoxes(null, translated)){
		tempBB = (AxisAlignedBB)o;
		if(translated.intersectsWith(tempBB)){
			return true;
		}
	}	
	return false;
}

@Override
    public double calculateXOffset(AxisAlignedBB other, double p_72316_2_){
	ArrayList<AxisAlignedBB> m80s = new ArrayList<AxisAlignedBB>();
	AxisAlignedBB translated = transformToLocal(other);
	m80s.addAll(parent.region.getCollidingBoundingBoxes(null, translated));
	double highest = 0;
	for(AxisAlignedBB bb:m80s){
		if(bb.calculateXOffset(translated, highest)>highest){
			highest= bb.calculateXOffset(translated, highest);
		}
	}
	return highest;
}

@Override
    public double calculateYOffset(AxisAlignedBB other, double p_72316_2_){
	ArrayList<AxisAlignedBB> m80s = new ArrayList<AxisAlignedBB>();
	AxisAlignedBB translated = transformToLocal(other);
	m80s.addAll(parent.region.getCollidingBoundingBoxes(null, translated));
	double highest = 0;
	for(AxisAlignedBB bb:m80s){
		if(bb.calculateYOffset(translated, highest)>highest){
			highest= bb.calculateYOffset(translated, highest);
			System.out.println("Obm");
		}
	}
	return highest;
}

@Override
    public double calculateZOffset(AxisAlignedBB other, double p_72316_2_){
	ArrayList<AxisAlignedBB> m80s = new ArrayList<AxisAlignedBB>();
	AxisAlignedBB translated = transformToLocal(other);
	m80s.addAll(parent.region.getCollidingBoundingBoxes(null, translated));
	double highest = 0;
	for(AxisAlignedBB bb:m80s){
		if(bb.calculateZOffset(translated, highest)>highest){
			highest= bb.calculateZOffset(translated, highest);
		}
	}
	return highest;
}

@Override
public boolean isVecInside(Vec3 vec){
        AxisAlignedBB temp = new AxisAlignedBB(vec.xCoord-.05D,vec.yCoord-.05D,vec.zCoord-.05D,vec.xCoord+.05D,vec.yCoord+.05D,vec.zCoord+.05D);
        AxisAlignedBB translated = transformToLocal(temp);
        ArrayList<AxisAlignedBB> m80s = new ArrayList<AxisAlignedBB>();
	for(Object o:parent.region.getCollidingBoundingBoxes(null, translated)){
		m80s.add((AxisAlignedBB)o);
	}
        
	for(AxisAlignedBB bb:m80s){
		if(bb.isVecInside(vec)){
			return true;
		}
	}
        return false;
    }

@Override
public AxisAlignedBB addCoord(double x, double y, double z){
        return this;
    }

@Override
public AxisAlignedBB expand(double x, double y, double z){
	return this;
}

@Override
public AxisAlignedBB union(AxisAlignedBB other){
	return this;
}

@Override
public AxisAlignedBB offset(double x, double y, double z){
        return this;
    }

public AxisAlignedBB transformToLocal(AxisAlignedBB inRealWorld){
	double xSize = (inRealWorld.maxX-inRealWorld.minX)/2.0D;
	double ySize = (inRealWorld.maxY-inRealWorld.minY)/2.0D;
	double zSize = (inRealWorld.maxZ-inRealWorld.minZ)/2.0D;
	double xPos = (inRealWorld.minX+inRealWorld.maxX)/2.0D;
	double yPos = (inRealWorld.minY+inRealWorld.maxY)/2.0D-parent.posY+64;
	double zPos = (inRealWorld.minZ+inRealWorld.maxZ)/2.0D;
	Vec3 rotatedPos = rotateVec3DAroundVec3D(new Vec3(xPos,yPos,zPos),new      Vec3(parent.posX,parent.posY,parent.posZ),-parent.rotationYaw);
	Vec3 localCoords = new Vec3(rotatedPos.xCoord-parent.posX,yPos,rotatedPos.zCoord-parent.posZ);	
	return new AxisAlignedBB(localCoords.xCoord-xSize+.5D,localCoords.yCoord-ySize,localCoords.zCoord-zSize+.5D,localCoords.xCoord+xSize+.5D,localCoords.yCoord+ySize,localCoords.zCoord+zSize+.5D);
}

public Vec3 rotateVec3DAroundVec3D(Vec3 var1, Vec3 var2, double var3){
        double var5 = Math.cos(-var3 / 180.0D * Math.PI);
        double var7 = Math.sin(-var3 / 180.0D * Math.PI);
        double var9 = var1.xCoord - var2.xCoord;
        double var11 = var1.zCoord - var2.zCoord;
        double var13 = var9 * var5 - var11 * var7;
        double var15 = var9 * var7 + var11 * var5;
        double var17 = var13 + var2.xCoord;
        double var19 = var15 + var2.zCoord;
        return new Vec3(var17, var1.yCoord, var19);
    }

}

 

Any suggestions on how to fix?

"you seem to be THE best modder I've seen imo."

~spynathan

 

ლ(́◉◞౪◟◉‵ლ

Link to comment
Share on other sites

  • 6 years later...
On 9/1/2015 at 11:05 PM, thebest108 said:

Ive got something like this which kind of works but not yet. The collision detection is spot on but I think I screwed up the offsets

 


 
package FlyingFortress.BoundingBoxes;

import java.util.ArrayList;

import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.MathHelper;
import net.minecraft.util.MovingObjectPosition;
import net.minecraft.util.Vec3;
import FlyingFortress.Entities.Ship;

public class ShipBoundingBox extends AxisAlignedBB{

Ship parent;
private static AxisAlignedBB tempBB;

public ShipBoundingBox(double x1, double y1, double z1, double x2, double y2, double z2) {
	super(x1, y1, z1, x2, y2, z2);
}

public ShipBoundingBox(Ship ship){
	//Will be the ship min/max coords
	super(ship.posX-10,ship.posY-10,ship.posZ-10,ship.posX+10,ship.posY+10,ship.posZ+10);
	parent = ship;
}

@Override
public boolean intersectsWith(AxisAlignedBB other){
	AxisAlignedBB translated = transformToLocal(other);
	for(Object o:parent.region.getCollidingBoundingBoxes(null, translated)){
		tempBB = (AxisAlignedBB)o;
		if(translated.intersectsWith(tempBB)){
			return true;
		}
	}	
	return false;
}

@Override
    public double calculateXOffset(AxisAlignedBB other, double p_72316_2_){
	ArrayList<AxisAlignedBB> m80s = new ArrayList<AxisAlignedBB>();
	AxisAlignedBB translated = transformToLocal(other);
	m80s.addAll(parent.region.getCollidingBoundingBoxes(null, translated));
	double highest = 0;
	for(AxisAlignedBB bb:m80s){
		if(bb.calculateXOffset(translated, highest)>highest){
			highest= bb.calculateXOffset(translated, highest);
		}
	}
	return highest;
}

@Override
    public double calculateYOffset(AxisAlignedBB other, double p_72316_2_){
	ArrayList<AxisAlignedBB> m80s = new ArrayList<AxisAlignedBB>();
	AxisAlignedBB translated = transformToLocal(other);
	m80s.addAll(parent.region.getCollidingBoundingBoxes(null, translated));
	double highest = 0;
	for(AxisAlignedBB bb:m80s){
		if(bb.calculateYOffset(translated, highest)>highest){
			highest= bb.calculateYOffset(translated, highest);
			System.out.println("Obm");
		}
	}
	return highest;
}

@Override
    public double calculateZOffset(AxisAlignedBB other, double p_72316_2_){
	ArrayList<AxisAlignedBB> m80s = new ArrayList<AxisAlignedBB>();
	AxisAlignedBB translated = transformToLocal(other);
	m80s.addAll(parent.region.getCollidingBoundingBoxes(null, translated));
	double highest = 0;
	for(AxisAlignedBB bb:m80s){
		if(bb.calculateZOffset(translated, highest)>highest){
			highest= bb.calculateZOffset(translated, highest);
		}
	}
	return highest;
}

@Override
public boolean isVecInside(Vec3 vec){
        AxisAlignedBB temp = new AxisAlignedBB(vec.xCoord-.05D,vec.yCoord-.05D,vec.zCoord-.05D,vec.xCoord+.05D,vec.yCoord+.05D,vec.zCoord+.05D);
        AxisAlignedBB translated = transformToLocal(temp);
        ArrayList<AxisAlignedBB> m80s = new ArrayList<AxisAlignedBB>();
	for(Object o:parent.region.getCollidingBoundingBoxes(null, translated)){
		m80s.add((AxisAlignedBB)o);
	}
        
	for(AxisAlignedBB bb:m80s){
		if(bb.isVecInside(vec)){
			return true;
		}
	}
        return false;
    }

@Override
public AxisAlignedBB addCoord(double x, double y, double z){
        return this;
    }

@Override
public AxisAlignedBB expand(double x, double y, double z){
	return this;
}

@Override
public AxisAlignedBB union(AxisAlignedBB other){
	return this;
}

@Override
public AxisAlignedBB offset(double x, double y, double z){
        return this;
    }

public AxisAlignedBB transformToLocal(AxisAlignedBB inRealWorld){
	double xSize = (inRealWorld.maxX-inRealWorld.minX)/2.0D;
	double ySize = (inRealWorld.maxY-inRealWorld.minY)/2.0D;
	double zSize = (inRealWorld.maxZ-inRealWorld.minZ)/2.0D;
	double xPos = (inRealWorld.minX+inRealWorld.maxX)/2.0D;
	double yPos = (inRealWorld.minY+inRealWorld.maxY)/2.0D-parent.posY+64;
	double zPos = (inRealWorld.minZ+inRealWorld.maxZ)/2.0D;
	Vec3 rotatedPos = rotateVec3DAroundVec3D(new Vec3(xPos,yPos,zPos),new      Vec3(parent.posX,parent.posY,parent.posZ),-parent.rotationYaw);
	Vec3 localCoords = new Vec3(rotatedPos.xCoord-parent.posX,yPos,rotatedPos.zCoord-parent.posZ);	
	return new AxisAlignedBB(localCoords.xCoord-xSize+.5D,localCoords.yCoord-ySize,localCoords.zCoord-zSize+.5D,localCoords.xCoord+xSize+.5D,localCoords.yCoord+ySize,localCoords.zCoord+zSize+.5D);
}

public Vec3 rotateVec3DAroundVec3D(Vec3 var1, Vec3 var2, double var3){
        double var5 = Math.cos(-var3 / 180.0D * Math.PI);
        double var7 = Math.sin(-var3 / 180.0D * Math.PI);
        double var9 = var1.xCoord - var2.xCoord;
        double var11 = var1.zCoord - var2.zCoord;
        double var13 = var9 * var5 - var11 * var7;
        double var15 = var9 * var7 + var11 * var5;
        double var17 = var13 + var2.xCoord;
        double var19 = var15 + var2.zCoord;
        return new Vec3(var17, var1.yCoord, var19);
    }

}

 

 

 

Any suggestions on how to fix?

Sorry for the Necro post....
So anyone looking at this thread, it's probably because this person tried to create a new axisalignedBB with rotated cords. Since an axisalignedBB can only make a cube that's aligned with the world cordinates with the two new cords, what happens is the two cords are basically wrapped with the rotated cordinates. So the size changes but it never actually rotates. For anyone looking for a solution to this particular issue in the future, a custom class called something like "LocalAlignedBB" would need to be coded that allows for rotated edges. Then have a custom renderer for how to render it in debug properly and also have custom math to figure out the collision logic, since the old AxisAlignedBB code would be garbage for the new application. Using the previous axisalignedBB would help in making the collision logic, but new math is required to figure out if the cordinate is inside the box *After* rotation and not before

Link to comment
Share on other sites

Still don't know why anyone would assume that an Axis Aligned bounding box could be anything other than aligned to axis.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share



×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.