Jump to content

body box vs createcube set scale


Rick
 Share

Recommended Posts

It should be the same size.

EDIT: And I verified that it is, by adding those 3 lines to the windmill script (replace object with entity).

The only explanation to your phenomena I can imagine is that you see only half of the mesh, since the lower half is under the terrain, since you created the mesh at 0/0/0.

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 seems to me that :SetScale is doing ScaleEntity, which is usually the wrong thing to do. Lua seems to be missing ScaleMesh, which would do it right. As a workaround you can just do:

object.body = CreateBodyBox(1,1,1)

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 issue I have now, is when I set the bodymass to 1, the body falls, but the cube doesn't go with it. Any ideas?

 

require("scripts/class")
require("Scripts/hooks")
require("Scripts/linkedlist")


local class=CreateClass(...)


function class:InitDialog(grid)
self.super:InitDialog(grid)

group = grid:AddGroup("Cube")
group:AddProperty("size", PROPERTY_VEC3, "0", "Cube Scale")
group:AddProperty("mat", PROPERTY_FILE, "", "Material")
group:AddProperty("body_mass", PROPERTY_FLOAT, "", "Body Mass")
group:AddProperty("entity_type", PROPERTY_INTEGER, "", "Entity Type")
group:Expand(1)
end

function class:CreateObject(model)
local object=self.super:CreateObject(model)


function object:CreateCube(size, mass, type)
	-- delete if already exists
	if object.cube ~= nil then
		object.cube:Free()
		object.cube = nil
		object.body:Free()
		object.body = nil
	end

	-- create
	object.body = CreateBodyBox(size.x, size.y, size.z)
	object.body:SetMass(mass)
	EntityType(object.body, type)
	object.cube = CreateCube()
	object.cube:SetScale(size)

	object.body:SetParent(object.cube, 1)

	-- paint
	object.mat = LoadMaterial(object.matFile)
	object.cube:Paint(object.mat)

	-- position
	local scale = object.cube:GetScale()
	local pos = object.model:GetPosition()
	pos.y = pos.y - (scale.y / 2)
	object.cube:SetPosition(pos)
	object.cube:SetRotation(object.model:GetRotation())
end

-- defaults
object.model = model
object.matFile = "abstract::cobblestones.mat"
object.bodyMass = 1
object.entityType = 1
object.size = Vec3(2, 2, 2)
object.cube = nil
object.body = nil
object.material = nil

-- create default cube
object:CreateCube(object.size, object.bodyMass, object.entityType)

function object:SetKey(key,value)
	if key == "mat" then
		object.matFile = "abstract::"..value
		object.mat = LoadMaterial(object.matFile)
		object.cube:Paint(object.mat)
	elseif key == "size" then
		object.size = StringToVec3(value)
		object:CreateCube(object.size, object.bodyMass, object.entityType)
	elseif key == "body_mass" then
		object.bodyMass = tonumber(value)
		object:CreateCube(object.size, object.bodyMass, object.entityType)
	elseif key == "entity_type" then
		object.entityType = tonumber(value)
		object:CreateCube(object.size, object.bodyMass, object.entityType)
	else
		return self.super:SetKey(key,value)
	end
	return 1
end

function object:GetKey(key,value)
	if key=="" then
	else
		return self.super:GetKey(key,value)
	end
	return value
end

function object:Update()
	if GetGlobalString("mode") == "GAME_MODE" then
	else	-- editor mode
		-- moves the editor model to the top of the cube
		local scale = object.cube:GetScale()
		local pos = object.model:GetPosition()
		pos.y = pos.y - (scale.y / 2)
		--object.cube:SetPosition(pos)
		--object.body:SetPosition(pos)
		--object.cube:SetRotation(object.model:GetRotation())
	end
end
function object:Free(model)
	if object.cube ~= nil then
		object.cube:Free()
		object.cube = nil
	end
	if object.body ~= nil then
		object.body:Free()
		object.body = nil
	end
	--if object.mat ~= nil then
	--	object.mat:Free()
	--end
	self.super:Free()
end

end

Link to comment
Share on other sites

Parenting the other way works, except the box and the cube aren't together (the physics body moves with my editor object but the box is created at the origin, however when I move the physics body the box does move also), but from my understanding you can't scale bodies after they are created. You can, but the physics gets messed up from what I remember. That shouldn't be an issue though as I just recreate the physics body when the size changes anyway. Just need to get the cube at the same position as the body now.

 

[EDIT]

I got it. Thank you!

Link to comment
Share on other sites

Here's something interesting. So I uncomment the code in the update method that positions the body so I can move the body around in the editor. When I run the game everything drops and works as expected except for 1 detail. The longer I'm in editor mode and then switch to game mode, the faster the objects drop to the ground. For example, if I sit in editor mode for 1 second, then switch to game mode, the objects fall at a decent speed. Then if I go back to editor mode and wait 10 seconds and then go back into game mode, the objects fly to the ground really fast. It's almost because I SetPosition() of the body in editor mode that it's really building up speed behind the scenes, and when I don't call SetPosition() anymore, it still has the speed at which it would be falling. How can I get around this? Ideally I would think that once SetPosition() is called it resets any velocity the body would have so when it's not called on the body anymore the velocity is starting from scratch.

Link to comment
Share on other sites

Has anyone experienced issues when setting the position of a physics body and then not setting the position and letting physics happen on the body, but the body velocity is really fast now. Almost like while set position was being called on it, it was increases it's velocity even though it was standing still?

Link to comment
Share on other sites

This fast movement happens when you don't call fw.Update() in each frame. It then tries to catch up the lost time when you next time call fw.Update().

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

If you do SetPosition on a physics body in each frame, then it might also do the same, as a missing fw.Update().

You should use AddBodyForce with CalcBodyVelocity to simulate SetPosition on physics 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

hmm, well this is all just for editor mode so you can position the cube where you want it to be when the game runs. I need the cube to follow the position of the editor object model. Would you think there would be a way to reset the physics body so it doesn't have this high velocity after SetPosition() is stopped being called on the body? That way I could do that on the first run inside the update method.

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