Jump to content

Joint Class Finished


Josh

1,511 views

 Share

I'm happy to say the physics joint class in the new engine is completed. I made all the members that are not meant to be accessed private. One interesting part is the parent and child public members, which are constant pointers to private members. This should result in a read-only member in C++. A sol property is used to expose this to Lua in a read-only manner.

The upvector joint will align an object's Y axis to any vector you set, but still allow rotation around the axis. This is perfect for making 2D games with physics. You can just set the axis to point along the Z world axis, and you will have 2D physics locked to the XY plane.

class Joint : public SharedObject
{
	std::list<shared_ptr<Joint> >::iterator bodylink[2];
	Mat4 entitymatrix[2];
	bool uselimits;
	Vec2 limits;
	Vec3 origin;
	PhysicsJoint* physicsjoint;
	bool motorenabled;
	bool limitsenabled;
	float angle;
	float motorspeed;
	float motorpower;
	float stiffness;
	float spring;
	Vec2 friction;
	Mat4 targetmatrix;
	shared_ptr<Entity> parent_;
	shared_ptr<Entity> child_;
public:

	Joint();
	virtual ~Joint();

	const shared_ptr<Entity>& parent;
	const shared_ptr<Entity>& child;
	virtual void Break();
	virtual void SetSpring(const float relaxation, const float spring, const float damper);
	virtual float GetSpring();
	virtual void SetLimits(const float limits0, const float limits1);
	virtual Vec2 GetLimits();
	virtual void SetStiffness(const float stiffness);
	virtual float GetStiffness();
	virtual void SetMotorSpeed(const float speed);
	virtual void SetMotorPower(const float power);
	virtual float GetMotorPower();
	virtual float GetMotorSpeed();
	virtual void EnableLimits();
	virtual void EnableMotor(const bool enabled);
	virtual void DisableLimits();
	virtual void DisableMotor();
	virtual bool LimitsEnabled();
	virtual bool MotorEnabled();
	virtual void SetFriction(const float angularfriction, const float linearfriction);
	virtual Vec2 GetFriction();
	virtual void SetTargetPosition(const Vec3& pos, const float blend = 1.0f);
	virtual void SetTargetPosition(const float x, const float y, const float z, const float blend = 1.0f);
	virtual void SetTargetRotation(const Quat& rotation, const float blend = 1.0f);
	virtual void SetTargetRotation(const Vec3& rotation, const float blend = 1.0f);
	virtual void SetTargetRotation(const float pitch, const float yaw, const float roll, const float blend = 1.0f);		
	virtual void SetTarget(const float target);
	virtual void SetTarget(const Mat4& target);
	
	friend shared_ptr<Joint> CreateUpVectorJoint(const Vec3&, shared_ptr<Entity>);
	friend shared_ptr<Joint> CreateKinematicJoint(const float, const float, const float, shared_ptr<Entity>);
	friend shared_ptr<Joint> CreateSliderJoint(const float, const float, const float, const float, const float, const float, shared_ptr<Entity>, shared_ptr<Entity>);
	friend shared_ptr<Joint> CreateBallAndSocketJoint(const float, const float, const float, shared_ptr<Entity>, shared_ptr<Entity>);
	friend shared_ptr<Joint> CreateHingeJoint(const float, const float, const float, const float, const float, const float, shared_ptr<Entity>, shared_ptr<Entity>);
};

extern shared_ptr<Joint> CreateUpVectorJoint(const Vec3& pin, shared_ptr<Entity> child);
extern shared_ptr<Joint> CreateUpVectorJoint(const float pinx, const float piny, const float pinz, shared_ptr<Entity> child);
extern shared_ptr<Joint> CreateKinematicJoint(const Vec3& pos, shared_ptr<Entity> child);
extern shared_ptr<Joint> CreateKinematicJoint(const float posx, const float posy, const float posz, shared_ptr<Entity> child);
extern shared_ptr<Joint> CreateSliderJoint(const Vec3& pos, const Vec3& pin, shared_ptr<Entity> parent, shared_ptr<Entity> child);
extern shared_ptr<Joint> CreateSliderJoint(const float x, const float y, const float z, const float px, const float py, const float pz, shared_ptr<Entity> child, shared_ptr<Entity> parent);
extern shared_ptr<Joint> CreateBallAndSocketJoint(const Vec3& pos, shared_ptr<Entity> parent, shared_ptr<Entity> child);
extern shared_ptr<Joint> CreateBallAndSocketJoint(const float posx, const float posy, const float posz, shared_ptr<Entity> child, shared_ptr<Entity> parent);
extern shared_ptr<Joint> CreateHingeJoint(const Vec3& pos, const Vec3& pin, shared_ptr<Entity> parent, shared_ptr<Entity> child);
extern shared_ptr<Joint> CreateHingeJoint(const float x, const float y, const float z, const float pinx, const float piny, const float pinz, shared_ptr<Entity> child, shared_ptr<Entity> parent);

