TransWikia.com

Rotating an entity 180 degrees around a specific point in 2D space

Arqade Asked by Camp bell on August 16, 2020

My goal is to rotate an entity around a fixed point by 180 degrees. The following is what I have tried so far:

Rotating an entity by x degrees around a point a known distance away is easy enough:

execute positioned ~ ~ ~10 facing entity @s feet rotated ~x ~ positioned ^ ^ ^10 run teleport @s ~ ~ ~

If, as in my case, you want to teleport by exactly 180 degrees it is even easier:

teleport @s ~ ~ ~20

However, I haven’t been able to come up with a simple method to rotate an entity around a fixed point in space. The above methods rely on knowing how far away the point around which to rotate is from the entity (in this case exactly 10 blocks down z, relative to the entity). To keep things simple, I stuck with just the xz plane.

The only working method I have found employs a systematic approach using AECs (again around the point 30.5 ~ 30.5):

#> the following is not optimized, purely for clarity's sake
summon area_effect_cloud 30.5 ~ 30.5 {Duration:2,Tags:["rot.origin"]}

# Calculate offset on each axis from Entity E to the AEC (looking down from above): 
#  E-------------
#  |            |
#  |-----------AEC------------|
#               |             |
#               |-------------E*
# Then move the AEC by the offset amount on each axis so it ends up at E*
# Finally, teleport E to E*

# get the offset from the entity to the AEC
execute store result score @s pos.x run data get entity @s Pos[0] 1000
execute store result score @s pos.z run data get entity @s Pos[2] 1000
scoreboard players set @e[tag=rot.origin] pos.x 30500
scoreboard players set @e[tag=rot.origin] pos.z 30500
scoreboard players set @e[tag=rot.origin] offset.x 30500
scoreboard players set @e[tag=rot.origin] offset.z 30500
scoreboard players operation @e[tag=rot.origin] offset.x -= @s pos.x
scoreboard players operation @e[tag=rot.origin] offset.z -= @s pos.z

# move AEC and teleport
scoreboard players operation @e[tag=rot.origin] pos.x += @s offset.x
scoreboard players operation @e[tag=rot.origin] pos.z += @s offset.z
execute as @e[tag=rot.origin] store result entity @s Pos[0] 0.001 run scoreboard players get @s pos.x
execute as @e[tag=rot.origin] store result entity @s Pos[2] 0.001 run scoreboard players get @s pos.z

teleport @s @e[tag=rot.origin,limit=1]
kill @e[tag=rot.origin]

This approach is undoubtedly slow, even after optimizing the selectors into separate functions.

The closest simple solution I found was the following: the entity rotates around the point 30.5 ~ 30.5; but it again relies on knowing the distance to the player (note the 10 block offset):

execute positioned 30.5 ~ 30.5 facing entity @s feet run teleport @s ^10 ^ ^

This is a bit of a mess of a post, I did my best to share everything I’ve tried so far. If anyone has any ideas on how to this in a simpler fashion I’d love to hear your thoughts. Here is a brief summary of my intentions: Rotate the entity…

  • …around a fixed, known point; like 30.5 ~ 30.5
  • …when the entity is an arbitrary distance away, be it 5, 10 or 1412.2222 blocks, from that point.

One Answer

I'm definitely not an expert in stuff like this so forgive me if this doesn't work, but you could potentially make the entity have an invisible armor stand with a tag riding it or on a /setblock and teleport the entity relative to the armor stand.

Answered by Blobman122 on August 16, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP