Jump to content

Recommended Posts

Posted

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.

Posted

In the second version of your code, the one where you are using vectors. You are creating these vx and vz variables but then when you create the explosion you pass in the x and z value. What is the purpose of vx and vz then?

Posted

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.

Posted

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);
					    }

Posted

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

Posted

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.

Posted

The radius value should be added to the vz aswell otherwise "what you mentioned" will happend. And no the y coord shouldnt be used in this case. Unless you want these explosions to be created at a different height from that of the player.

Posted

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?

Posted

Look at this again

double vx = (player.posX + radius) * Math.cos(angle) -player.posZ * Math.sin(angle);
double vz = player.posX * Math.sin(angle) + player.posZ * Math.cos(angle);

 

Maybe teleport urself to the coordinate x=100000 and check out what will happen

  • 2 weeks later...
Posted

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;

Posted

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/

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.