Apply physics to player using bullet physics - c++

So I've had a lot of problems trying to properly get physics applied to the play by using bullet physics. I've tried kinematic bodies to an unsuccess. I am now trying now trying to use a rigid body I've had little successfully. Pasted below is the updating code for the player's matrix and the rigid bodies location. My question is how could I update the player's position correctly.
Update Player's rigid body and the camera's matrix *the gCamera.matrix is just the projection and view matrix's multiplyed together
physics.PlayerBody->getMotionState()->getWorldTransform(k);
k.getOpenGLMatrix(glm::value_ptr(gCamera.matrix));
btVector3 j;
j = physics.PlayerBody->getLinearVelocity();
gCamera.position.x = j.getX();
gCamera.position.y = j.getY();
gCamera.position.z = j.getZ();
btVector3 pastPos;
pastPos.setX(gCamera.position.x);
pastPos.setY(gCamera.position.y);
pastPos.setZ(gCamera.position.z);
gCamera.GetInput(window);
float lVelocityX = sin(gCamera.horizontalAngle * 3.14159265359 / 180) * 2;
float lVelocityY = physics.PlayerBody->getLinearVelocity().y();
float lVelocityZ = cos(gCamera.verticalAngle * 3.14159265359 / 180) * 2;
//physics.PlayerBody->setLinearVelocity(btVector3(lVelocityX, lVelocityY, lVelocityZ));
physics.PlayerBody->setLinearVelocity(btVector3(gCamera.position.x, gCamera.position.y, gCamera.position.z));
btTransform t;
t.setFromOpenGLMatrix(glm::value_ptr(gCamera.matrix));
physics.PlayerBody->getMotionState()->setWorldTransform(t);
physics.PlayerBody->setCenterOfMassTransform(t);

If you want to be in control of where the player is, then use kinematic. I know you've had trouble but that's just the best solution.
If you want physics to move player, you need a rigid body and apply forces to move it.
It is not possible to move a rigid body yourself in a reliable way. Where I have had to teleport objects in the past I have removed and added a new rigid body, but you do not want to be doing that every frame for your player.

Related

Moving and Imported model along the forward Vector - DirectX11 C++

I have spent ages trying to work out the way to create the forward vector of any imported object, This is so that I can complete the players controls and also add billboarding. This is for a University project where I have been given a set GameObject class to load in the objects using a MeshLoader and OBJLoader class.
I currently am using A .GetWorld(); which returns me an XMFLOAT4X4 which should give me the data of the object in the world. I then convert it to a XMMATRIX using the XMLoadFloat4X4 and then grab each row of data from that matrix into its own XMVECTOR. I have tested these and Believe that they are in the correct order but am still having the problem of my character never moving when I hit the forward key. I wondered If I am doing anything completely wrong or if I have just missed something out.
else if (GetAsyncKeyState(VK_UP))
{
XMFLOAT4X4 currrentWorld = _zaz.GetWorld();
XMMATRIX currentWorldM = XMLoadFloat4x4(&currrentWorld);
XMVECTOR scale = currentWorldM.r[1];
XMVECTOR rotation = currentWorldM.r[2];
XMVECTOR translation = currentWorldM.r[3];
XMVECTOR forward = currentWorldM.r[4];
forward = XMVector3Normalize(forward);
translation = XMVectorAdd(translation, forward);
newWorld = XMMATRIX(scale, rotation, translation, forward);
_zaz.SetWorld(&newWorld);
_zaz.UpdateWorld();
}
The other problem is that “_zaz.UpdateWorld(); only seems to work inside my update method even though all Keyboard controls are checked on update.
void GameObject::UpdateWorld()
{
XMMATRIX scale = XMLoadFloat4x4(&_scale);
XMMATRIX rotate = XMLoadFloat4x4(&_rotate);
XMMATRIX translate = XMLoadFloat4x4(&_translate);
XMStoreFloat4x4(&_world, scale * rotate * translate);
}
void GameObject::SetWorld(XMMATRIX* world)
{
XMStoreFloat4x4(&_world, *world);
}
It is definitely reading in the values, I get the game to break on the key “up” being pressed which should run the above code. All of the values are set. The car does currently rotate and If I rotate the car slightly and then break the game only the rotation and forward vectors change, Which should be ok?
The watch of all of the vectors after the second iteration of the elseif keypressed up code is called
If you have any Ideas it would help massively.
I assumed that It would be similar to:
else if (GetAsyncKeyState(VK_CONTROL))
{
XMFLOAT4 position = _cameraFree.GetEye();
XMFLOAT4 direction = _cameraFree.GetForward();
position.x -= (direction.x * 0.05f);
position.y -= (direction.y * 0.05f);
position.z -= (direction.z * 0.05f);
_cameraFree.SetEye(position);
_cameraFree.CalculateViewProjection();
}
Which uses XMFLOAT4's for the cameras data, which was easy to do, With the XMFLOAT4X4's and XMMATRIX's that my GameObject rely on it has so far been impossible to get to work. If there is anymore info that I can supply to help then please say!
Hope this helps you help me.
The way you imagine currentWorldM is completely wrong.
First, you do an out of bound access, XMMATRIX::r is only 4 elements, the valid range is [0..3].
Second, a 4x4 matrix is not built like that, it contains everything merged, you have to see it more like a frame, with 3 vectors i j k and a position O. One of them is your forward vector, one up and one right ( or left ).
A scale matrix is for example in r[0].x r[1].y and r[2].z, and this is only true as long as you do not concatenate it with a rotation, after that, it will be spread over the full matrix.

