Jump to content

Physics Constraints


SpiderPig
 Share

Recommended Posts

What's the best way to add physics constraints to a custom player controller?  For example, the cylinder shape is pulled toward gravity (which is not always straight down) then collides with an object, how do I stop little movements like sliding and twisting once it collides?

As far as I can tell, the inbuilt character controller only slides if the slope is greater than max slope.  And when it moves around by key press it doesn't rotate left or right or jitter as it goes over various sloped polygons.

Gravity is added like so in a different class to the controller;

float _force = -9.8f;
entity->AddForce(_gravity->gravityDirection.x * _force, _gravity->gravityDirection.y * _force, _gravity->gravityDirection.z * _force);		//gravityDirection is a normalized vector

And so far for the controller I've been doing this to make it move;

Vec3 _velocity = entity->GetVelocity(true);					//Get the current velocity
Vec3 _gravityVelocity = _velocity.Multiply(upDirection);			//Find how much of the velocity is along the gravity vector
Vec3 _otherVelocity = _velocity.Subtract(_gravityVelocity);			//Any remaining velocity other than gravity

Vec3 _forwardVelocity = _forwardVector * move;					//A vector in front of the player and perpindicular to gravity
Vec3 _strafeVelocity = _strafeVector * strafe;					//A vector that is the cross of gravity and the forward vector (out to the side)
Vec3 _jumpVelocity = upDirection * jump;					//GravityDirection * -1.0f
movementVector = _forwardVelocity + _strafeVelocity + _jumpVelocity;		//Find total movement vector (don't normalize, magnitude is speed)
entity->SetVelocity((_velocity - _otherVelocity) + movementVector, true);	//Subtract othervelocity to get only what gravity is, then add any movemnt
entity->SetOmega(0.0f, 0.0f, 0.0f, true);					//Been using this to reduce angular rotation, not perfect though

The above code has issues still, but it's my attempt at constraining it along gravity unless needed to move.  This code isn't done in a physics hook, which I was wondering if that might be better?

And here's a quick video showing the physic shape jittering.  It doesn't translate to the camera much at the moment but it does when the jitter is larger.  Also I can't move the shape along it's local axis because of the jitter and the random rotation that happens as a result of moving.

https://youtu.be/ZA_UNsYCEQs

Any thoughts on more accurate ways of constraining are appreciated :D

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...