Jump to content

CreateJointHinge not working correctly?


Canardia
 Share

Recommended Posts

I tried to add a propeller to the rear of the airplane, but no matter how I joint it, it always rotates the airplane too, when I turn the propeller (model is the airplane, target is the propeller), and I try to turn the target around its Z axis:

CreateJointHinge(model,target,Vec3(-1,0,-1.67),Vec3(0,0,-1.67))

A similar code works fine in C++, and I can turn the target around its Y axis:

CreateJointHinge(model,target,Vec3(-1,3,0),Vec3(0,3,0))

Btw, a documentation how joints should be used would be also nice, so far I've been only guessing :D

Ryzen 9 RX 6800M ■ 16GB XF8 Windows 11 ■
Ultra ■ LE 2.53DWS 5.6  Reaper ■ C/C++ C# ■ Fortran 2008 ■ Story ■
■ Homepage: https://canardia.com ■

Link to comment
Share on other sites

I tried to add a propeller to the rear of the airplane, but no matter how I joint it, it always rotates the airplane too, when I turn the propeller (model is the airplane, target is the propeller), and I try to turn the target around its Z axis:

CreateJointHinge(model,target,Vec3(-1,0,-1.67),Vec3(0,0,-1.67))

A similar code works fine in C++, and I can turn the target around its Y axis:

CreateJointHinge(model,target,Vec3(-1,3,0),Vec3(0,3,0))

Btw, a documentation how joints should be used would be also nice, so far I've been only guessing :D

 

Can you show the complete lua code.

Link to comment
Share on other sites

For the airplane:

function Initialize()
for model,entity in pairs(entitytable) do
	local target = model:GetTarget(0)
	if target~=nil then
		AppLog("TARGET 0 FOUND")
		pos = model:GetPosition()
		pos.y = pos.y + 0.02
		pos.z = pos.z - 1.67
		target:SetPosition(pos)
		rot = EntityRotation(model)
		target:SetRotation(rot)
		CreateJointHinge(model,target,Vec3(-1,0,-1.67),Vec3(0,0,-1.67))
	end
end
end

For the propeller:

function UpdatePhysics(model)
local entity,model
for model,entity in pairs(entitytable) do
	if entity.active~=0 then
		model:AddTorque( Vec3( 0, 0, 100 ) )
	end
end
end

Ryzen 9 RX 6800M ■ 16GB XF8 Windows 11 ■
Ultra ■ LE 2.53DWS 5.6  Reaper ■ C/C++ C# ■ Fortran 2008 ■ Story ■
■ Homepage: https://canardia.com ■

Link to comment
Share on other sites

My guess would be that the propeller code is adding torque to the plane as well. This is my confusion around what is stored in entitytable and how to find the entity you want when looping over it. From my understanding entitytable holds all entities loaded. So this would mean your plane and propellers are in this table. If that's the case it would be adding torque to both, which is what you are seeing.

 

Other possible things:

 

You have 2 model variables. The param coming in is named model also, so there might be confusion there. Is the parameter to UpdatePhysics() the model or the world? I know Update() passes in the world but one of the scripts I saw named it model, which Josh said was a bad variable name because it should have been world.

Link to comment
Share on other sites