How do I accomplish proper trajectory with a Cocos2d-x node using Chipmunk 2D impulses and rotation?

I'm building a game with Cocos2d-x version 3.13.1 and I've decided to go with the built-in physics engine (Chipmunk 2D) to accomplish animations and collision detection. I have a simple projectile called BulletUnit that inherits from cocos2d::Node. It has a child sprite that displays artwork, and a rectangular physics body with the same dimensions as the artwork.
The BulletUnit has a method called fireAtPoint, which determines the angle between itself and the point specified, then sets the initial velocity based on the angle. On each update cycle, acceleration is applied to the projectile. This is done by applying impulses to the body based on an acceleration variable and the angle calculated in fireAtPoint. Here's the code:
bool BulletUnit::init() {
if (!Unit::init()) return false;
displaySprite_ = Sprite::createWithSpriteFrameName(frameName_);
this->addChild(displaySprite_);
auto physicsBody = PhysicsBody::createBox(displaySprite_->getContentSize());
physicsBody->setCollisionBitmask(0);
this->setPhysicsBody(physicsBody);
return true;
}
void BulletUnit::update(float dt) {
auto mass = this->getPhysicsBody()->getMass();
this->getPhysicsBody()->applyImpulse({
acceleration_ * mass * cosf(angle_),
acceleration_ * mass * sinf(angle_)
});
}
void BulletUnit::fireAtPoint(const Point &point) {
angle_ = Trig::angleBetweenPoints(this->getPosition(), point);
auto physicsBody = this->getPhysicsBody();
physicsBody->setVelocityLimit(maxSpeed_);
physicsBody->setVelocity({
startingSpeed_ * cosf(angle_),
startingSpeed_ * sinf(angle_)
});
}
This works exactly as I want it to. You can see in the image below, my bullets are accelerating as planned and traveling directly towards my mouse clicks.
But, there's one obvious flaw: the bullet is remaining flat instead of rotating to "point" towards the target. So, I adjust fireAtPoint to apply a rotation to the node. Here's the updated method:
void BulletUnit::fireAtPoint(const Point &point) {
angle_ = Trig::angleBetweenPoints(this->getPosition(), point);
// This rotates the node to make it point towards the target
this->setRotation(angle_ * -180.0f/M_PI);
auto physicsBody = this->getPhysicsBody();
physicsBody->setVelocityLimit(maxSpeed_);
physicsBody->setVelocity({
startingSpeed_ * cosf(angle_),
startingSpeed_ * sinf(angle_)
});
}
This almost works. The bullet is pointing in the right direction, but the trajectory is now way off and seems to be arcing away from the target as a result of the rotation: the more drastic the rotation, the more drastic the arcing. The following image illustrates what's happening:
So, it seems that setting the rotation is causing the physics engine to behave in a way I hadn't originally expected. I've been racking my brain on ways to correct the flight path, but so far, no luck! Any suggestions would be greatly apprecitated. Thanks!

Asteroids game c++ inertia