And this is what the Lua binding code looks like:

L->new_usertype<Joint>
(
	"Joint",
	sol::base_classes, sol::bases<SharedObject>(),
	sol::meta_function::index, &SharedObject::dynamic_get,
	sol::meta_function::new_index, &SharedObject::dynamic_set,

	"parent", sol::property([](Joint& j) { return j.parent; }),
	"child", sol::property([](Joint& j) { return j.child; }),
	"Break", &Joint::Break,
	"SetSpring", &Joint::SetSpring,
	"GetSpring", &Joint::GetSpring,
	"SetLimits", &Joint::SetLimits,
	"GetLimits", &Joint::GetLimits,
	"EnableLimits", &Joint::EnableLimits,
	"DisableLimits", &Joint::DisableLimits,
	"LimitsEnabled", &Joint::LimitsEnabled,
	"EnableMotor", &Joint::EnableMotor,
	"DisableMotor", &Joint::DisableMotor,
	"MotorEnabled", &Joint::MotorEnabled,
	"SetFriction", &Joint::SetFriction,
	"GetFriction", &Joint::GetFriction,
	"SetStiffness", &Joint::SetStiffness,
	"GetStiffness", &Joint::GetStiffness,
	"SetMotorSpeed", &Joint::SetMotorSpeed,
	"GetMotorSpeed", &Joint::GetMotorSpeed,
	"SetMotorPower", &Joint::SetMotorPower,
	"GetMotorPower", &Joint::GetMotorPower,
	"SetTargetRotation", sol::overload(
		sol::resolve<void(const Vec3&, const float)>(&Joint::SetTargetRotation),
		sol::resolve<void(const Quat&, const float)>(&Joint::SetTargetRotation),
		sol::resolve<void(const float, const float, const float, const float)>(&Joint::SetTargetRotation)
	),
	"SetTargetPosition", sol::overload(
		sol::resolve<void(const Vec3&, const float)>(&Joint::SetTargetPosition),
		sol::resolve<void(const float, const float, const float, const float)>(&Joint::SetTargetPosition)
	),
	"SetTarget", sol::overload(
		sol::resolve<void(const float)>(&Joint::SetTarget),
		sol::resolve<void(const Mat4&)>(&Joint::SetTarget)
	)
);
L->set_function("CreateHingeJoint", sol::overload(
	sol::resolve<shared_ptr<Joint>(const Vec3&, const Vec3&, shared_ptr<Entity>, shared_ptr<Entity>)>(&CreateHingeJoint),
	sol::resolve<shared_ptr<Joint>(const float, const float, const float, const float, const float, const float, shared_ptr<Entity>, shared_ptr<Entity>)>(&CreateHingeJoint)
));
L->set_function("CreateSliderJoint", sol::overload(
	sol::resolve<shared_ptr<Joint>(const Vec3&, const Vec3&, shared_ptr<Entity>, shared_ptr<Entity>)>(&CreateSliderJoint),
	sol::resolve<shared_ptr<Joint>(const float, const float, const float, const float, const float, const float, shared_ptr<Entity>, shared_ptr<Entity>)>(&CreateSliderJoint)
));
L->set_function("CreateBallAndSocketJoint", sol::overload(
	sol::resolve<shared_ptr<Joint>(const Vec3&, shared_ptr<Entity>, shared_ptr<Entity>)>(&CreateBallAndSocketJoint),
	sol::resolve<shared_ptr<Joint>(const float, const float, const float, shared_ptr<Entity>, shared_ptr<Entity>)>(&CreateBallAndSocketJoint)
));
L->set_function("CreateKinematicJoint", sol::overload(
	sol::resolve<shared_ptr<Joint>(const Vec3&, shared_ptr<Entity>)>(&CreateKinematicJoint),
	sol::resolve<shared_ptr<Joint>(const float, const float, const float, shared_ptr<Entity>)>(&CreateKinematicJoint)
));
L->set_function("CreateUpVectorJoint", sol::overload(
	sol::resolve<shared_ptr<Joint>(const Vec3&, shared_ptr<Entity>)>(&CreateUpVectorJoint),
	sol::resolve<shared_ptr<Joint>(const float, const float, const float, shared_ptr<Entity>)>(&CreateUpVectorJoint)
));

A big thanks goes out to the developers of sol and Newton Dynamics for providing excellent technology for me to build on.

  • Like 2
 Share

2 Comments


Recommended Comments

One small change is all the "motor" functions have been renamed to "actuator" which is a little more accurate.

Link to comment

I was thinking about having the Enable functions accept a Boolean because it makes for fewer commands and sometimes the programming is a little easier.

Enable(false) would disable it.

Enable(true) or Enable() would enable it.

Any thoughts on this?

Link to comment
Guest
Add a comment...

×   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.

×
×
  • Create New...