Spiral towards a point iteratively - three.js

I tried to move the object using
if(Math.abs(ay-y)>0.8)
{
cubes[i].position.y += 0.01*(ay-y);
}
else
{
cubes[i].position.y = (i_posy[i]-miny) + ay+Math.sin(t)*0.8;
}
but that doesn't get the
effect I want, red is the point of attraction, black is path to follow, any help appreciated.

Related

How to animate component repositioning on recomposition in Jetpack Compose?

I have two squares. The green one is constrained to the top, and the blue one is constrained between the green one and the bottom of the screen. When clicking on the blue square, the green square doubles in size. Since the size of the green square increases, the blue square moves downwards because of its constraints.
How do I animate the position change of the blue square? Do I have to manually calculate and animate the offset? This feels very hacky. I feel like this should be doable declaratively, since the new position is automatically calculated by the updated constraints.
Edit: Basically, I'm looking for android:animateLayoutChanges, but for Compose.
Here is my code:
#Composable
fun AnimationTest() {
ConstraintLayout(
modifier = Modifier.fillMaxSize()
) {
val (block1, block2) = createRefs()
var blockSize by remember { mutableStateOf(20.dp) }
Surface(
color = Color.Green,
modifier = Modifier
.size(blockSize)
.constrainAs(block1) {
top.linkTo(parent.top)
start.linkTo(parent.start)
end.linkTo(parent.end)
}
) {}
Surface(
color = Color.Blue,
modifier = Modifier
.size(50.dp)
.constrainAs(block2) {
top.linkTo(block1.bottom)
start.linkTo(parent.start)
end.linkTo(parent.end)
bottom.linkTo(parent.bottom)
}
.clickable {
blockSize *= 2
}
) {}
}
}
Try wrapping it inside the AnimatedContent composable. Though it is ConstraintLayout so I don't think it would work, but it just might. Give it a try and let me know. Otherwise, it is a very simple scenario, and can be easily achieved with the Layout composable, inside which you can use lerp() to achieve the animation

Unity3D: How do I improve my Idle animation (Y-axis)

Okay, I am almost finished with my 2d rpg click to move game. If you look at this video you will be able to see that when I click forward my player, once it gets to it's position, face the wrong direction rather than facing straight towards the players. To make myself more clearer, this is an image of the sprite sheet I am currently using, as you can see it has 8 directions. When you click here (in the game), my player would walk down and face this direction or this direction, rather than facing this direction (the normal/preferred position). This also happens when I click here (in the game) my player would walk up and face this direction or face this direction, rather than facing this direction. How can I make sure that my player face the right direction once it reached it's destination. Again this is only occurring within the Y-axis, so walking along the X-axis is fine.
private Animator anim;
public float speed = 15f;
private Vector3 target;
private bool touched;
private bool playerMovementRef;
void Start () {
target = transform.position;
anim = GetComponent<Animator> ();
}
void Update () {
if (Input.GetMouseButtonDown (0)) {
Vector3 mousePosition = Input.mousePosition;
mousePosition.z = 10; // distance from the camera
target = Camera.main.ScreenToWorldPoint (mousePosition);
target.z = transform.position.z;
var movementDirection = (target - transform.position).normalized;
Vector3 animDirection = Vector3.zero;
if (movementDirection.sqrMagnitude > 0)
{
// Use >= to default to horizontal on both being equal
if (movementDirection.x > movementDirection.y)
animDirection.x = 1;
else
animDirection.y = 1;
anim.SetBool ("walking", true);
anim.SetFloat ("SpeedX", movementDirection.x);
anim.SetFloat ("SpeedY", movementDirection.y);
if (movementDirection.x < 0) {
anim.SetFloat ("LastMoveX", -1f);
} else if (movementDirection.x > 0) {
anim.SetFloat ("LastMoveX", 1f);
} else {
anim.SetFloat ("LastMoveX", 0f);
}
if (movementDirection.y > 0) {
anim.SetFloat ("LastMoveY", 1f);
} else if (movementDirection.y < 0) {
anim.SetFloat ("LastMoveY", -1f);
} else {
anim.SetFloat ("LastMoveY", 0f);
}
}
} else {
if (Mathf.Approximately (transform.position.x, target.x) && Mathf.Approximately (transform.position.y, target.y)) {
touched = false;
anim.SetBool ("walking", false);
} else {
transform.position = Vector3.MoveTowards (transform.position, target, speed * Time.deltaTime);
}
}
}
}
I'm just starting out with Unity, but hope I can help.
From the video you've posted I've noticed that the sprite is only 'wrong' when the oldPosition and newPosition click differs in the X component, e.g. when you would click straight down it would show the desired behavior and sprite.
Have you tried printing out the x and y values that your code is using to calculate which sprite it's setting?
At first I thought that maybe it's because the values you set in the Blend Tree were at -1, 1 etc, and due to normalizing you sometimes wound up with 0.9 for a certain value.
Can you maybe try debugging it with the animator window open, like you did at the end? Writing down the values and comparing them between Desired and Undesired behavior might tell you something more.
Sorry I don't have a concrete solution to your problem, but I hope this helps.
Edit for clarification:
Basically, what I'm recommending is to:
1) Print out the values you are getting when the behavior happens, an example of how to print these things out is by using LogFormat, for example:
Debug.LogFormat("X: {0}, Y: {1}", xPosition, yPosition);
Now, you will get the values printed out when it finishes moving.
2) Write down the values for when the moving finishes with the 'Wrong' sprite, and keep clicking to move until the 'Right' sprite shows up. Write down the values again.
3) Compare each values, and find the differences. Now deduce why the differences are as they are, using the values in conjunction with your blend trees.
4) Once you know why, go back through your code/blend trees and rewrite/fix it to work as you intended it to.