I'm trying to make my asteroid keep moving once I press a button.
void Ship::applyThrust()
{
_v.setX(_v.getX() - sin(2 * M_PI * (_angle / 360)) * 2.5);
_v.setY(_v.getY() + cos(2 * M_PI * (_angle / 360)) * 2.5);
}
This is the formula I have to help my ship move.
The _v.setX and _vsetY updates the X and Y position
The M_PI is just 3.14.
The _angle is how much of a rotation I set with my left and right arrow keys and
The 2.5 is how many frames per I want it to move
My ship is moving just fine, the problem is that I'm trying to simulate space inertia and have my ship continue moving.
Any ideas on how would be the logic for this?
Thanks,
Inside of your game loop, you'll need a function that updates the position of the ship based on its x and y velocity. You're close in getting the new x and y coordinates of your ship, but you don't account for the velocity of the ship. When you apply thrust, get the x and y components of your velocity, not your new position. You'll have an additional function to update position which should be called within the game loop at a timed interval - e.g. every time you update the frame. So your applyThrust function should actually update your ship's velocity. That means you'll need to add variables to your Ship class for your ship's position and your ship's velocity if you don't already have them. I'm breaking out the components of position and velocity for simplicity, but you'll probably want to store them in a vector for clarity:
class Ship
{
private float xpos, ypos; // current position of your ship
private float velocityX, velocityY; // current velocity of your ship
}
Then, when you apply thrust, you change the velocity, and remember that applyThrust is only called when the thrust button is pushed, not every frame as the position update is:
void Ship::applyThrust()
{
/*
Assume 1 total unit of thrust is applied each time you hit the thrust button
so you break down the velocity into x and y components depending on the direction
your ship is facing.
*/
// thrustUnit is how much thrust each thrust button press gets you, it's arbitrary
// and can be the 2.5 multiplier you're using in your question
float thrustUnit = 1.0;
// now get the x and y thrust components of velocity based on your ship's direction
// assuming 0 degrees is pointing to the right
float newVX, newVY = 0.0;
newVX = thrustUnit * cos(_angle);
newVY = thrustUnit * sin(_angle); // radian conversion left out to save space
// Update your ship's velocity
updateVelocity(newVX, newVY);
}
updateVelocity will look something like: (note that the velocity is additive so it continues to drift unless a thrust is applied in the opposite direction - the same as if it were in a frictionless medium such as space)
void Ship::updateVelocity(newVX, newVY)
{
// update the x component
velocityX += newVX;
velocityY += newVY;
}
So now you'll also need an updatePosition function that takes into account your ship's velocity. This gets called with each frame update:
void Ship::updatePosition()
{
// add the x component of velocity to the ship's x position
// assuming xpos and ypos are variables in the Ship class for tracking position
xpos += velocityX;
ypos += velocityY;
}
Now the position of the ship changes incrementally according to each velocity component at each frame update. You can also make thrustUnit a member variable to allow for power-ups that can either boost or decrease your thrust component for your ship's speed and being able to control it better with a smaller thrustUnit or giving it hyperspeed with a large thrustUnit.
Good luck with your game!

How to move a sprite with dynamic physics body with constant speed

Until lately I've been just changing the x coordinate of my sprite on each update and I was happy with it. But yesterday when being in the debugDraw mode, I found out that after certain speed physics body wouldn't align correctly with the sprite ,like this:
Later I got told that, (by Birkemose in cocos2d forum) the preferred way to move a physics body from A to B is to apply impulse to it. But I have no idea how to achieve constant speed this way. This is the code I used to move it without applying any impulse:
-(void)update:(CCTime)delta{
rollingHero.position=ccp(rollingHero.position.x+scrollSpeed*delta,
rollingHero.position.y);
physicsNode.position=ccp(physicsNode.position.x-scrollSpeed*delta,
physicsNode.position.y);
}
So to create a feeling of moving I scroll the physics node and the hero in opposite directions with the same scrolling speed.
I tried lots of different variants of applying impulse, but I never got it moving with constant speed. The speed accelerates and the hero gets offscreen. I would appreciate it very much if someone would post a sample code.
The reason impulse isn't working to keep your character at a constant speed is because impulse translates directly into a change in momentum (and thus a change in velocity). So if you were to try to maintain a constant velocity through impulse, you would have to check your sprite's velocity first, and although you could get pretty close to a constant velocity, it wouldn't be truly constant.
static const float kRollingHeroMoveSpeed = 10.f;
static const float kRollingHeroAccelConstant = 10.f;
-(void)update:(CCTime)delta {
// check velocity of sprite
if(_rollingHero.physicsBody.velocity.x < kRollingHeroMoveSpeed) {
// if velocity is under limit, push character
[_rollingHero.physicsBody applyImpulse:ccp(kRollingHeroAccelConstant, 0)];
}
}
The better way to do this is to step into the C level of the Chipmunk2D physics engine that powers Cocos2D physics.
-(void)onEnter {
[super onEnter];
// tell physics engine to use our C function to update physics body
_rollingHero.physicsBody.body.body->velocity_func = playerUpdateVelocity;
}
static void playerUpdateVelocity(cpBody *body,
cpVect gravity,
cpFloat damping,
cpFloat dt) {
// check validity of cpBody
cpAssertSoft(body->m > 0.0f && body->i > 0.0f, "Body's mass and moment must be positive to simulate. (Mass: %f Moment: %f)", body->m, body->i);
// update velocity and angular velocity
body->v = cpvadd(cpvmult(body->v, damping), cpvmult(cpvadd(gravity, cpvmult(body->f, body->m_inv)), dt));
body->w = body->w*damping + body->t*body->i_inv*dt;
// reset force vector
body->f = cpvzero;
// reset torque
body->t = 0.0f;
// set body's velocity to desired velocity
body->v.x = kRollingHeroMoveSpeed;
}
Here's cpBody.h on Github.

