Jump to content

how to implement a custom collision handler??


Recommended Posts

Just to note, I have absolutely no experience with java and minecraft...

but I'm not here to get schooled on that...


I'm trying to do something seemingly impossible  (as usual)...


Basically I'm building a mod that gives autistically-creative people the ability to add custom progammable models (up to characters) to the game.


To help you get an understanding, here's a screenie of some of my foodoolery testing:



I may know nothing about java, but I know quite a lot about models (search up my project Universal Model Converter for some info).

However, I lack in knowledge on animation and collision on models.


So what I have above has absolutely no physics.

I want to make the above be detectable by minecraft to where the player can collide  with the model, such as standing on a (possibly slanted) platform, or the model  (for characters) can collide with the player and world.


A quick overview of the Entity class shows me minecraft doesn't understand anything other than collision boxes, which doesn't work  well for triangles or slanted models...


So, what could I do to implement better collisions into minecraft, and what functions would I need to use/override to make this work as expected??


Thanks :)

Link to comment
Share on other sites

With minecraft, you should do many hijacking with use of entity to achieve something like that, because minecraft uses AABB box to control collision. In regular way, you just have box align


OBB will suit your needs if you can't live with boxes, but it will be challanging to achieve that. (If you have little experience to java, it gets far worse)

I. Stellarium for Minecraft: Configurable Universe for Minecraft! (WIP)

II. Stellar Sky, Better Star Rendering&Sky Utility mod, had separated from Stellarium.

Link to comment
Share on other sites

Lol, I was already plotting out the complexities of a separate interface, kind of like what AE2 and Mallisis Doors does, except more than simply re-implementing the AABB base-class.


So I kind of already know what you just told me ;)

I just need a little help understanding what's safe to hijack and how to do it in a safe manner.


Big thanks :)



oh yea, forgot to ask... :P

what exactly is OBB??


And thanks for the insight btw. :)

Link to comment
Share on other sites

I'm certain that there is no easy way but checking everything manually on onUpdate or moveEntity. (They are used to check bounds of them)

Also you have to change the movement of other entities like players, and the only ways to do that are using LivingUpdateEvent(This limits everything to living entity) or wrapping entities when they are spawned(Which needs much more research and can be impossible to retain compatibility).

Or, there is coremod, which should be avoided if possible. (But this could be the only option in some case)

This is the cause why there are few ship mods, and they tend to be unstable.


And, OBB is abbreviation for Oreinted Bounding Box, wiki link

I. Stellarium for Minecraft: Configurable Universe for Minecraft! (WIP)

II. Stellar Sky, Better Star Rendering&Sky Utility mod, had separated from Stellarium.

Link to comment
Share on other sites

And "AABB" is an abbreviation of "Axis Aligned Bounding Box" which is a kind of OBB that is oriented to be congruent with the world's 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

Oh goodie, looks like I need to hack then, and possibly biuld a library to wrap support with... #notamused


The only problem with that, and why I wanted to avoid that is because of the safety/compatability with other mods, which I really don't want to interfere with...


If that's what it comes to though, it's really not all that complex...

I just need to know how to keep it safe.


Hey I have no doubt it'll work as expected...

I don't expect entities to glitch through collisions and such, as that's something I won't allow.

(If it means sending an extra network packet to ensure the positions, I'll do it)


The only doubt I have is crashing the client. >_>


Anyways, no OBB won't work either... you can't fit a box inside a triangle.

I'm aiming for something like what blender's game engine does, where the mesh itself is used for collision.


Sure boxes save a little CPU, but they're anything but accurate when it comes to something like this.


A separate low-poly mesh is the typical norm for collision now-a-days.

(Used in games like Smash Melee and Brawl)

which THAT'S what I'm aiming for.



@Draco18s: Thank you Captain Obvious

(as your text and sig so gracefully implies)


You're welcome


Lol, but no, seriously thanks for the attempt to help :)

much appreciated ;)

Link to comment
Share on other sites


Just wanted to make a comment after rereading my response and taking notice of how rude I sounded.


I wasn't trying to be rude when I made that comment...

rather I was only trying to have a little fun with the circumstance.


I never mean any offence with anything I say. :)

so excuse my autism when I don't realize what I'm saying, and try to look at what I'm saying as a joke rather than an insult.


I'm sorry if you took that the wrong way and I offended you.

I like to make friends, not enemies.

Link to comment
Share on other sites

Rather than creating a new bounding box system that will cause headaches, why not change the action that's performed when a player collides with your model?  It should be simple.


First, ensure the current bounding box covers the entire model, even when rotating.  You can view the box by pressing F3+B to ensure it's the right size.  Next, override applyEntityCollision to get rid of the default bounding-box based collision system and create your own.  If your entity is a triangle, you can use the entity's rotationYaw to find out where the slope is and whether or not to you need to prevent movement.  More complex entities would have to be broken up into parts, but that's no different than breaking up your model mesh.


You could also link multiple entities together to make a collision system.  It's what I do for one of my mods.  That's much more difficult, though, so I'd recommend sticking with a collision mesh if you can.

Link to comment
Share on other sites

Oh wow that sounds insanely simple when put into perspective :o

and here I call myself a programmer ::)



Big thanks for the tip :)


Is it possible to localize my custom handler to my mod alone??

(Avoiding any lingering security issues with other mods)

Link to comment
Share on other sites

You're only overriding that method on the Entities that you create, so inherently there's no incompatibility.  If a mod overrides default collision handling, your mod will override theirs, as you're overriding the default method that they overrode.  While this might seem like you're breaking their system, consider that their code would have broken yours.  In general, you don't want to mess with default Minecraft code.  Just create your own that works the way you want it to.

Link to comment
Share on other sites

ah, well I see that was a stupid question... lol

I should've known that one :P


thanks for the clarification :)


I'll lock this topic (whenever I have net access) if I manage to get it working. ;)

(otherwize I may have more related sub-questions)


thanks again everyone :)

Link to comment
Share on other sites

ugh... as I was afraid of... I've tried to extend my Widget class (the base class for the triangles) with the Entity class.

when I try to run the LUA script to add the entities, I get kicked out of my game and everything messes up...


is there an example which adds a raw entity that I can follow??

a few google searches doesn't seem to give me what I need, since the results only list stuff for Entity children classes.


if not, then what can I do to get this working??


if you'd like to see my source, I've actually copied the source with Marcin212's permission for OpenGlasses


so here's my code for the Widget class:

package com.tcll.ochardlight.surface;

import java.util.HashMap;

import com.tcll.ochardlight.lua.LuaReference;
import com.tcll.ochardlight.surface.widgets.core.AttributeRegistry;
import com.tcll.ochardlight.surface.widgets.core.attribute.IAttribute;

import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;

//import net.minecraftforge.fml.relauncher.Side;
//import net.minecraftforge.fml.relauncher.SideOnly;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