Rotation between 2 points facing away when standing at back while facing towards while standing infront (pics)

With this little piece of code I've made it possible to rotate the 'monster' towards the avatar but if the avatar is behind the 'monster', the 'monster' is facing away from the avatar. (Pic's below)
Note: the white numbers are the value of m_RotationAngle
DOUBLE2 mousePos = GAME_ENGINE->GetMousePosition();
double xDiff = m_ActPtr->GetPosition().x - mousePos.x;
double yDiff = m_ActPtr->GetPosition().y - mousePos.y;
m_RotationAngle = atan(yDiff, xDiff);
m_ActPtr->SetAngle(m_RotationAngle);
I've tried to fix it with:
if (diff.x < 0)
{
m_RotationAngle = -atan(diff.y / diff.x);
//also tried following but gave and error:
//m_RotationAngle = tan(diff.y / diff.x);
}
else
{
m_RotationAngle = atan(diff.y / diff.x);
}
But this gave the following output:
You are probably looking for atan2(yDiff, xDiff); which computes the arc tangent of yDiff/xDiff using the signs of arguments to determine the correct quadrant, instead of atan (which also require only one parameter).
Be aware that the result is in the range [-π ; +π] radians, not degrees.

Qt 5.5 drawing a pointer

Is there a built in method for drawing an arrow/pointer at the end of a line in Qt?
I know I can draw it myself, but curious to know if there is already a way of doing this, I've search online and couldn't find anything.
I want to be able to rotate the line and the arrow rotates with it.
Below is the image I'm working on:
I would like to draw the arrow at the end of the dashed line, it should rotate with the dash line pointing around from the bottom left.
[edit] In the end I used a polygon and rotated that:
Code to add polygon needle:
Polygon clsMyClass::plygnGetNeedle(QPoint* pptOrigin
,int intRadius
,float fltAngle) {
const int cintNeedleHalfWidth = 4;
QPolygon plyNeedle;
int intX = pptOrigin->x(), intY = pptOrigin->y();
plyNeedle << QPoint(intX - cintNeedleHalfWidth, intY)
<< QPoint(intX, intY + cintNeedleHalfWidth)
<< QPoint(intX + intRadius - cintNeedleHalfWidth, intY)
<< QPoint(intX, intY- cintNeedleHalfWidth);
return QTransform().translate(intX, intY)
.rotate(fltAngle)
.translate(-intX, -intY)
.map(plyNeedle);
}
I don't recall something like this in Qt. As for the position and angle of the arrow - you can easily find those out if you use something like QPainterPath's angleAtPercent() and pointAtPercent() functions.

How do I correctly get control bound coordinates relitive to the control for mouse comparison?

I am presently learning C# and have chosen as a project to write a simple colour picker control ; however things have changed substantially since I last looked at writing code and I have struck this problem.
I am using the Mousedown event in my control to get the mouse coordinates as a Point - this is working fine and returns exactly what I would expect - the mouse coordinates relative to my control; however when I try and do a check against the control location I am returned a value as a Point showing the position of my control relative to the form which in certain cases the mouse coordinates will be outside the bounds of because their values will be less than the relative start position of the control I.E I click at pixel 1,1 in the control - the mouse position is 1,1 but because the control is located at 9,9 relative to the form the location of the mouse is less than the bounds of the control - I have absolutely no idea how to fix this, I have been attempting to sort it out with PointToClient and PointToScreen to no avail as they seem to come up with outlandish values can someone please help me out it's driving me insane.
I've managed to sort this out myself so thought I'd post the answer and it can hopefully help someone else. I was having problems getting Point values that were relative to the same pixel origin : this sorted it.
private void ColourPicker_MouseDown(object sender, MouseEventArgs e)
{ // Probably being paranoid but I am worried about scaling issues, this.Location
// would return the same result as this mess but may not handle
// scaling <I haven't checked>
Point ControlCoord = this.PointToClient(this.PointToScreen(this.Location));
int ControlXStartPosition = ControlCoord.X;
int ControlYStartPosition = ControlCoord.Y;
int ControlXCalculatedWidth = ((RectangleColumnsCount + 1) * WidthPerBlock ) + ControlXStartPosition;
int ControlYCalculatedHeight = ((RectangleRowsCount + 1) * HeightPerBlock) + ControlYStartPosition;
// Ensure that the mouse coordinates are comparible to the control coordinates for boundry checks.
Point ControlRelitiveMouseCoord = this.ParentForm.PointToClient(this.PointToScreen(e.Location));
int ControlRelitiveMouseXcoord = ControlRelitiveMouseCoord.X;
int ControlRelitiveMouseYcoord = ControlRelitiveMouseCoord.Y;
// Zero Relitive coordinates are used for caluculating the selected block location
int ZeroRelitiveXMouseCoord = e.X;
int ZeroRelitiveYMouseCoord = e.Y;
// Ensure we are in the CALCULATED boundries of the control as the control maybe bigger than the painted area on
// the design time form and we don't want to use unpaited area in our calculations.
if((ControlRelitiveMouseXcoord > ControlXStartPosition) && (ControlRelitiveMouseXcoord < ControlXCalculatedWidth))
{
if((ControlRelitiveMouseYcoord > ControlYStartPosition) && (ControlRelitiveMouseYcoord < ControlYCalculatedHeight))
{
SetEvaluatedColourFromPosition(ZeroRelitiveXMouseCoord, ZeroRelitiveYMouseCoord);
}
}
}

Resources