I have a top down game. The player moves the avatar with WASD where W rotates the avator to 180, S rotates to 0, A rotates to 90 and D rotates to 270. I also do diagonal movement so if they hold SA it rotates to 45, WA it rotates to 135, WD it rotates to 225, and SD it rotates to 315.  I smoothly rotate from the current rotation to the hardcoded desired rotation based on those key combo's. Of course what happens is if I'm rotating from anything > 180 to 0 it goes the long way back to 0. So if I'm at 270 rotation and I press S key it sends it back to 0 but that goes from 270 to 0 vs going from 270 to 360 which would result in the same end result but how it got there would be shorter.  I was looking at this () and trying to translate it but it's not working. Below is my translation.  function Script:CalcShortestRot(current, target)     --if current == target then return from end      local dist = target - current + 180     dist = dist - math.floor(dist / 2 / 180) * 2 * 180 - 180      System:Print("Dist = "..dist)      if dist &lt; 0 then         System:Print("Value 1 = "..target - current)         return current - dist     else         System:Print("Value 2 = "..current + target)         return current + dist     end end   The usage is:  -- determine the model rotation based on direction we are going     if move &gt; 0 then         if strafe &gt; 0 then             self.targetRot = self:CalcShortestRot(self.targetRot, 225)             --self.targetRot = 225         elseif strafe &lt; 0 then             --self.targetRot = 135             self.targetRot = self:CalcShortestRot(self.targetRot, 135)         else             --self.targetRot = 180             self.targetRot = self:CalcShortestRot(self.targetRot, 180)         end     elseif move &lt; 0 then         if strafe &gt; 0 then             --self.targetRot = 315             self.targetRot = self:CalcShortestRot(self.targetRot, 315)         elseif strafe &lt; 0 then             --self.targetRot = 45             self.targetRot = self:CalcShortestRot(self.targetRot, 45)         else             --self.targetRot = 0             self.targetRot = self:CalcShortestRot(self.targetRot, 0)         end     elseif strafe &gt; 0 then         --self.targetRot = 270         self.targetRot = self:CalcShortestRot(self.targetRot, 270)     elseif strafe &lt; 0 then         --self.targetRot = 90         self.targetRot = self:CalcShortestRot(self.targetRot, 90)     end      self.rot = Math:Curve(self.targetRot, self.rot, self.damping)   EDIT: updated my CalcShortestRot() function per comments below but still not working. It spins all around
122816
Below is a section of my code which handles a player's sprite rotating towards an angle after a user touches the screen:  touchState = TouchPanel.GetState(); Vector2 touchPosition;  if (touchState.Count &gt; 0) {    touchPosition = new Vector2(touchState[0].Position.X, touchState[0].Position.Y);    targetPosition = Math.Atan2(player.Position.X - touchPosition.X, player.Position.Y - touchPosition.Y);  if (angle_radians &lt; targetPosition) {     angle_radians += 2 * fps; }  if(angle_radians &gt; targetPosition) {     angle_radians -= 2 * fps; }  player.Angle = angle_radians * -1; }   The problem I'm having is that when the angle goes past a certain point (I believe 3.15 radians?), the logic no longer functions correctly and the sprite reverses direction in a 360 circle until it meets the target position again.  I know there is something I'm missing from this logic, and I can see the problem, but I'm not sure how to approach handling it.  How would I prevent the sprite from reversing direction?  Thanks  EDIT: I tried the solution below but I only receive a positive number from 0-6 for angle_distance, what have I done wrong?              if (touchState.Count &gt; 0)             {                 touchPosition = new Vector2(touchState[0].Position.X, touchState[0].Position.Y);                 targetPosition = Math.Atan2(player.Position.X - touchPosition.X, player.Position.Y - touchPosition.Y);                 angle_distance = targetPosition - angle_radians + Math.PI;                 angle_distance = angle_distance - Math.Floor(angle_distance / 2 / Math.PI) * 2 * Math.PI;                  if (angle_distance &lt; 0)                 {                     angle_radians -= 2 * fps;                 }                  if (angle_distance &gt; 0)                 {                     angle_radians += 2 * fps;                 }                  player.Angle = angle_radians * -1;             }   EDIT: I now realise my mistake, correct solution is below  if (touchState.Count &gt; 0)                 {                     touchPosition = new Vector2(touchState[0].Position.X, touchState[0].Position.Y);                     targetPosition = Math.Atan2(player.Position.X - touchPosition.X, player.Position.Y - touchPosition.Y);                      angle_distance = targetPosition - angle_radians + Math.PI;                     angle_distance = angle_distance - Math.Floor(angle_distance / 2 / Math.PI) * 2 * Math.PI;                      if (angle_distance &lt; Math.PI)                     {                         angle_radians -= 2 * fps;                     }                      if (angle_distance &gt; Math.PI)                     {                         angle_radians += 2 * fps;                     }                      player.Angle = angle_radians * -1;                 }
108419
I'm building an MMO using Node.js, and there are monsters roaming around. I can make them move around on the server using vector variables acceleration, velocity and position.  acceleration = steeringForce / mass;  velocity += acceleration * dTime;  position += velocity * dTime;  Right now I just send the positions over, and tell the players these are the "target positions" of the monsters, and let the monsters move towards the target positions on the client with a speed dependant on the distance of the target position. It works but looks rather strange.  How do I synchronise these properly with the players without looking funny to them, taking into account the server lag?  The problem is that I don't know how to make use of the correct acceleration/velocity values here; right now they just move directly in a straight line to the target position instead of accelerating/braking there properly. How can I implement such behaviour?
49041
I am trying to scale an object along the Y Axis (flipping it by -1) but I am having trouble. When I scale it by Y, it seems to be a semi-arbitrary axis. The 3D cursor shows that the object's orientation is not aligned with the world, but I want to scale it on the world's Y Axis. I've tried clearing and applying rotation but it doesn't seem to be doing what I expect. I would think that clear rotation would be what I'm looking for, but it doesn't seem to work. I want the object to stay in the same exact world location but I want its rotation to be 0,0,0. How can I zero the object's rotation without affecting its actual position/rotation?  Thanks, Warren
66435
When I create something in blender and than I export it to unity as an fbx, it is rotated -90 degrees on the x axis to keep it correct rotation but can I export it with (0,0,0) rotation by default ? So it will be correctly rotated and its rotation would be (0,0,0).
71392
In general I know that if we rotate $(x, y)$  about origin through $180^\circ$ we  will get new image $(-x, -y)$, but suppose that we make rotation not about origin but some other point $(a, b)$ does your result be rotation  around origin + or -  $(a, b)$? Suppose we have point $A(3, 27)$ and we want  turn it by $180$   around the point $(2, -1)$,  if  we rotate $(3, 27)$ about origin by $180$ we get $(-3, -27)$ but how  to connect $(2, -1)$ to this result?
26515
I have written some code that calculates the world space coordinate I want to move the bone to but i am having trouble figuring out how to get that back into the bones coordinates.  from bpy import context, data from mathutils import Vector  def test_empty(matrix, name):     # view matrix by applying it to an object     obj_empty = data.objects.new(name, None)     context.scene.objects.link(obj_empty)     obj_empty.matrix_world = matrix   armature = data.objects["Armature"] pbones = armature.pose.bones root = pbones["root"] hips = pbones["hips"] test = pbones["test"]  # calculate root root_matrix = armature.matrix_world * root.matrix test_empty(root_matrix, "Root")  # calculate hips hips_matrix = armature.matrix_world * hips.matrix test_empty(hips_matrix, "Hips")  # calculate bone test_matrix = armature.matrix_world * test.matrix test_empty(test_matrix, "Test")  # get offset of hips from root bone offset = root_matrix.translation - hips_matrix.translation print(offset)  # calculate new location in local space new_position = test_matrix.translation + Vector((0,offset.y,0))  obj_empty = data.objects.new("TEST", None) context.scene.objects.link(obj_empty) obj_empty.location = new_position  # obviously something wrong here new_position = armature.matrix_world.inverted() * new_position  # move bone to new location test.location = new_position
3115
I see questions come up quite often that have this underlying issue, but they're all caught up in the particulars of a given feature or tool. Here's an attempt to create a canonical answer we can refer users to when this comes up - with lots of animated examples! :)    Let's say we're making a first-person camera. The basic idea is it should yaw to look left &amp; right, and pitch to look up &amp; down. So we write a bit of code like this (using Unity as an example):  void Update() {     float speed = lookSpeed * Time.deltaTime;      // Yaw around the y axis using the player's horizontal input.             transform.Rotate(0f, Input.GetAxis("Horizontal") * speed, 0f);      // Pitch around the x axis using the player's vertical input.     transform.Rotate(-Input.GetAxis("Vertical") * speed,  0f, 0f); }   or maybe  // Construct a quaternion or a matrix representing incremental camera rotation. Quaternion rotation = Quaternion.Euler(                         -Input.GetAxis("Vertical") * speed,                          Input.GetAxis("Horizontal") * speed,                          0);  // Fold this change into the camera's current rotation. transform.rotation *= rotation;   And it mostly works, but over time the view starts to get crooked. The camera seems to be turning on its roll axis (z) even though we only told it to rotate on the x and y!    This can also happen if we're trying to manipulate an object in front of the camera - say it's a globe we want to turn to look around:    The same problem - after a while the North pole starts to wander away to the left or right. We're giving input on two axes but we're getting this confusing rotation on a third. And it happens whether we apply all our rotations around the object's local axes or the world's global axes.  In many engines you'll also see this in the inspector - rotate the object in the world, and suddenly numbers change on an axis we didn't even touch!    So, is this an engine bug? How do we tell the program we don't want it adding extra rotation?  Does it have something to do with Euler angles? Should I use Quaternions or Rotation Matrices or Basis Vectors instead?
61131
I'm trying to use blender to test the probabilities associated with different faces of non-cubic dice (some of you may even know why). I've already written a python script that randomly sets the objects position, rotation, initial linear velocity and initial angular velocity, then bakes the physics and jumps to a frame where the shape is likely in a settled position. The problem is that the rotation data at this frame seems to be the same as the frame right before the physics engine took over. It looks like the physics engine doesn't operate directly on object transformations. Is there a way to get this rotation information from python? Right now I can't even seem to find it in the UI.
54026
I have the following code and I expect the resulting angle to be 135 degrees but whatevet I try, I get 90.  \documentclass{article} \usepackage{tikz} \usetikzlibrary{calc} \begin{document} \begin{tikzpicture}  \coordinate (A) at (1,0); \coordinate (B) at (-1,1);  \pgfmathanglebetweenpoints{A}{B} \edef\angleAB{\pgfmathresult} \node at (0,0) {\angleAB};  \end{tikzpicture} \end{document}   I also tried the following command without knowing what it means, it also prints 90 except one case. The only working case is when A=(1,0) and B=(-1,0), which it prints 180.  \pgfmathanglebetweenpoints{\pgfpointanchor{A}{center}}{\pgfpointanchor{B}{center}}   Why is that? Couldn't find any reference document for "pgfmathanglebetweenpoints" by the way, that's why I am asking here.  EDIT: B is corrected from (-1,-1) to (-1,1)
28083
So I have this script:  #!/bin/bash # Flips the screen (hopefully)  syntax_error=0 orientation=0  current_orientation="$(xrandr -q --verbose | grep 'connected' | egrep -o  '\) (normal|left|inverted|right) \(' | egrep -o '(normal|left|inverted|right)')" case $current_orientation in         normal)                 current_orientation=0         ;;         left)                 current_orientation=1         ;;         inverted)                 current_orientation=2         ;;         right)                 current_orientation=3         ;; esac  if [ $current_orientation -eq 0 ]; then         orientation=2 fi  if [ $current_orientation -eq 2 ]; then         orientation=0 fi method=evdev  # LENOVO S10-3t CHANGE ==&gt; Hard Coded my device number to 11!!!!!!!!  device=11  swap=0 invert_x=0 invert_y=0 real_topx=0 real_topy=0 real_bottomx=4020 real_bottomy=4020  case $orientation in         0)                 swap=0                 invert_x=0                 invert_y=0                 topx=$real_topx                 topy=$real_topy                 bottomx=$real_bottomx                 bottomy=$real_bottomy         ;;         1)                 swap=1                 invert_x=1                 invert_y=0                 topx=$real_topx                 topy=$real_topy                 bottomx=$real_bottomy                 bottomy=$real_bottomx         ;;         2 )                 swap=0                 invert_x=1                 invert_y=1                 topx=$real_topx                 topy=$real_topy                 bottomx=$real_bottomx                 bottomy=$real_bottomy         ;;         3 )                 swap=1                 invert_x=0                 invert_y=1                 topx=$real_topx                 topy=$real_topy                 bottomx=$real_bottomy                 bottomy=$real_bottomx         ;; esac  if [ $method = "evdev" ]; then         xinput set-prop "$device" "Evdev Axes Swap" $swap         xinput set-prop "$device" "Evdev Axes Swap" $swap         xinput set-prop "$device" "Evdev Axis Inversion" $invert_x $invert_y         xinput set-prop "$device" "Evdev Axis Calibration" $topx $bottomx $topy $bottomy         if [ $orientation = 2 ]; then                            xrandr -o inverted         fi         if [ $orientation = 0 ]; then                 xrandr -o normal         fi fi  #   It's for flipping screen in my Lenovo S10-3t. I copied it from the netbook's wiki page, and added the #!/bin/bash at the top. The filename is flipscreen.sh. How can I make it work?
55203
I'm trying to make the joint rotate smoothly around the center of the canvas, toward the angle of mouse pointer. What I have works, but I want it to animate the shortest distance possible to get to the mouse angle. The problem occurs when the value loops around at the horizontal line (3.14 and -3.14). Mouseover that area to see how the direction switches and it takes the long way back around.  Relevant Code   // ease the current angle to the target angle joint.angle += ( joint.targetAngle - joint.angle ) * 0.1;  // get angle from joint to mouse var dx = e.clientX - joint.x,     dy = e.clientY - joint.y;   joint.targetAngle = Math.atan2( dy, dx );   How can I make it rotate the shortest distance, even "across the gap"?
4260
I'm writing a python script that creates a large number of objects and I'd like them all to point at a certain point in 3d space.  (left is what I've got, right is what I want)   Pretty much exactly like the 'Track-to' constraint, but I don't need it to update interactively or be animated, I just need to rotate each object when they are created. I also don't care about the up-direction at all, since the objects are cylindrically symmetrical.  I know how to create the objects, but I don't know how to aim them. I'm not good at maths, so I don't even know where to begin with this other than that it'll probably involve vectors and matrices (which I don't really understand).  The reason I don't want to use the existing constraint system is because it's a bit slow when dealing with a lot of objects, and I'd like to learn a bit about this sort of maths.
18916
I'm learning Go and I wrote this for a programming challenge. It is working (building and running) but I feel the code is not what Go code should be.  I used an OOP design. Is it correctly implemented in GO ?   In my main, I will call the method pod.playTurn(target, speed)  package main  import (     "fmt"     "math"     "os"     "strconv" ) // Dot is king type Dot struct {     x, y int } // Pod is life type Pod struct {     position                Dot     vx, vy, angle, nextCpID int     hasShieldOn             bool }  func squaredDist(a, b Dot) int {     return (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y) } func distance(a, b Dot) float64 {     return math.Sqrt(float64(squaredDist(a, b))) }  func (pod Pod) getAngle(p Dot) float64 {     d := distance(p, pod.position)     dx := float64(p.x-pod.position.x) / d     dy := float64(p.y-pod.position.y) / d     a := math.Acos(dx) * 180.0 / math.Pi      // If the point I want is below me, I have to shift the angle for it to be correct     if dy &lt; 0 {         a = 360.0 - a     }     return a }  func (pod Pod) diffAngle(p Dot) float64 {     a := pod.getAngle(p)     pangle := float64(pod.angle)      right := 0.0     if pangle &lt;= a {         right = a - pangle     } else {         right = 360.0 - pangle + a     }      left := 0.0     if pangle &gt;= a {         left = pangle - a     } else {         left = pangle + 360.0 - a     }      if right &lt; left {         return right     }     return -left }  func (pod Pod) rotate(p Dot) int {     a := pod.diffAngle(p)      // Can't turn more than 18° in one turn !     if a &gt; 18.0 {         a = 18.0     } else if a &lt; -18.0 {         a = -18.0     }      pod.angle += int(math.Round(a))      if pod.angle &gt;= 360.0 {         pod.angle = pod.angle - 360.0     } else if pod.angle &lt; 0.0 {         pod.angle += 360.0     }     return pod.angle }  func (pod Pod) boost(t int) (int, int) {     if pod.hasShieldOn {         return pod.vx, pod.vy     }     pangle := float64(pod.angle)     pod.vx += int(math.Round(math.Cos(pangle) * float64(t)))     pod.vy += int(math.Round(math.Sin(pangle) * float64(t)))     return pod.vx, pod.vy }  // t shoud become a float later on func (pod Pod) move(t int) (int, int) {     pod.position.x += pod.vx * t     pod.position.y += pod.vy * t     return pod.position.x, pod.position.y }  func (pod Pod) endTurn() (int, int) {     // todo rounding position if needed     pod.vx = int(float64(pod.vx) * 0.85)     pod.vy = int(float64(pod.vy) * 0.85)     return pod.vx, pod.vy }  func (pod Pod) playTurn(p Dot, t int) {     pod.angle = pod.rotate(p)     pod.vx, pod.vy = pod.boost(t)     pod.position.x, pod.position.y = pod.move(1)     pod.vx, pod.vy = pod.endTurn()     fmt.Fprintf(os.Stderr, "\nPredicted Pod position : ")     fmt.Fprintf(os.Stderr, "\n(%d, %d) speed (%d,%d)", pod.position.x, pod.position.y, pod.vx, pod.vy) }   Am I using struct correctly ? Is it acceptable object oriented code ?
101123
Right now, when I spawn a shootable object, it goes like this:    Because I just set it's angle to this:  float aimAngle = (float) Math.atan2(velocity.y, velocity.x); bullet.setBodyAngle(aimAngle);   I want it to be like this:    I think I should use angular velocity or torque, but I have no idea how. I can't just set it's angle like in previous code every frame, because it makes physics collisions glitch.
25027
I can't seem to figure this out. I am trying to testfor if a player is near certain coordiantes, namely {-9, 100, 16}, and then to teleport them somewhere else.  In earlier versions it would be /testfor @a[x=-9,y=100,z=16,r=2] in a repeating command block going into a chain conditional command block that says /tp @p ~8 ~ ~, although it seems in 1.13 they have removed /testfor. I've seen that apparently I'm supposed to use /execute now in some way but I'm not sure how.
87246
I'm using this code in my script to move a gameobject:  void Update () {      // Move the object forward along its z axis 1 unit/second.     transform.position = new Vector2(transform.position.x , transform.position.y - 0.2f);  }   On screen the movement is a bit jaggy and not fluent.  Is there a different approach?  Hint: Please do not suggestion to use rigidbody functions since adding rigidbody would disrupt everything I did.  Can I use FxedUpdate?  Mirza
47935
I need to convert lat/long that is expressed as degrees, minutes, and seconds in the data into decimal degrees. For example, in the data, they are listed as N335042.06 in the Latitude column, and W86031.04 in the Longitude column. I have done this problem before where I created a script which converted DMS to DD, and vice-versa, so I guess I could use bits from that. But the problem I'm having is how to ignore (for lack of a better word) the 'N' and 'W' in the data? Can I skip them? And DMS are listed all together without any symbols or spaces.  Can I use len(), range(), split() to specify what part to read from the value? For example, can do the following?  N335042.06 where, 33 = degrees 50 = minutes 42.06 = seconds ...?  I came across  ESRI article, but it's in VB. Will probably use it as a reference but some of the terminology/syntax is different from Python.  Final code that works!   # Pre-logic def latDD(x):   D = int(x[1:3])   M = int(x[3:5])   S = float(x[5:])   DD = D + float(M)/60 + float(S)/3600   return DD  # Expression latDD(!Latitude!)