public abstract class Widget extends Entity implements IAttribute{
boolean isVisable = true;

public Widget(World worldIn) {
	// TODO Auto-generated constructor stub

public abstract void readData(ByteBuf buff);
public abstract void writeData(ByteBuf buff);
public final void read(ByteBuf buff){ isVisable = buff.readBoolean(); readData(buff); }
public final void write(ByteBuf buff){ buff.writeBoolean(isVisable); writeData(buff); }

public Object[] getLuaObject(LuaReference ref) {
	HashMap<String, Object> luaObject = new HashMap<String, Object>();
	Class<?> current = getClass();
	do {
		for (Class<?> a : current.getInterfaces()) {
			if (IAttribute.class.isAssignableFrom(a)) {
				luaObject.putAll(AttributeRegistry.getFunctions(a.asSubclass(IAttribute.class), ref));
		current = current.getSuperclass();
	} while (!current.equals(Object.class));

	return new Object[] { luaObject };

public void setVisable(boolean isVisable) { this.isVisable = isVisable; }
public boolean isVisible() { return isVisable; }

public abstract WidgetType getType();
public RenderType getRenderType() { return RenderType.WorldLocated; }
public boolean shouldWidgetBeRendered() { return isVisible(); }
public void render(EntityPlayer player, double playerX, double playerY, double playerZ) {}

protected void entityInit() {
	// TODO Auto-generated method stub
protected void readEntityFromNBT(NBTTagCompound nbt){
	if(!nbt.hasKey("WidgetData")) return;
	byte[] b = nbt.getByteArray("WidgetData");
	ByteBuf buff = Unpooled.copiedBuffer(b);

protected void writeEntityToNBT(NBTTagCompound nbt) {
	ByteBuf buff = Unpooled.buffer();
	nbt.setByteArray("WidgetData", buff.array());



and here's my code for the triangle class:

package com.tcll.ochardlight.surface.widgets.component.world;

import io.netty.buffer.ByteBuf;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

import org.lwjgl.opengl.GL11;

import com.tcll.ochardlight.surface.RenderType;
import com.tcll.ochardlight.surface.Widget;
import com.tcll.ochardlight.surface.WidgetType;
import com.tcll.ochardlight.surface.widgets.core.attribute.I3DVertex;
import com.tcll.ochardlight.surface.widgets.core.attribute.IColorizable;
import com.tcll.ochardlight.surface.widgets.core.attribute.IThroughVisibility;
import com.tcll.ochardlight.utils.Color;
import com.tcll.ochardlight.utils.Vector;

public class Triangle3D extends Widget implements IColorizable, IThroughVisibility, I3DVertex {
Vector[] positions = {new Vector(0,0,0), new Vector(0,0,0), new Vector(0,0,0)};
Color[] colors = {new Color(0,0,0,.5f), new Color(0,0,0,.5f), new Color(0,0,0,.5f)};
boolean isThroughVisibility = false;

public Triangle3D(World worldIn) {
	// TODO Auto-generated constructor stub
//public Triangle3D() {}

public void writeData(ByteBuf buff) {

public void readData(ByteBuf buff) {
	positions[0].x = buff.readFloat();
	positions[1].x = buff.readFloat();
	positions[2].x = buff.readFloat();
	positions[0].y = buff.readFloat();
	positions[1].y = buff.readFloat();
	positions[2].y = buff.readFloat();
	positions[0].z = buff.readFloat();
	positions[1].z = buff.readFloat();
	positions[2].z = buff.readFloat();
	colors[0].a = buff.readFloat();
	colors[1].a = buff.readFloat();
	colors[2].a = buff.readFloat();
	colors[0].r = buff.readFloat();
	colors[1].r = buff.readFloat();
	colors[2].r = buff.readFloat();
	colors[0].g = buff.readFloat();
	colors[1].g = buff.readFloat();
	colors[2].g = buff.readFloat();
	colors[0].b = buff.readFloat();
	colors[1].b = buff.readFloat();
	colors[2].b = buff.readFloat();
	isThroughVisibility = buff.readBoolean();

public void render(EntityPlayer player, double playerX, double playerY, double playerZ) {
	GL11.glBegin(GL11.GL_TRIANGLES);//_STRIP); // pseudo double-sided support
	GL11.glColor4f(colors[0].r, colors[0].g, colors[0].b, colors[0].a);
	GL11.glVertex3f(positions[0].x, positions[0].y, positions[0].z);
	GL11.glColor4f(colors[1].r, colors[1].g, colors[1].b, colors[1].a);
	GL11.glVertex3f(positions[1].x, positions[1].y, positions[1].z);
	GL11.glColor4f(colors[2].r, colors[2].g, colors[2].b, colors[2].a);
	GL11.glVertex3f(positions[2].x, positions[2].y, positions[2].z);
	// pseudo double-sided support:
	//GL11.glColor4f(colors[0].r, colors[0].g, colors[0].b, colors[0].a);
	//GL11.glVertex3f(positions[0].x, positions[0].y, positions[0].z);

public WidgetType getType() { return WidgetType.TRIANGLE3D; }
public RenderType getRenderType() { return RenderType.WorldLocated; }
public boolean shouldWidgetBeRendered() { return isVisible(); }

public void setColor(int n, double r, double g, double b, double a) {
	colors[n].r = (float) r; colors[n].g = (float) g; colors[n].b = (float) b; colors[n].a = (float) a; }
public double[] getColor(int n) { double[] cl = {
	colors[n].r, colors[n].g, colors[n].b, colors[n].a }; return cl; } // Tcll - how can I return the collection w/o `cl`??

public void setVisibleThroughObjects(boolean visible) { isThroughVisibility = visible; }
public boolean isVisibleThroughObjects() { return isThroughVisibility; }

public void setVertex(int n, double x, double y, double z) { positions[n].x = (float) x; positions[n].y = (float) y; positions[n].z = (float) z; }
public int getVertexCount() { return positions.length; }



and also, the code which renders everything:

package com.tcll.ochardlight.surface;

//import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL20;
//import org.lwjgl.opengl.GL30;

//import com.tcll.ochardlight.surface.widgets.component.world.FloatingText;
import com.tcll.ochardlight.utils.Location;

import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.math.RayTraceResult;

//import net.minecraftforge.client.event.RenderGameOverlayEvent;
//import net.minecraftforge.client.event.RenderGameOverlayEvent.ElementType;
import net.minecraftforge.client.event.RenderWorldLastEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

public class ClientSurface {
public static ClientSurface instances = new ClientSurface();
public Map<Integer, Widget> renderables = new ConcurrentHashMap<Integer, Widget>();
public Map<Integer, Widget> renderablesWorld = new ConcurrentHashMap<Integer, Widget>();
boolean isPowered = true;
//public boolean haveGlasses = false;
public Location lastBind;
//IRenderableWidget noPowerRender;
private ClientSurface() {
	noPowerRender = getNoPowerRender();

// TODO: is this confusion really needed?? 
public void updateWigets(Set<Entry<Integer, Widget>> widgets){
	for(Entry<Integer, Widget> widget : widgets){
		Widget w = widget.getValue();
		case GameOverlayLocated: renderables.put(widget.getKey(), w);	break; // not used
		case WorldLocated: renderablesWorld.put(widget.getKey(), w); break;

public void removeWidgets(List<Integer> ids){
	for(Integer id : ids){ renderables.remove(id); renderablesWorld.remove(id); }

public void removeAllWidgets(){ renderables.clear(); renderablesWorld.clear(); }
public void onRenderGameOverlay(RenderGameOverlayEvent evt) {
	if (evt instanceof RenderGameOverlayEvent.Post) {
		//if(!isPowered || lastBind == null){ noPowerRender.render(null, 0, 0, 0); return;}
		if(lastBind == null) {lastBind = new Location();};
		if(!isPowered){ noPowerRender.render(null, 0, 0, 0); };
		GL11.glScaled(evt.getResolution().getScaledWidth_double()/512D, evt.getResolution().getScaledHeight_double()/512D*16D/9D, 0);

		for(IRenderableWidget renderable : renderables.values()){
				renderable.render(null, 0, 0, 0);

//			GL11.glBegin(GL11.GL_QUADS);
//			GL11.glColor4f(1, 0, 0, 1);
//			GL11.glVertex3f(0, 0, 0);
//			GL11.glVertex3f(0, 10, 0);
//			GL11.glVertex3f(10, 10, 0);
//			GL11.glVertex3f(10, 0, 0);
//			GL11.glEnd();
public void renderWorldLastEvent(RenderWorldLastEvent event) {	
	//if(!isPowered || lastBind == null) return;
	if(lastBind == null) {lastBind = new Location();};
	EntityPlayer player= Minecraft.getMinecraft().thePlayer;
	double playerX = player.prevPosX + (player.posX - player.prevPosX) * event.getPartialTicks(); 
	double playerY = player.prevPosY + (player.posY - player.prevPosY) * event.getPartialTicks();
	double playerZ = player.prevPosZ + (player.posZ - player.prevPosZ) * event.getPartialTicks();
	GL11.glTranslated(-playerX, -playerY, -playerZ);
	GL11.glTranslated(lastBind.x, lastBind.y, lastBind.z);



	GL11.glPolygonMode( GL11.GL_FRONT_AND_BACK, GL11.GL_FILL );
	GL11.glDisable( GL11.GL_CULL_FACE );



	//int prog = GL20.GL_CURRENT_PROGRAM;
	GL20.glUseProgram( 0 );
	//Start Drawing In World

	for(Widget renderable : renderablesWorld.values()){
		if(renderable.shouldWidgetBeRendered()) renderable.render(player, playerX - lastBind.x, playerY - lastBind.y, playerZ - lastBind.z);

	//Stop Drawing In World
	//GL20.glUseProgram( prog );

public static RayTraceResult getBlockCoordsLookingAt(EntityPlayer player){
	RayTraceResult objectMouseOver;
	objectMouseOver = player.rayTrace(200, 1);	
	if(objectMouseOver != null && objectMouseOver.typeOfHit == RayTraceResult.Type.BLOCK) { return objectMouseOver; }
	return null;

public void setPowered(boolean isPowered) { this.isPowered = isPowered; }
private IRenderableWidget getNoPowerRender(){
	Text t = new Text();
	t.setText("NO POWER");
	t.setColor(1,1, 0.25, 0.25, 0.5);
	return t.getRenderable();


I'm sure plenty of people here will find disgusting implementations in the code, heck, even Marcin212 himself says his code sucks :P



what do I not know I'm doing wrong??


thanks :)

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.
Note: Your post will require moderator approval before it will be visible.

Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
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.


  • Create New...

Important Information

By using this site, you agree to our Terms of Use.