Jump to content

Physically Attaching Two Entities


gamecreator
 Share

Recommended Posts

I'm trying to attach two boxes together. The red is supposed to be attached to the blue so that when the blue rotates, red rotates with it. Red will always be "above" the blue. However, I can't figure out how to do this.

 

SetParent doesn't work for physically interacting boxes.

 

My newest attempt was to connect them by a hinge joint but boxes then go through each other for some reason, even though when they're not jointed they do collide. Is this a bug? Do jointed objects not collide with each other?

 

Below is my code.

 

#include "App.h"
using namespace Leadwerks;
App::App() : window(NULL), context(NULL), world(NULL), camera(NULL) {}
App::~App() { delete world; delete window; }
Entity *parent, *child;
Joint* joint;
float angle=0;
bool App::Start()
{
window = Window::Create("TileParentTest");
context = Context::Create(window);
world = World::Create();
camera = Camera::Create();
camera->Move(0,1,-3);

Light* light = DirectionalLight::Create();
light->SetRotation(35,35,0);
window->HideMouse();

//Move the mouse to the center of the screen
window->SetMousePosition(context->GetWidth()/2,context->GetHeight()/2);

// Parent
parent = Model::Box();
parent->SetColor(0.0,0.0,1.0);
parent->SetShape(Shape::Box());
parent->SetPosition(0,0,0);
parent->SetPhysicsMode(Entity::RigidBodyPhysics);
parent->SetMass(1);			
parent->SetCollisionType(1);

// Child
child = Model::Box();
child->SetColor(1.0,0.0,0.0);
child->SetShape(Shape::Box());
child->SetPosition(0,1.5,0);
child->SetPhysicsMode(Entity::RigidBodyPhysics);
child->SetMass(1);			
child->SetCollisionType(1);

// Show joint visually
Entity *jointpos = Model::Box(0.1,0.1,0.1);
jointpos->SetColor(1.0,1.0,0.0);
jointpos->SetPosition(0,0.7,0);
jointpos->SetParent(parent);

// Create joint between parent and child
joint = Joint::Hinge(0,0.7,0, 0,0,1,child,parent);

return true;
}

bool App::Loop()
{
if(window->Closed() || window->KeyHit(Key::Escape)) return false;
if(window->KeyDown(Key::Left)) angle-=0.1*Time::GetSpeed();
if(window->KeyDown(Key::Right)) angle+=0.1*Time::GetSpeed();

parent->PhysicsSetPosition(0,0,0,true);
parent->PhysicsSetRotation(0,0,angle,0.01);
Time::Update();
world->Update();
world->Render();
context->Sync(false);

return true;
}

 

 

Note that if you comment out this line:

joint = Joint::Hinge(0,0.7,0, 0,0,1,child,parent);

the boxes collide. If you don't comment it out (you create the joint), tilting the blue box results in the red one rotating on the hinge and then going through the blue, several times, back and forth. (The yellow box is the hinge position.)

 

Thoughts?

 

boxes.gif

Link to comment
Share on other sites

I was just working on this today and chatted with Josh a bit about it. You can still parent the objects together, but to create the physical connect, yes, a joint has to be used.

 

When two objects are jointed together they do not collide with each other--normal operation there as the physics engine see's them as the same body. Use the joint's SetLimits() so that the objects don't penetrate each other...

 

if you:

 

joint->SetLimits(0, 0)

 

then there will be no rotation around the pin (in the case of a hinge), hence creating a "weld", if you will.

I HAVE noticed that both objects need to be the same mass. If one object's mass is greater than another you will get a spring-like effect at the joint location.

 

Finally, if any objects in the chain (either parent or objects jointed to it) has their mass set to 0.0 then the entire "body" will be excluded from physics updates. ALL objects need a mass...

  • Upvote 3

--"There is no spoon"

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