Jump to content

Joint Class Finished

Josh

124 views

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


2 Comments


Recommended Comments

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

Share this comment


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?

Share this comment


Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

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.

  • Blog Entries

    • By Josh in Josh's Dev Blog 3
      What's new
      EAX audio effects for supported hardware. Source class renamed to "Speaker". Plane joint for 2D physics, so now you can make Angry Birds with Vulkan graphics. Fixed DPI issues with fullscreen mode. Added impact noise to barrels, fixed Lua collision function not being called. Script functions now start with "Entity:" instead of "Script:", i.e. Entity:Update() instead of Script:Update(). Additionally, four examples can be run showing various functionality. Double-click on the .bat files to launch a different demo:
      First-person shooter game. 2D physics demonstration. Advanced 2D drawing with text, rotation, and scaling. Multi-camera setup.
    • By 💎Yue💎 in The shock absorbers 0
      It's interesting that when you become an expert on something, you're not sparing any effort to see how something works, but rather you're focusing on creating something. And so everything becomes easier.
      At this point of learning there is a glimpse of a low idea of creating a game, but the secret of all this is to keep it simple and to be very clear that a game is a game, and not an exact simulation of the real world. For example anyone who has a low idea of the red planet, will understand no matter the colors of the scene that is a terrain of Mars, even if it is not very real what is transmitted, a game, that's just it.
      At this point I already have an astronaut character who runs from one place to another on a very large 4096 x 4046 terrain that would surely take a long walk. My previous prototype projects involve a vehicle, but I didn't get the best implementation prospect in that time and I always found performance problems in my machine, something that isn't happening with the character controller for a third person player. 
      As always, I think I'm a scavenger looking for game resources, that's where this community exposes links to websites with interesting hd textures, and one or another model searched on the net, but what I've greatly improved is learning to write code, I have a better workflow, writing Lua code focused on the paradigm of object programming.



      Something interesting is the system of putting rocks, all very nice from the point of implementing them. And it works very well with the character controller if you put collision in cube form.
      I've been thinking about implementing a car system, I think it would be necessary in such a large terrain, but I think it's not the time, my previous experience, involves deterioration in performance and something I think is the physics of the car with respect to the terrain and rocks that in the previous project involve deterioration in the fps. Although if you implement a car would have an option would be to remove the rocks, but I prefer not to have a car and if you have rocks. 
       
       
       
       
    • By reepblue in reepblue's Blog 6
      Loading sounds in Leadwerks has always been straight forward. A sound file is loaded from the disk, and with the Source class emits the sound in 3D space. The sound entity also has a play function, but it's only really good for UI sounds. There is also Entity::EmitSound() which will play the sound at the entity's location. (You can also throw in a Source, but it'll auto release the object when it's done.)
      While this is OK for small games, larger games in which sounds may change might mean you have to open your class, and adjust the sounds accordingly. What if you use the sound in multiple places and you're happy with the volume and pitch settings from an earlier implementation? You could just redefine the source in a different actor, but why should you?
      A solution I came up with comes from SoundScripts from the Source Engine. With that engine, you had to define each sound as a SoundScript entry. This allowed you to define a sound once, and it allowed for other sound settings such as multiple sounds per entry. I thought this over, and with JSON, we can easily create a similar system for Leadwerks 4 and the new engine.
      I first started with a dummy script so I can figure out how I wanted the end result to be.
      { "soundData": { "Error": { "file": "Sound/error.wav", "volume": 1.0, "pitch": 1.0, "range": 0.25 }, "RandomSound": { "files": { "file1": "Sound/Test/tone1.wav", "file2": "Sound/Test/tone2.wav", "file3": "Sound/Test/tone3.wav" }, "volume": 1.0, "pitch": 1.0, "range": 0.25 } } } In this script, we have two sound entries. We have an error sound (Which is suppose to be the fall back sound for an invalid sound entry) and we have a sound entry that holds multiple files. We want a simple, straight forward. entry like "Error" to work, while also supporting something "RandomSound" which can be used for something like footstep sounds.
      The script is streamed and stored into multiple structs in a std::map at the application start. We use the key for the name, and the value is the struct.
      typedef struct { std::string files[128]; char filecount; float volume; float pitch; float range; bool loopmode; } sounddata_t; std::map<std::string, sounddata_t> scriptedsounds; Also notice that we don't store any pointers, just information. To do the next bit, I decided to derive off of the engine's Source class and call it "Speaker". The Speaker class allows us to load sounds via the script entry, and support multiple sounds.
      You create one like this, and you have all the functionalities with the Source as before, but a few differences.
      // Speaker: auto speaker = CreateSpeaker("RandomSound"); When you use Play() with the speaker class and if the sound entry has a "files" table array, it'll pick a sound at random. You can also use PlayIndex() to play the sound entry in the array. I also added a SetSourceEntity() function which will create a pivot, parent to the target entity. From there, the Play function will always play from the pivot's position. This is a good alternative to Entity::EmitSound(), as you don't need to Copy/Instance the Source before calling the function as that function releases the Source as mentioned earlier. Just play the speaker, and you'll be fine! You can also change the sound entry at anytime by calling SetSoundEntry(const std::string pSoundEntryName); The creation of the Speaker class will start the JSON phrasing. If it has already been done, it will not do it again.
      Having sounds being loaded and stored like this opens up a lot of possibles. One thing I plan on implementing is a volume modifier which will adjust the volume based on the games volume setting.Right now, it uses the defined volume setting. It's also a part of another system I have in the works.
×
×
  • Create New...