Yaw pitch rotation issues - math

am coding some stuff which should intercept target correctly like turret,cannon.
All calculated correctly but while enemy is moving to left or right side of screen (also up and down) my bot cant shoot them correctly, center is fine. Here little example whats happens with wrong yaw issue, same with pitch https://i.stack.imgur.com/GYGNV.png
Mission is save bot rotation as is, and fix missfire issues.
As main concept i taken camera position and enemy center
Vector2 Rotate(Vector3 Calc)
{
Vector2 coord;
float Yaw = (float)(atan2(Calc.y, Calc.x)) * 180.0f / M_PI;
float Pitch = (float)(atan2(Calc.z, sqrtf(Calc.x * Calc.x + Calc.y * Calc.y))) * 180.0f / M_PI;
coord.x = Pitch;
coord.y = Yaw;
return coord;
}
Vector3 Diff = Target - Bot;
Vector2 Angle = Rotate(Diff);

Related

2d physics ball bouncing and rolling on incline response

I asked this question before but it got closed because it said I was asking for recommendations which isn't allowed.
Really I'm just looking for a solution whether it's from a book,website or someone who knows how to do this.
I progressed somewhat since I asked the question the last time, but anyways:
So far I've got a program where you can spawn balls which can collide with other balls and bounce off the window boundaries just fine, you can also draw a linestrip (am using sfml as I'm just a beginner and it requires very little setting up before you can get going with coding), so you hold down the mouse and every however many pixels it'll set the line you're currently drawing and start a new one at the end, so I can either do straight edges or change the setting to make a new line every pixel and make very ugly curves with the mouse (need to figure out how to do splines).
It makes no difference for the collision as it finds the closest point on the line every frame.
So dropping the ball onto the incline is fine, it bounces off like it should, or even several times,
but getting it to roll is a different story, even if you start the ball on the incline with 0 velocity as soon as it starts colliding it gets sent up off the incline instead of moving along it, and starts doing tiny bounces.
The only solution I found, which is terrible, is to time each bounce (or height of bounce I guess) and when its low enough to stop it bouncing and only have gravity act on it.
I'd be happy with it except what happens is it will bounce lower and lower until it reaches the
threshold, at which point it visibly slows down before it starts accelerating again rolling down the incline, which looks really bad, like it comes to a halt and starts getting pulled along.
Ideally I'd like it to actually simulate the physical forces rather than having to resort to tricks like timing the bounces.
While I was writing this I put in some code to work out what the angle is that the ball hits the incline at and I think the problem lies there, it's fine if it hits it from free fall but when rolling the angle shows as about 67-72 degrees when it should be..0 i guess?
Any help appreciated.
Here's the code for the collision.
void Particle::collideRamp(sf::Vector2f cp,sf::Vector2f l1,sf::Vector2f l2) {
//distance between ball and closest point on line
float dx = x - cp.x;
float dy = y - cp.y;
float distance = hypot(dx, dy);
//find line normal
float lx = l2.x - l1.x;
float ly = l2.y - l1.y;
sf::Vector2f LineNormal = normalize(ly, -lx);
//make velocity and projection same length so can mirror velocity against normal
sf::Vector2f normalMag(velocity.x * LineNormal.x,velocity.y * LineNormal.y);
sf::Vector2f Projection = LineNormal * hypotf(normalMag.x, normalMag.y);
sf::Vector2f vel = normalize(velocity.x, velocity.y) * hypotf(normalMag.x, normalMag.y);
//working on making circles rotate but early days
float rsx = prevx - x;
float rsy = prevy - y;
float rSpeed = hypot(rsx, rsy) / circumference;
//work out gravity forces for the incline, sin#mg down incline etc.
sf::Vector2f gravPerpendicular = normalize(-ly, lx);
sf::Vector2f gravParallell = normalize(lx, ly);
float gravPerMag;
float gravParMag;
gravParMag = sin(M_halfPI + atan2f(gravParallell.x,gravParallell.y));
gravPerMag = cos(M_halfPI + atan2f(gravParallell.x,gravParallell.y));
//// Collision detected.
if (distance < (radius)) {
//work out angle the ball struck the incline
float iAngle = atan2f(lx, ly) + atan2f(velocity.x, velocity.y);
//cout << iAngle * 180 / M_PI << " aft " << endl;
//make sure its 0-90
if (iAngle > 1.5708)
iAngle = 1.5708 - (iAngle - 1.5708);
//move the ball back if it went past the line by the amount it passed it by
sf::Vector2f v = normalize(velocity.x + forceAcc.x, velocity.y + forceAcc.y);
float overlap = (radius - distance );
x -= v.x * overlap;
y -= v.y * overlap;
rotationSpeed = rSpeed;
//messing with changing the angle only if bounce is a certain height as this is what's causing the
//problem i think, however the ball slows down before it starts rolling making it look weird
if (collClock.getElapsedTime().asSeconds() > 0.01) {
sf::Vector2f newVel = ((velocity + Projection) + Projection);
forceAcc = newVel;
float e = elasticity * elasticity;
forceAcc *= e;
velocity = sf::Vector2f(0, 0);
}
//add gravity forces, since the force that goes along the line normal is cancelled out
//by an equal force from the line I guess it doesn't need to be added?
//accelerateI(sf::Vector2f(-sin(gravPerpendicular) * gravPerMag * gravity * mass, cos(gravPerpendicular) * gravPerMag * gravity * mass));
//accelerate(sf::Vector2f(sin(gravPerpendicular) * gravPerMag * gravity * mass, -cos(gravPerpendicular) * gravPerMag * gravity * mass));
//this one rolls it down the incline
accelerateI(sf::Vector2f(gravParallell.x * gravParMag * gravity * mass, gravParallell.y * gravParMag * gravity * mass ));
//trying wether subtracting gravity helps
//accelerateI(sf::Vector2f(0, -1 * gravity * mass));
//friction
//accelerateI(sf::Vector2f(gravParallell.x * forceAcc.x * friction * mass, gravParallell.y * forceAcc.y * friction * mass));
collClock.restart();
}
}
Thanks guys

