Jump to content

[1.7.10] spawining an entity at precise world coordinates


Recommended Posts

Posted

Hi everyone,

 

I am looking to spawn an entity at precise world coordinates on top of a block to within (xCoord +or- 0.1, yCoord +or- 0.1,zCoord +or- 0.1) where xCoord is the world coordinate of each block.

currently I do my spawning like this but it is pretty inaccurate as the entity doesnt spawn where the + on the screen (that represents the mouse) points.

 

 

//Gets the ray trace vector from the player using the arguments  distance, partialTickTime
					MovingObjectPosition movingObjectPosition = player.rayTrace(changeableRange, 1F);
					if (movingObjectPosition != null && !player.worldObj.isAirBlock(movingObjectPosition.blockX, movingObjectPosition.blockY, movingObjectPosition.blockZ) && (movingObjectPosition.typeOfHit == MovingObjectPosition.MovingObjectType.BLOCK || movingObjectPosition.typeOfHit == MovingObjectPosition.MovingObjectType.ENTITY))
					{
//Note* movingObjectPosition.blockX +0.5 and movingObjectPosition.blockZ + 0.5 are there so that the entity doesnt spawn at the corner of the block, movingObjectPosition.blockY + 1 is so that the entity spawns above ground
					player.worldObj.spawnEntityInWorld(new EntityCustom(player.worldObj, movingObjectPosition.blockX +0.5, movingObjectPosition.blockY + 1, movingObjectPosition.blockZ +0.5)); 
					}

 

 

 

is there a more accurate way of doing this using the mouse position maybe and running the same sort of checks as found in my if statement. My goal is to only spawn my entity if a non air block is selected, and the raytrace hits a block or entity and isnt null. Also it is important for the raytrace to have a changeableRange that can be altered to represent a max distance away from the player at which the entity can be spawned. Does anyone know of a more accurate way that this could be done so that I could spawn an entity at  block coordinates of example (xCoord =312.1, yCoord = 1, zCoord = 454.3) or more generically(xxx.u, 1, zzz.v) where xxx and zzz represent the integer block coordinate and u and v represent the decimal places and can have a value of 0-9 so you can spawn the entity at

 

(xCoord = 312.1 or 312.2 or 312.3...and so on up to... 313.0, yCoord = 1, and zCoord =454.1 or 454.2 or 454.3...and so on up to... 455.0) as determined by where the + on the screen (that represents the mouse) points so that if (for example) it points 0.4 position units along the x axis of a block and 0.2 units along the z axis of a block that is where the entity will spawn (1 block above this point on the x/z plane).

 

(hopefully I have clearly described the functionality i am searching for)

Posted

Mhmm, ray tracing should work and give you the coordinates but I once saw someone using a little trick to spawn an entity at the players position + n number of blocks used in one of my experimental mods (If i remember correctly it works :P).

 

int n = 5;
Vec3 playerLookVector = player.getLookVec();
if (EntityList.stringToClassMapping.containsKey("entity_name"))
{
EntityLiving entityToSpawn = (EntityLiving) EntityList.createEntityByName("entity_name", worldObj);
double spawnX = player.posX+n*playerLookVector.xCoord;
double spawnZ = player.posZ+n*playerLookVector.zCoord;
double spawnY = worldObj.getHeightValue((int)spawnX, (int)spawnZ);
entityToSpawn.setLocationAndAngles(spawnX, spawnY, spawnZ, MathHelper.wrapAngleTo180_float(rand.nextFloat()	* 360.0F), 0.0F);
worldObj.spawnEntityInWorld(entityToSpawn);
}

 

It's not precise but got the job done, if you want to go for the Ray Trace route:

 

If you spawn the entity at close range does it spawn at the crosshair? At a distance the ray trace might be hitting something else and it gives you the wrong coordinates. Remember at a distance the pixel the mouse is on might be two blocks or more... Even if the chunk is not loaded it might give you incorrect values.

I require Java, both the coffee and the code :)

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.

Guest
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.

Announcements



×
×
  • Create New...

Important Information

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