Jump to content

[1.7.10] Creating a Ring Around Player


HalestormXV

Recommended Posts

Alright so I have an item that you use and it is supposed to create explosions around the player in a certain radius. Good news is, it works. Bad news is that it also causes the explosions where the player is standing which is not supposed to happen because then you take damage and fall into the hole it creates and all that nonsense. When really all that is supposed to happen is explosions are supposed to occur around you. So my code works but two problems occur which I can't figure out the math I guess to fix it.

 

First problem is the circle is a complete circle including the inside of it when it should only be the "outter ring" of the circle when the explosions occur.

 

Second problem is i know this code is TERRIBLE and inefficient since when the item is used there is a huge amount of lag time so any tips on how to fix that would be great (even if it is just adding a simple wait, the explosions don't all HAVE to occur at once which is ultimately what is causing it, since the same code with lightning strikes works fine.)

 

Here is the block of code that needs adjusting: (radius is defined up top)

http://pastebin.com/7FRY2FsV

 

 

 

EDIT: The above code is garbage yes. So I tried an alternative way using a vector (this is the first time using them so i could be using it totally wrong) but my problem is still the same. It blows up where I am standing also when I just want it to blow up in a ring around me. Here is the vector code piece.

 

					ChunkCoordinates location = player.getPlayerCoordinates();

					for (double degrees = 0.0D; degrees < 360.0D; degrees += 10.0D)
					{
						double angle = Math.toRadians(degrees);
						 Vec3 vec3 = Vec3.createVectorHelper(player.posX, player.posY + player.getEyeHeight(), player.posZ);
					      
					      double x = vec3.xCoord;
					      double y = vec3.yCoord;
					      double z = vec3.zCoord;
					      
					      double vx = x * Math.cos(angle) - z * Math.sin(angle);
					      double vz = x * Math.sin(angle) + z * Math.cos(angle);
					      			      
					      par2World.createExplosion(player, x, y, z, 3.2F, true);
					    }

 

 

Any help on which method to use would be appreciated. The vector way seems more efficient but i could be using it totally wrong.

Link to comment
Share on other sites

For this particular purpose, no not really. I mean if explosions are going to be created the terrain around it is going to be destroyed anyway so doing it in a circle is not really necessary.

 

However, I would still like to learn how to do this for the future, if for example I want to create rings of fire, replace blocks with lava in a circle around the player, etc. etc.

Link to comment
Share on other sites

well first of all why are you converting the position into a vector? you can just directly access the players position and use that

I think the main part of the lagg is that you are spawning 36 explosion. maybe you should try degrees+=20? And maybe use float instead of double you dont really need the precision of double there

 

for (double degrees = 0.0D; degrees < 360.0D; degrees += 10.0D)
					{
						double angle = Math.toRadians(degrees);

					      double vx = player.posX* Math.cos(angle) -player.posZ * Math.sin(angle);
					      double vz = player.posX * Math.sin(angle) + player.posZ * Math.cos(angle);
					      			      
					      par2World.createExplosion(player,(int)vx, player.posY, (int)vz, 3.2F, true);
					    }

Link to comment
Share on other sites

Do you mean that all the explosions are created at the player? Thats probably vecause your circle has no radius. You are just taking the player posx and posz and multiplying it witg sin and cos of the angle. Try this:

 

vx=(player.posx + radius) * sin(angle)....

 

I was on my phone and cant type well. But i think this should work

Link to comment
Share on other sites

Wohoo, getting somewhere now. I thank you for all the help by the way to the both of you. Circles and forming them along with fomring rings around the player is something once I get down, I can think of tons of things to do with them.

 

Here is the new code with the modification off adding in the radius calculation: http://pastebin.com/PEsUzhnh

 

And it works as expected. The radius which is defined at the top of my class as an int. When using the item it no longer creates the explosion at my feet, but at the proper radius. However the only problem that remains is that it is only in one direction (the x-coord), what adjustments do I have to make so that once you use this item explosions are created in all directions around the player within the specified radius.

 

Also, shouldn't the Y coordinate be used somewhere in here?

 

Ideally, think of mods that add an item that when you click it a ring of fire gets created around you in a certain radius. So like an arena of fire is created around you 6 blocks or 10 blocks out and the whole inside is your "fighting ground". Gaia Guardian from botania for example where once you activate the beacon the wall of particles surround you and you can only fight within that wall. I am looking for the same effect but obviously much simpler where explosions are just created around you.

Link to comment
Share on other sites

I hope you know that there is no way that code creates a circle around you

 

I'm afraid I don't know what you mean? I am not looking to create a circle around me. I am simply looking to have explosions be created in a circle around me with a radius of what I define. For example if you are familiar with the Botania mod the Gaia fight, you click on an item and a circle within a certain radius is created around you (of particle effects) and it essentially forms an arena around you.

 

I am trying to do the same thing here except not form an arena of particles. Simply create explosions.

 

As I said, this is my first time even venturing to work with this type of code so perhaps an example?

Link to comment
Share on other sites

  • 2 weeks later...

It's obvious some of the people on here don't quite have the grasp of trigonometry. So here's a quick rundown.

 

First off, Java's Math functions for sin, cos, and tan require Radians, not Degrees.

Fortunately, there's a convenience method toRadians that you can pass your angle in degrees.

 

Second, to use points calculated from angles, you use origins, and deltas. This is dead simple.

The sin, cos, and tan functions return numbers from 1 to -1. This is a "normalized" (technically it's not normalized, but for sake of argument, that's what we're calling it) delta based on the angle. To get the radius you want, multiply the calculated result by your radius. Now it's an un-normalized.

 

double radius = 16d;
double deltaX = Math.cos(Math.toRadians(angle))*radius;
// you may have to switch these two around, and play with the positivity/negativity, since I'm unsure which way is which for Minecraft's coordinate system
double deltaZ = -Math.sin(Math.toRadians(angle))*radius;

double finalX = player.posX + deltaX;
double finalZ = player.posZ + deltaZ;

Link to comment
Share on other sites

I find that the easiest code logically for this is to take a cube (or square) that is big enough to contain the entire sphere (or circle) and then just check (using simple Pythagorean theorem math) every position for distance to the center. If it is within a certain distance you can destroy it. If you want a ring instead then you would only destroy it within certain range of distances.

 

This is a lot simpler than using vectors, or sine / cosine, etc.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

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.

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • I'm using Modrinth as a launcher for a forge modpack on 1.20.1, and can't diagnose the issue on the crash log myself. Have tried repairing the Minecraft instillation as well as removing a few mods that have been problematic for me in the past to no avail. Crash log is below, if any further information is necessary let me know. Thank you! https://paste.ee/p/k6xnS
    • Hey folks. I am working on a custom "Mecha" entity (extended from LivingEntity) that the player builds up from blocks that should get modular stats depending on the used blocks. e.g. depending on what will be used for the legs, the entity will have a different jump strength. However, something unexpected is happening when trying to override a few of LivingEntity's functions and using my new own "Mecha" specific fields: instead of their actual instance-specific value, the default value is used (0f for a float, null for an object...) This is especially strange as when executing with the same entity from a point in the code specific to the mecha entity, the correct value is used. Here are some code snippets to better illustrate what I mean: /* The main Mecha class, cut down for brevity */ public class Mecha extends LivingEntity { protected float jumpMultiplier; //somewhere later during the code when spawning the entity, jumpMultiplier is set to something like 1.5f //changing the access to public didn't help @Override //Overridden from LivingEntity, this function is only used in the jumpFromGround() function, used in the aiStep() function, used in the LivingEntity tick() function protected float getJumpPower() { //something is wrong with this function //for some reason I can't correctly access the fields and methods from the instanciated entity when I am in one of those overridden protected functions. this is very annoying LogUtils.getLogger().info(String.valueOf(this.jumpMultiplier))) //will print 0f return this.jumpMultiplier * super.getJumpPower(); } //The code above does not operate properly. Written as is, the entity will not jump, and adding debug logs shows that when executing the code, the value of this.jumpMultiplier is 0f //in contrast, it will be the correct value when done here: @Override public void tick() { super.tick(); //inherited LivingEntity logic //Custom logic LogUtils.getLogger().info(String.valueOf(this.jumpMultiplier))) //will print 1.5f } } My actual code is slightly different, as the jumpMuliplier is stored in another object (so I am calling "this.legModule.getJumpPower()" instead of the float), but even using a simple float exactly like in the code above didn't help. When running my usual code, the object I try to use is found to be null instead, leading to a crash from a nullPointerException. Here is the stacktrace of said crash: The full code can be viewed here. I have found a workaround in the case of jump strength, but have already found the same problem for another parameter I want to do, and I do not understand why the code is behaving as such, and I would very much like to be able to override those methods as intended - they seemed to work just fine like that for vanilla mobs... Any clues as to what may be happening here?
    • Please delete post. Had not noticed the newest edition for 1.20.6 which resolves the issue.
    • https://paste.ee/p/GTgAV Here's my debug log, I'm on 1.18.2 with forge 40.2.4 and I just want to get it to work!! I cant find any mod names in the error part and I would like some help from the pros!! I have 203 mods at the moment.
  • Topics

×
×
  • Create New...

Important Information

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