Rotating a QGraphicsItem and finding the new position of it

I am working on a tower defence game and trying to shoot a projectile from a turret. The sprite of the turret I have looks like this
Also, the bullet looks like this
What I want to do is make the turret shot the bullet to a certain point (for example attackDestination = QPointF(1000, 500);
for this, I have a class Bullet, with the slot move:
void Bullet::move()
{
int stepSize = 20; // how fast the bullet moves
double angle = rotation();
double dy = stepSize * qSin(qDegreesToRadians(angle)); // The X that needs to be moved
double dx = stepSize * qCos(qDegreesToRadians(angle)); // The Y that needs to be moved
setPos(x() + dx, y() + dy);
}
which is triggered by a QTimer.
I also have a slot in the Tower class (which stands for the turret)
void Tower::attackTarget()
{
Bullet *bullet = new Bullet();
//getWidthMap() returns the width of the tower
//getHeightMap() returns the height of the tower
bullet->setPos(x() + getWidthMap() /2, y());
QLineF line(QPointF(x() + getWidthMap() /2, y()), attackDestination);
double angle =(-1) * line.angle();
bullet->setRotation(angle);
this->setRotation(90 + angle);
game->scene->addItem(bullet);
}
I have rotated the turret by +90 degrees because its initial position is vertical and it needs to form an angle (that of the line with oX) just like the bullet. The rotation happens clockwise.
The problem is with the position of the bullet relative to the turret when attacking.
Without the line this->setRotation(90 + angle); (first picture), with it (second picture)
As you can see, the bullets are starting from the initial position of the turret (when it was not rotated), because the pos() function keeps the initial X and Y. How can I fix that ?

When rotating 2D sprite towards cursor via angularVelocity, it spins at one point

Intro
I've created a spaceship sprite in my Unity project, I wanted it to rotate towards the cursor via angular velocity, because I'd like make my game to be heavily physics based.
Problem
Now my problem with rotating the sprite via by angular velocity is the following:
At -180° / 180° rotation my ship spins around, because while my mouse's angle is already 180°, while my ship's rotation is still -180°, or the other way around.
I tried
I tried to solve it mathematically, wasn't too successful, I could make it spin the right way just much slower/faster, I could fix the 180/-180 point, but made two different ones instead.
Looked for different solutions, but couldn't find a more fitting one.
Code
So I have this code for the rotation:
// Use this for initialization
void Start () {
rb = gameObject.GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update () {
//getting mouse position in world units
mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
//getting the angle of the ship -> cursor vector
angle = Mathf.Atan2(mousePos.y - transform.position.y, mousePos.x - transform.position.x) * Mathf.Rad2Deg;
//getting the angle between the ship -> cursor and the rigidbody.rotation vector
diffAngle = angle - (rb.rotation + 90);
//Increasing angular velocity scaling with the diffAngle
rb.angularVelocity = diffAngle * Time.deltaTime * PlayerShipStats.Instance.speed * 100f;
Thank you for your contribution in advance
Solution for Problem 1
Inserting this code made it work, not for long :
if(diffAngle > 180) {
diffAngle -= 360;
} else if (diffAngle < -180) {
diffAngle += 360;
}
Problem 2 and Solution for Problem 2
The new problem is:
rigidbody.rotation can exceed it's boundaries, it can be rotated for more than 360 degrees.
this code patched this bug:
if(rb.rotation + 90 >= 180) {
rb.rotation = -270;
} else if (rb.rotation + 90 <= -180) {
rb.rotation = 90;
}
The perfect code
void AimAtTarget(Vector2 target, float aimSpeed) {
//getting the angle of the this -> target vector
float targetAngle = Mathf.Atan2(target.y - transform.position.y, target.x - transform.position.x) * Mathf.Rad2Deg;
if (rb.rotation + 90 >= 180) {
rb.rotation = -270;
} else if (rb.rotation + 90 <= -180) {
rb.rotation = 90;
}
//getting the angle between the this -> target and the rigidbody.rotation vector
float diffAngle = targetAngle - (rb.rotation - 90);
if (diffAngle > 180) {
diffAngle -= 360;
} else if (diffAngle < -180) {
diffAngle += 360;
}
//Increasing angular velocity scaling with the diffAngle
rb.angularVelocity = diffAngle * Time.deltaTime * aimSpeed * 100;
}
There are two problems I see here:
Problem 1
angle is always going to be between -180 and 180, while rb.rotation is between 0 and 360. So you are comparing angles using two different notations. The first step is to get both angles returning -180 to 180 or 0 to 360. I chose to do the following which puts both angles between -180 and 180:
//getting the angle of the ship -> cursor vector
float targetAngle = Mathf.Atan2(
mousePos.y - transform.position.y,
mousePos.x - transform.position.x) * Mathf.Rad2Deg;
//get the current angle of the ship
float sourceAngle = Mathf.Atan2(
this.transform.up.y,
this.transform.up.x) * Mathf.Rad2Deg;
Problem 2
If you fix problem 1 and tried your app you would notice that the ship sometimes rotates the wrong way, although it will eventually get to its target. The problem is that diffAngle can sometimes give a result that is greater than +180 degrees (or less than -180). When this happens we actually want the ship to rotate the other direction. That code looks like this:
//getting the angle between the ship -> cursor and the rigidbody.rotation vector
float diffAngle = targetAngle - sourceAngle;
//use the smaller of the two angles to ensure we always turn the correct way
if (Mathf.Abs(diffAngle) > 180f)
{
diffAngle = sourceAngle - targetAngle;
}
I made a simple Unity to verify this works. I was able to rotate my ship in either direction smoothly.
One thing you may have to handle, if you don't already, is appropriately stopping the rotation of the ship when the it is facing the cursor. In my test I noticed that the ship would jitter slightly when it reached its target because it would (very) slightly overshoot the cursor's angle in one direction and then the other. The larger the value of PlayerShipStats.Instance.speed the more pronounced this effect will likely be.

Multi direction gravity 2D Platform Game - trigonometry issue

So, like the title said, I'm making a 2D platform game with multi direction physics, but my lack of trigonometry skills are killing me.
When my character hits something, it returns a Vector2 hitPoint.
If this was a one-way gravity platform game, I would do the following:
// considering the player's origin point in the middle
public void HitGround(Vector2 hitPoint)
{
this.position.y = hitPoint.y + height /2;
}
But since this have multi gravity physics, this simple method can't be done. So I've worked with relative positions, suppose the gravity is (-3, -3), the player would fall southwest.
// considering the player's origin point in the middle
public void HitGround(Vector2 hitPoint)
{
this.position = hitPoint + GetRelativeYPos(height / 2);
}
// returns the relative Y position based on its rotation
public Vector2 GetRelativeYPos(float offset)
{
// rotation is a Quaternion
return this.rotation * new Vector2(0, 1) * offset;
}
So far, so good. It works perfectly!!! But wait... this is only working when the CENTER of the player hits the ground. In fact, the player has 3 raycasts when he is falling. If the center raycast hits the ground, the code WORKS in EVERY direction he falls.
But when the EDGE RAYCASTS hits the ground. The Player tilts to the center, because the code do not allow him to move right. Now, my last code idea was born:
public void HitGround(Vector2 hitPoint, Vector2 raycastOrigin)
{
// Considering that the raycastOrigin is in the same "relative X" as the player's origin
float signedDistance = SignedDistance(hitPoint, raycastOrigin);
this.position = hitPoint + GetRelativeYPos(height / 2) + GetRelativeXPos(signedDistance);
}
// returns the relative Y position based on its rotation
public Vector2 GetRelativeYPos(float offset)
{
return this.rotation * new Vector2(0, 1) * offset;
}
// returns the relative X position based on its rotation
public Vector2 GetRelativeXPos(float offset)
{
return this.rotation * new Vector2(1, 0) * offset;
}
public float SignedDistance(Vector2 p1, Vector2 p2)
{
float distance = Distance(p1, p2);
float angle = Atan2(p2.y, p2.x) + Atan2(p1.y, p1.x);
// Returns the magnitude AND direction of the vector
return distance * Sign(angle);
}
And there you go!
When the code doesn't work, its just because of the sign of the angle, that, for some reason, it's being calculated wrong, When the angle has to be positive, it returns negative, and vice versa, but it only returns the wrong value when the "player's relative north position" is below 0.
For example:
if the player is falling down, when the Y position is higher than 0, it works, when it is lower, it doesn't.
If the player is falling up, when the Y position is lower than 0, it works, when it is higher, it doesn't.
If the player is falling right, when the X position is lower than 0, it works, when it is
higher, it doesn't.
If the player is falling left, when the X position is greater than 0, it works, when it is lower, it doesn't.
Just one more:
If the player is falling southeast, when the Y position is higher than 0 AND the X position is lower than 0, it works, otherwise, it doesn't
The following method is the responsible for calculating the "signed distance" of two points. I mean, the magnitude of the resulting vector and its direction
public float SignedDistance(Vector2 p1, Vector2 p2)
{
float distance = Distance(p1, p2);
// THIS LINE HERE!!!
float angle = Atan2(p2.y, p2.x) + Atan2(p1.y, p1.x);
return distance * Sign(angle);
}
There is something wrong with that line that I CAN'T FIGURE IT OUT. Is there someone kind enough to answer this question?
I hope I was clear. Bye

Trying to figure out the new rotation angle for rocket

i have this code
float angle = rocket.rotation;
float vx = sin(angle * M_PI / 180) * xVelocity;
float vy = cos(angle * M_PI / 180) * yVelocity;
CGPoint direction = ccp(vx, vy);
[rocket setPosition:ccpAdd(rocket.position, direction)];
yVelocity -= 0.2;
basically it fires a rocket in the direction i set it to face. This works fine the rocket goes up then comes down fine. I now need to make the rockets rotation change with the new direction i am setting so that the rocket is rotated correctly for the way it is flying. How can i work out the new angle i need to rotate the rocket properly? I'm assuming i can use the new direction to create this new angle but im not sure how. Thanks
I'm not really sure what you mean, but the first part is not accurate
float angle = rocket.rotation;
float vx = sin(angle * M_PI / 180) * xVelocity;
float vy = cos(angle * M_PI / 180) * yVelocity;
To say how fast the rocket is going and in what direction, you have two options:
Tell direction(angle) and speed(how fast it goes in m/second)
Tell horizontal velocity (how fast it goes horizontally in m/second) and vertical velocity(m/second)
If you have the velocity(horizontal and vertical), you can calculate the speed and direction. And also, if you have the angle and speed, you can calculate the velocity(horizontal and vertical). Your code seems to be calculating the velocity, from direction and velocity instead of direction and speed.
float angle = rocket.rotation;
xVelocity = sin(angle * M_PI / 180) * speed;
yVelocity = cos(angle * M_PI / 180) * speed;
yVelocity -= 0.2f;//apply gravity
//now we need to find the new angle and speed again
//speed is easy, Pythagoras helps
speed = sqrt(xVelocity*xVelocity + yVelocity*yVelocity);
//angle is more difficult, luckily atan2 solves this:
angle = atan2(yVelocity,xVelocity);
//now we can update the rocket
//Sorry, but I don't know COCOS...

Resources