I don't know if the Pin is relative or absolute to the Position vector. Like I said, I'm just trying and guessing, since there is no documentation how joints should be used (not even on the Newton forum, and they don't have a wiki of the commands either).

Ryzen 9 RX 6800M ■ 16GB XF8 Windows 11 ■
Ultra ■ LE 2.53DWS 5.6  Reaper ■ C/C++ C# ■ Fortran 2008 ■ Story ■
■ Homepage: https://canardia.com ■

Link to comment
Share on other sites

I tried all combinations, but it still rotates model, when I rotate target around its Z axis:

CreateJointHinge(model,target,Vec3(0,0,-1.67),Vec3(0,0,1))
CreateJointHinge(model,target,Vec3(0,0,-1.67),Vec3(0,0,-1)) -- maybe pin needs be outside of both bodies?
CreateJointHinge(target,model,Vec3(0,0,-1.67),Vec3(0,0,1)) -- maybe target should be jointed to the model, and not vice versa?
CreateJointHinge(target,model,Vec3(0,0,-1.67),Vec3(0,0,-1))
CreateJointHinge(model,target,Vec3(0,0,-30),Vec3(0,0,1)) -- maybe both position and pin needs to be outside both bodies?

Ryzen 9 RX 6800M ■ 16GB XF8 Windows 11 ■
Ultra ■ LE 2.53DWS 5.6  Reaper ■ C/C++ C# ■ Fortran 2008 ■ Story ■
■ Homepage: https://canardia.com ■

Link to comment
Share on other sites

I tried to make a standalone LUA demo, and it works fine there, when I used Vec3(0,0,0) as position and Vec3(0,0,1) as pin. I can't figure out what's different in Editor.

dofile("scripts/base.lua")
RegisterAbstractPath("")
Graphics(800,600)
fw=CreateFramewerk()
DebugPhysics(1)

model=CreateBodyBox(1,1,4)
pos=model:GetPosition()

target=CreateBodyCone(1,1)
target:Move(Vec3(0,0,-2.7))
target:Turn(Vec3(90,180,0))

fw.main.camera:SetPosition(pos)
fw.main.camera:Turn(Vec3(0,-90,0))
fw.main.camera:Move(Vec3(0,0,-10))

model:SetMass(1)
target:SetMass(1)
SetBodyGravityMode(model,0)
SetBodyGravityMode(target,0)

CreateJointHinge(model,target,Vec3(0,0,0),Vec3(0,0,1))

while(KeyHit(KEY_ESCAPE)==0) do
target:AddTorque(Vec3(0,1,0))
model:AddTorque(Vec3(0,2,0))
fw:Update()
fw:Render()
Flip(0)
end

Ryzen 9 RX 6800M ■ 16GB XF8 Windows 11 ■
Ultra ■ LE 2.53DWS 5.6  Reaper ■ C/C++ C# ■ Fortran 2008 ■ Story ■
■ Homepage: https://canardia.com ■

Link to comment
Share on other sites

The way you are looping through all the entities to find the target is kind of backwards. You know you want your plane to have a propeller. You don't need to place it in the editor every time you want a plane. It's far easier just to do this in the spawn function:

 

Function Spawn(model)
entity=base_Spawn(model)
entity.propeller = LoadModel( "abstract::propeller.gmf" )
If entity.propeller~=nil then
	attachmentposition=TFormPoint(Vec3(1,0,1),model,nil)
	entity.propeller:SetPosition( attachmentposition )
	CreateJointHinge( model, entity.propeller, attachmentposition, Vec3(0,0,1) )
End
End

 

Make sure you get rid of the propeller in the Kill function:

Function Kill( model )
entity=entitytable[ model ]
If entity~=nil then
	If entity.propeller~=nil then
		entity.propeller:Free()
		entity.propeller=nil
	End
End
base_Kill(model)
End

 

I used this kind of automatic subobjects in the train cars to attach wheels to the car.

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Thanks, that works much better, but if I do all attachments to the airplane this way, I lose all property dialogs in Editor.

For example the smoke emitter is really nice to have in Editor, and not in code.

 

I think I will just use TurnEntity() on the propeller, since Newton has a problem with high speed rotations also.

The wheels can be done by code, as they don't need any adjustments in Editor.

The engine exhaust smoke should be done in Editor, as it's too much coding to make particles by code.

Ryzen 9 RX 6800M ■ 16GB XF8 Windows 11 ■
Ultra ■ LE 2.53DWS 5.6  Reaper ■ C/C++ C# ■ Fortran 2008 ■ Story ■
■ Homepage: https://canardia.com ■

Link to comment
Share on other sites

It should still have its convex collision hull, but that works also when rotating it with TurnEntity(), it just won't get affected by other physics bodies, but the other physics bodies will.

Ryzen 9 RX 6800M ■ 16GB XF8 Windows 11 ■
Ultra ■ LE 2.53DWS 5.6  Reaper ■ C/C++ C# ■ Fortran 2008 ■ Story ■
■ Homepage: https://canardia.com ■

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