TheRPGAdventurer Posted November 26, 2018 Posted November 26, 2018 This question is more java mathematics related rather than minecraft related, so i plan to make a dragon whistle item that would command a dragon to either fly towards or circle the player when flying, so is my code for making the dragon circle the player and flying correct? protected boolean tryToCircleBlockPos(BlockPos midPoint, double speed) { // midpoint is gonna be player position dragon.setFlying(true); return dragon.getNavigator().tryMoveToXYZ(midPoint.getX() + 0.5 * Math.PI, midPoint.getY() + 10, midPoint.getZ() + 0.5 * Math.PI, speed); } Quote
Animefan8888 Posted November 26, 2018 Posted November 26, 2018 2 hours ago, TheRPGAdventurer said: so is my code for making the dragon circle the player and flying correct? Does the dragon circle the player? And it doesnt look correct to me you are not doing any trigonometry. 1 Quote VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect. Forge and vanilla BlockState generator.
Cadiboo Posted November 26, 2018 Posted November 26, 2018 (edited) You might want to google “draw circle with points”. I found a stackoverflow answer with a similar search that was very helpful and described how to find points for both an elipse and a circle Edited November 27, 2018 by Cadiboo Quote About Me Spoiler My Discord - Cadiboo#8887 My Website - Cadiboo.github.io My Mods - Cadiboo.github.io/projects My Tutorials - Cadiboo.github.io/tutorials Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support. When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible. Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)
TheRPGAdventurer Posted November 27, 2018 Author Posted November 27, 2018 yes circle above the player Quote
V0idWa1k3r Posted November 27, 2018 Posted November 27, 2018 (edited) Well, you could store the player's position somewhere within the dragon/AI in addition to the angle variable. When the AI to circle around kicks in calculate the current angle between these vectors [dragonPos - playerPos], [unitZ](<- I am not sure which direction the game consideres forward, I think it's unitZ though). You can calculate an angle between two vectors using arccos((vec1 DOT vec2) / (vec1.length * vec2.length)) if I am not mistaken. Then you can start circling, You know the angle the dragon started at so now you can just add to it each tick. The amount you add should be manually tweaked based on your dragon's speed. Now you have a new angle, which is your previous angle + X. You can then use this angle to calculate the position the dragon needs to be at: [x = playerX + r * cos(a), y = playerY + NUM, z = playerZ + r * sin(a) Where r is the distance from the dragon to the player you want to acheive(radius of your circle) and NUM is the height of the dragon relative to the player. Then just tell the dragon to move there. Edited November 27, 2018 by V0idWa1k3r 1 Quote
TheRPGAdventurer Posted November 27, 2018 Author Posted November 27, 2018 Just now, V0idWa1k3r said: Well, you could store the player's position somewhere within the dragon/AI in addition to the angle variable. When the AI to circle around kicks in calculate the current angle between these vectors [dragonPos - playerPos], [unitZ](<- I am not sure which direction the game consideres forward, I think it's unitZ though). You can calculate an angle between two vectors using arccos((vec1 DOT vec2) / (vec1.length * vec2.length)) what does DOT mean Quote
TheRPGAdventurer Posted November 27, 2018 Author Posted November 27, 2018 also would it be good to use this instead of arccos? dragon.getDistanceSq(midPoint); Quote
V0idWa1k3r Posted November 27, 2018 Posted November 27, 2018 32 minutes ago, TheRPGAdventurer said: what does DOT mean A dot product of two vectors. 23 minutes ago, TheRPGAdventurer said: also would it be good to use this instead of arccos? arccos is the operation that is the reverse of the cos operation. The squared distance is just x*x + y*y + z*z. So the answer is no since these are completely different operations. Quote
TheRPGAdventurer Posted November 27, 2018 Author Posted November 27, 2018 where is arccos found? Math.class? or MathHelper.class Quote
Animefan8888 Posted November 27, 2018 Posted November 27, 2018 29 minutes ago, TheRPGAdventurer said: where is arccos found? Math.class? or MathHelper.class Should be in Math, but it might be called acost instead of arccos. Quote VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect. Forge and vanilla BlockState generator.
TheRPGAdventurer Posted November 28, 2018 Author Posted November 28, 2018 protected boolean tryToCircleBlockPos(BlockPos midPoint, double speed) { Vec3d vec1 = new Vec3d(midPoint.getX(),midPoint.getY(),midPoint.getZ()); Vec3d vec2 = dragon.getPositionVector(); double r = Math.acos((vec1.dotProduct(vec2)) / (vec1.lengthVector() * vec2.lengthVector() ) ); double x = midPoint.getX() + r * Math.cos(dragon.rotationYaw + 90); double y = midPoint.getY() + 50; double z = midPoint.getZ() + r * Math.sin(dragon.rotationYaw + 90); return dragon.getNavigator().tryMoveToXYZ(midPoint.getX() + x, midPoint.getY() + y, midPoint.getZ() + z, speed); } so how'sthis Quote
V0idWa1k3r Posted November 28, 2018 Posted November 28, 2018 5 minutes ago, TheRPGAdventurer said: Vec3d vec1 = new Vec3d(midPoint.getX(),midPoint.getY(),midPoint.getZ()); Vec3d vec2 = dragon.getPositionVector(); Did I tell you "Get the position of the dragon and the position of the center and do things with those vectors"? No, I told you to On 11/27/2018 at 7:06 AM, V0idWa1k3r said: calculate the current angle between these vectors [dragonPos - playerPos], [unitZ] And you did none of that. Moving on 6 minutes ago, TheRPGAdventurer said: double r = Math.acos((vec1.dotProduct(vec2)) / (vec1.lengthVector() * vec2.lengthVector() ) ); Did I tell you "Oh, the radius of the circle is the angle you calculate"? No, because that makes zero sense. I told you On 11/27/2018 at 7:06 AM, V0idWa1k3r said: When the AI to circle around kicks in calculate the current angle between these vectors On 11/27/2018 at 7:06 AM, V0idWa1k3r said: Then you can start circling, You know the angle the dragon started at so now you can just add to it each tick. Moving on 7 minutes ago, TheRPGAdventurer said: double x = midPoint.getX() + r * Math.cos(dragon.rotationYaw + 90); Did I tell you "Use the dragon's rotation yaw plus an arbitrary number as an angle"? No, because unless you are rotating your dragon that makes no sense(and if you are - it is always looking away from the player and moves sideways). I told you to On 11/27/2018 at 7:06 AM, V0idWa1k3r said: Now you have a new angle, which is your previous angle + X. You can then use this angle to calculate the position the dragon needs to be at Moving on: 9 minutes ago, TheRPGAdventurer said: return dragon.getNavigator().tryMoveToXYZ(midPoint.getX() + x, midPoint.getY() + y, midPoint.getZ() + z, speed); So you have 2 vectors - midPoint and your [x, y, z]. [x, y, z] is midPoint + someNumbers. If you add them together you are essentially doubling the midPoint vector(kinda). So if your dragon is at [200, 100, 200] you are telling it to move to ~[400, 250, 400]. I don't think that it is what you want and that is not what I told you. Quote
TheRPGAdventurer Posted November 28, 2018 Author Posted November 28, 2018 also what is this "a" for playerX + r * cos(a) Quote
Cadiboo Posted November 28, 2018 Posted November 28, 2018 1 hour ago, TheRPGAdventurer said: also what is this "a" for playerX + r * cos(a) If r is radius then I assume a is angle Quote About Me Spoiler My Discord - Cadiboo#8887 My Website - Cadiboo.github.io My Mods - Cadiboo.github.io/projects My Tutorials - Cadiboo.github.io/tutorials Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support. When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible. Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)
TheRPGAdventurer Posted November 28, 2018 Author Posted November 28, 2018 protected boolean tryToCircleBlockPos(BlockPos midPoint, double speed) { Vec3d vec1 = new Vec3d(midPoint.getX(),midPoint.getY(),midPoint.getZ()); Vec3d vec2 = dragon.getPositionVector(); double a = Math.acos((vec1.dotProduct(vec2)) / (vec1.lengthVector() * vec2.lengthVector())); Vec3d r = vec1.subtract(vec2); double x = midPoint.getX() + r.x * Math.cos(a); double y = midPoint.getY() + 50; double z = midPoint.getZ() + r.z * Math.sin(a); return dragon.getNavigator().tryMoveToXYZ(midPoint.getX() + x, midPoint.getY() + y, midPoint.getZ() + z, speed); } Quote
V0idWa1k3r Posted November 28, 2018 Posted November 28, 2018 1 hour ago, TheRPGAdventurer said: Vec3d vec1 = new Vec3d(midPoint.getX(),midPoint.getY(),midPoint.getZ()); Vec3d vec2 = dragon.getPositionVector(); 6 hours ago, V0idWa1k3r said: calculate the current angle between these vectors [dragonPos - playerPos], [unitZ] You are still not doing that. 1 hour ago, TheRPGAdventurer said: double x = midPoint.getX() + r.x * Math.cos(a); On 11/27/2018 at 7:06 AM, V0idWa1k3r said: r is the distance from the dragon to the player you want to acheive(radius of your circle) Where did you get the impression that radius is somehow a vector? 1 hour ago, TheRPGAdventurer said: return dragon.getNavigator().tryMoveToXYZ(midPoint.getX() + x, midPoint.getY() + y, midPoint.getZ() + z, speed); 6 hours ago, V0idWa1k3r said: So you have 2 vectors - midPoint and your [x, y, z]. [x, y, z] is midPoint + someNumbers. If you add them together you are essentially doubling the midPoint vector(kinda). So if your dragon is at [200, 100, 200] you are telling it to move to ~[400, 250, 400]. I don't think that it is what you want and that is not what I told you. Quote
TheRPGAdventurer Posted November 28, 2018 Author Posted November 28, 2018 protected boolean tryToCircleBlockPos(BlockPos midPoint, double speed) { Vec3d vec1 = new Vec3d(midPoint.getX(),midPoint.getY(),midPoint.getZ()); Vec3d vec2 = dragon.getPositionVector(); // "calculate the angle between these 2 vectors" I think this im missing and im bad a t trigonometry double a = Math.acos((vec1.dotProduct(vec2)) / (vec1.lengthVector() * vec2.lengthVector())); double r = dragon.getDistanceSqToCenter(midPoint); // radius is now a double double x = midPoint.getX() + r * Math.cos(a); // x = playerX + r * cos(a) as stated double y = midPoint.getY() + 50; // y = playerY + NUM as stated double z = midPoint.getZ() + r * Math.sin(a); // z = playerZ + r * sin(a) as stated return dragon.getNavigator().tryMoveToXYZ(x, y, z, speed); // no more adding } Quote
V0idWa1k3r Posted November 28, 2018 Posted November 28, 2018 (edited) Well, the only thing that you are missing right now is On 11/27/2018 at 7:06 AM, V0idWa1k3r said: these vectors [dragonPos - playerPos], [unitZ] What I am saying is vec1 is [dragonPos - playerPos] and vec2 is [unitZ]. This 2 hours ago, TheRPGAdventurer said: double a = Math.acos((vec1.dotProduct(vec2)) / (vec1.lengthVector() * vec2.lengthVector())); Is the line that calculates the angle, you just need to get the vec1 and vec2 right. Also this 2 hours ago, TheRPGAdventurer said: double r = dragon.getDistanceSqToCenter(midPoint); is a squared distance, as the name of the method implies. Have your radius be a constant or something, this will make your dragon fly away from the player at an exponential rate. Edited November 28, 2018 by V0idWa1k3r Quote
TheRPGAdventurer Posted November 29, 2018 Author Posted November 29, 2018 (edited) what does unitZ mean? Edited November 29, 2018 by TheRPGAdventurer d Quote
TheRPGAdventurer Posted November 29, 2018 Author Posted November 29, 2018 what does unitZ mean? Quote
TheRPGAdventurer Posted November 29, 2018 Author Posted November 29, 2018 protected boolean tryToCircleBlockPos(BlockPos midPoint, double speed) { Vec3d vec1 = dragon.getPositionVector().subtract(midPoint.getX(),midPoint.getY(),midPoint.getZ()); Vec3d vec2 = dragon.getPositionVector(); // what does unitZ mean double a = Math.acos((vec1.dotProduct(vec2)) / (vec1.lengthVector() * vec2.lengthVector())); double r = 12; // radius is now a constant // double r = dragon.getDistance(midPoint.getX(),midPoint.getY(),midPoint.getZ()); can i use dis? double x = midPoint.getX() + r * Math.cos(a); // x = playerX + r * cos(a) as stated double y = midPoint.getY() + 20; // y = playerY + NUM as stated double z = midPoint.getZ() + r * Math.sin(a); // z = playerZ + r * sin(a) as stated return dragon.getNavigator().tryMoveToXYZ(x + 0.5, y + 0.5, z + 0.5, speed); // no more adding } Quote
V0idWa1k3r Posted November 29, 2018 Posted November 29, 2018 4 hours ago, TheRPGAdventurer said: what does unitZ mean? [0, 0, 1]. But I don't atually know what direction the game considers the forward one, so it might be some other unit vector. Quote
TheRPGAdventurer Posted November 29, 2018 Author Posted November 29, 2018 its z of course, in models z is forward or back ward, by [0,0,1] you mean; new Vec3d(0,0,1) or new BlockPos(0,0,1) Quote
V0idWa1k3r Posted November 29, 2018 Posted November 29, 2018 1 hour ago, TheRPGAdventurer said: new Vec3d(0,0,1) or new BlockPos(0,0,1) The only difference between these two is that the first one uses doubles while the second one uses ints. You need the double one. 1 hour ago, TheRPGAdventurer said: its z of course, in models z is forward or back ward It should be, but I wouldn't count on that. Quote
TheRPGAdventurer Posted November 29, 2018 Author Posted November 29, 2018 protected boolean tryToCircleBlockPos(BlockPos midPoint, double speed) { Vec3d vec1 = dragon.getPositionVector().subtract(midPoint.getX(),midPoint.getY(),midPoint.getZ()); Vec3d vec2 = new Vec3d(0,0,1); // "calculate the angle between these 2 vectors" I hope vec3d is compat with Blockpos Vec3i double a = Math.acos((vec1.dotProduct(vec2)) / (vec1.lengthVector() * vec2.lengthVector())); double r = 12; // radius is now a constant double x = midPoint.getX() + r * Math.cos(a); // x = playerX + r * cos(a) as stated double y = midPoint.getY() + 20; // y = playerY + NUM as stated double z = midPoint.getZ() + r * Math.sin(a); // z = playerZ + r * sin(a) as stated return dragon.getNavigator().tryMoveToXYZ(x + 0.5, y + 0.5, z + 0.5, speed); // no more adding } they do go to me, but they dont rotate on a position, am i doing something wrong, does this needs to be a static or a loop? Quote
Recommended Posts
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.