Rotating the LookAt vector of gluLookAt

I'm a student new to opengl. Currently, I'm doing a project that creates a scene.
Right now, my team is using gluLookAt() for my camera. What I want to accomplish is to try and rotate the LookAt vector around a certain point, namely where the camera is looking at.
This accomplishes a sort of "swaying in a circle". I need this because I am making a dart game for the scene, and my camera stay still, but I need it to move in a circle, but still allow the user's mouse to influence it. I also need it to create a drunken movement. That is why I am not considering rotating the Up or Eye vectors.
Currently, my look at code is like this.
int deltax = x - mouse.mX;
int deltay = y - mouse.mY;
cameradart.mYaw -= ((deltax/360.0) * 3.142) * 0.5;
cameradart.mPitch -= deltay * 0.02;
mouse.mX = x;
mouse.mY = y;
cameradart.lookAt.x = sin (cameradart.mYaw);
cameradart.lookAt.y = cameradart.mPitch ;
cameradart.lookAt.z = cos (cameradart.mYaw);
gluLookAt (cameradart.eye.x, cameradart.eye.y, cameradart.eye.z,
cameradart.eye.x + cameradart.lookAt.x, cameradart.eye.y + cameradart.lookAt.y,
cameradart.eye.z + cameradart.lookAt.z,
cameradart.up.x, cameradart.up.y, cameradart.up.z);
I know that it could be done easier using a different camera, but I really don't want to mess with my team's code by not using gluLookAt().
There's a couple of solutions in my mind, I'll tell you the easiest to understand/implement as a new graphics student
Assuming at first you're looking at (0,0,1) -store that vector-:
Think of a point that's drawing a circle and you're looking at it,
-Do it first to turn right and left (2D on X & Z)
-Let HDiff be the horizontal difference between old mouse position and the new one
-Update the x = cos(HDiff)
-Update the z = sin(HDiff)
*I didn't try it but it should work :)
If you want to be able to manipulate the camera, using a camera matrix is a much more effective mechanism than working with gluLookat. GLM is a good library for matrix and vector math and includes a lookat mechanism you can use to initialize the matrix, or you can just initialize it with a series of operations. However, remember that lookat produces a view matrix, and the view matrix is the inverse of the camera matrix.
This piece of code has a demonstration of what I'm talking about. Specifically look at the player member variable and how it's manipulated
glm::mat4 player;
...
glm::vec3 playerPosition(0, eyeHeight, ipd * 4.0f);
player = glm::inverse(glm::lookAt(playerPosition, glm::vec3(0, eyeHeight, 0), GlUtils::Y_AXIS));
This approach lets you apply changes like rotation and translation directly to the player matrix
// Rotate on the Y axis
player = glm::rotate(player, angle, glm::vec3(0, 1, 0));
This is much more intuitive than manipulating the view matrix, since changes to the view matrix always have to be the inverse of what you'd do to the player matrix.
When you're ready to render you need to convert the player matrix to a view matrix by taking it's inverse. In my example it's done like this:
gl::Stacks::modelview().top() = riftOrientation * glm::inverse(player);
This is because I'm using an application based modelview matrix stack that gets applied to the Shader programs I'm running.
For an OpenGL 1.x program, you'd instead use LoadMatrix
glMatrixMode(GL_MODELVIEW);
glm::mat4 modelview = glm::inverse(player);
glLoadMatrixf(&modelview);