Hello I have some problem using Gazebo AddRelativeForce() function.
I make Some airplane, and has 12 links.
So. I set force each link through ROS command.
Look this pic.
you can see 12 links
In this case, I apply force just 1 Link at position of 1 o'clock motor
I think airplane is left rolling and Nose up motion.
but motion look like this video.
enter link description here
My plugin code is
public: void SetForce_C2(const double &_forceC2)
{
this->link2->AddRelativeForce({0,0,_forceC2});
}
public: void SetForce_C3(const double &_forceC3)
{
this->link3->AddRelativeForce({0,0,_forceC3});
}
public: void SetForce_C4(const double &_forceC4)
{
this->link4->AddRelativeForce({0,0,_forceC4});
}
this->link2 = model->GetLinks()[2];
this->link3 = model->GetLinks()[3];
this->link4 = model->GetLinks()[4];
private:
physics::LinkPtr link2;
private:
physics::LinkPtr link3;
private:
physics::LinkPtr link4;
I think code is correct.
I want to motion correct, that apply force just 1 Link at position of 1 o'clock motor
and than airplane motion is rolling and nose up
I think gazebo is not calculate moment of inertia and apply to airplane.
if is correct, should i calculate moment of inertia force and apply myself?
Ps. Airplane body inertia and mass is
<mass>5315.4</mass>
<inertia>
<ixx>34198</ixx>
<ixy>-0.08385</ixy>
<ixz>-1.56572</ixz>
<iyy>49580</iyy>
<iyz>3959.2</iyz>
<izz>77842</izz>
</inertia>
Ps2. joint axis of Z is Up
Related
My code for physics in my game is this:
-- dt = time after each update
self.x = math.floor(self.x + math.sin(self.angle)*self.speed*dt)
self.y = math.floor(self.y - math.cos(self.angle)*self.speed*dt)
-- addVector(speed,angle,speed2,angle2)
self.speed,self.angle = addVector(self.speed,self.angle,g,math.pi)`
when it hits the ground, the code for it to bounce is :
self.angle = math.pi - self.angle
self.y = other.y - self.r`
the function addVector is defined here:
x = math.sin(angle)*speed + math.sin(angle2)*speed2
y = math.cos(angle)*speed + math.cos(angle2)*speed2
v = math.sqrt(x^2 + y^2)
a = math.pi/2 - math.atan(y,x)
return v,a
but when I place a single ball in the simulation without any drag or elasticity, the height of the ball after each bounce keeps getting higher. Any idea what may be causing the problem?
Edit: self.r is the radius, self.x and self.y are the position of the centre of the ball.
Your Y axis is increasing downward and decreasing upward.
Making self.y = math.floor(..) moves your ball a bit upward every frame.
The solution is to store your coordinates with maximal precision.
You could make new variable y_for_drawing = math.floor(y) to draw the ball at pixel with integer coordinates, but your main y value must have fractional part.
I’ve managed to get your code to run and reproduce the behavior you are seeing. I also find it difficult to figure out the issue. Here’s why: movement physics involves position, which is affected by a velocity vector, which in turn is affected by an acceleration vector. In your code these are all there, but are in no way clearly separated. There are trig functions and floor functions all interacting in a way that makes it difficult to see what role they are playing in the final position calculation.
By far the best and easiest-to-understand tutorial to help you implement basic physics lime this is The Nature of Code (free to read online, with interactive examples). As an exercise I ported most of the early exercises into Lua. I would suggest you see how he clearly separates the position, velocity and acceleration calculations.
As an experiemnt, increase g to a much higher number. When I did that, I noticed the ball would eventually settle to the ground, but of course the bouces were too fast and it didnt bounce in a way that seems natural.
Also, define other.y - it doesnt seem to affect the bouncing issue, but just to be clear on what that is.
What am I trying to achieve?
I have a Sprite which is supposed to move with the mouse position (kinda like a cursor). In my case though I also have some other Textures (Obstacle-class). So if the mouse collides with such an obstacle I want the texture to stop moving in that direction.
What is the problem?
While the texture does follow the mouse and also does stop when the mouse "collides" with an obstacle, at some point the cursor is not within the Bounding Rectangle anymore, but on the other side of a wall for example. The consequence, the texture's position is updated to the mouse position and it suddenly appears behind the wall which is not a desired behavior.
My collision method.
private void CheckCollision(List<Obstacle> _obstacleList, MouseState mState)
{
int xOffset = oldMouseState.X - mState.X;
int yOffset = oldMouseState.Y - mState.Y;
Vector2 offsetPosition = new Vector2(oldMouseState.X + xOffset,oldMouseState.Y + yOffset);
bool collides = false;
foreach (Obstacle obstacle in _obstacleList)
{
if (obstacle.BoundRectangle.Contains(offsetPosition))
{
collides = true;
}
}
if (!collides)
{
position = offsetPosition;
}
}
Question
What be a way to prevent the sprite to move through walls in my case?
Thanks in advance.
As you know, you can read the the mouse position by calling Mouse.GetState(). But you can also set the mouse position to whatever you want through Mouse.SetPosition(X,Y) and the mouse will go there.
So, if you are up against, say, an X barrier (vertical barrier), simply call
Mouse.SetPosition(oldMouseState.X, mState.Y);
and your mouse will not change its X value even if pushing your mouse in that direction, it will not go through the wall at all but it is allowed to go up and down just fine.
If you back off from the wall, simply don't call this line and it will operate like befor.
You could store the last (valid) known position of the mouse and the current position of the mouse (valid means the mouse isn't in a rectangle where it shouldn't be). When you hit a rectangle that the mouse shouldn't pass through with your current mouse position, you iterate back to the last valid position in a while loop and check if the mouse is still in the blocking sprite every time you moved the mouse closer to the valid position. If the mouse is outside the forbidden zone, you just exit the while loop and the mouse is quite close to the border of the obstacle.
I am creating a game which uses accelerometer to move ball (football model game).
function acc(e)
physics.setGravity(e.xInstant*(screenW/4), -1*e.yInstant*(screenH/4))
end
But this code not giving a smooth flow of game. Can u guys help me for right option
You either need to "calibrate" or to change the algorithm. I would try calibrate first: replace your acc() with
function acc(e)
local calibX = 1
local calibY = 1
-- physics.setGravity(calibX * e.xInstant*(screenW/4), -calibY * e.yInstant*(screenH/4))
print(e.xInstant, e.yInstant, e.zInstant)
end
and do the tilting that you feel is "not smooth", looking at the values printed when you do that. This will tell you what calibX and Y should be, set them then uncomment the physics line, comment out the print line, try again, until you get it right. For example, if the x and y instant are around 10, you could try calibX = 0.1 or less.
If you can't find values for calibration coefficients that give you desired motion, you need to change your algorithm. For example, if you are trying to simulate the tilt of your device as though it was a table on which a marble rolls, and tilting the table should make marble move, then changing gravity is not the way to do it. You want to apply a horizontal force which is equal to g*sin(theta) where g is gravity and theta is the tilt angle of the device side-to-side. The formula is slightly more complex if you allow tilt along the other direction.
How do I make a RTS camera so that when the mouse is at the edge of the window, it will move either left/right/up/down. I been trying to create an invisible box at the side of the screen so that when the mouse is at the box it will move the camera, but it still doesn't work. Please help!
Building upon what #Davor Mlinaric said, using the mouses x and y coordinates (which can be gotten from Mouse.GetState()), and testing whether those coordinates come in contact with the top, bottom and sides of the screen.
It would be a good start to set where those boxes will be something along the lines of:
GraphicsDevice.Viewport.Width/Height -/+ offset
Where offset is the amount of distance from the top,bottom or side.
Then test where the mouse position is, with a boolean.
boolean inTheZone = false;
//Bottom Box
if(Mouse.GetState().Y > GraphicsDevice.Viewport.Height - offset)
{
//Move camera in the y axis downwards (+).
inTheZone = true;
}
else
{
inTheZone = false;
}
and then the same for the 4 remaining sides.
Notice ive also used Y here, depending on how you set up the camera this may change to Z.
I hope this helps
Suppose you have quaternion that describes the rotation of a 3D Model.
What I want to do is, given an Object (with rotationQuaternion, side vector...), I want to align it to a target point.
For a spaceship, I want the cockpit to point to a target.
Here is some code I have ... It's not doing what I want and I don't know why...
if (_target._ray.Position != _obj._ray.Position)
{
Vector3 vec = Vector3.Normalize(_target._ray.Position - _obj._ray.Position);
float angle = (float)Math.Acos(Vector3.Dot(vec, _obj._ray.Direction));
Vector3 cross = Vector3.Cross(vec, _obj._ray.Direction);
if (cross == Vector3.Zero)
cross = _obj._side;
_obj._rotationQuaternion *= Quaternion.CreateFromAxisAngle(cross,angle);
}
// Updates direction, up, side vectors and model Matrix
_obj.UpdateMatrix();
after some time the rotationQuaternion is filled with almost Zero at X,Y,Z and W
Any help?
Thanks ;-)
This is a shortcut I've used to get the quaternion for lock-on-target rotation:
Matrix rot = Matrix.CreateLookAt(_arrow.Position, _cube.Position, Vector3.Down);
_arrow.Rotation = Quaternion.CreateFromRotationMatrix(rot);
For this example, I'm rendering an arrow and a cube, where the cube is moving around in a circle, and with the above code the arrow is always pointing at the cube. (Though I imagine there are some edge cases when cube is exactly above or below).
Once you get this quaternion (from spaceship to target), you can use Quaternion.Lerp() to interpolate between current ship rotation and the aligned one. This will give your rotation a smooth transition (not just snap to target).
Btw, might be that your rotation gets reduced to zero because you're using *= when assigning to it.
Your code's a bit funky.
if (_target._ray.Position != _obj._ray.Position)
{
This may or may not be correct. Clearly, you've overridden the equals comparator. The correct thing be be doing here would be to ensure that the dot-product between the two (unit-length) rays is close to 1. If the rays have the same origin, then presumably have equal 'positions' means they're the same.
Vector3 vec = Vector3.Normalize(_target._ray.Position - _obj._ray.Position);
This seems particularly wrong. Unless the minus operator has been overridden in a strange way, subtracting this way doesn't make sense.
Here's pseudocode for what I recommend:
normalize3(targetRay);
normalize3(objectRay);
angleDif = acos(dotProduct(targetRay,objectRay));
if (angleDif!=0) {
orthoRay = crossProduct(objectRay,targetRay);
normalize3(orthoRay);
deltaQ = quaternionFromAxisAngle(orthoRay,angleDif);
rotationQuaternion = deltaQ*rotationQuaternion;
normalize4(rotationQuaternion);
}
Two things to note here:
Quaternions are not commutative. I've assumed that your quaternions are rotating column vectors; so I put deltaQ on the left. It's not clear what your *= operator is doing.
It's important to regularly normalize your quaternions after multiplication. Otherwise small errors accumulate and they drift away from unit length causing all manner of grief.
OMG! It worked!!!
Vector3 targetRay = Vector3.Normalize(_target._ray.Position - _obj._ray.Position);
Vector3 objectRay = Vector3.Normalize(_obj._ray.Direction);
float angle = (float)Math.Acos(Vector3.Dot(targetRay, objectRay));
if (angle!=0)
{
Vector3 ortho = Vector3.Normalize(Vector3.Cross(objectRay, targetRay));
_obj._rotationQuaternion = Quaternion.CreateFromAxisAngle(ortho, angle) * _obj._rotationQuaternion;
_obj._rotationQuaternion.Normalize();
}
_obj.UpdateMatrix();
Thank you very much JCooper!!!
And niko I like the idea of Lerp ;-)