[SOLVED] Basic Movement Using A Static Velocity

Hey i have been trying to figure out why my code isn’t working… its very simple but the object doesn’t move, I looked around for some sample code and found the exact same code that seemed to be working. Which makes me think maybe I tweaked a project setting. Anyways all i am trying to do is move the object by 100 on the x-axis every second.

It seems that the new position is correct but that the next frame when it does go.get_position() its still using the previous frames position, as if the set_position() didn’t do anything.

Any ideas?

function init(self)
	-- msg.post(".", "acquire_input_focus")

	self.velocity = vmath.vector3(100, 0, 0);
end

function final(self)
	-- Return input focus when the object is deleted
    -- msg.post(".", "release_input_focus")
end

function update(self, dt)
	-- Just want to move on the x axis by 100..
	local pos = go.get_position()
	local delta_pos = self.velocity * dt

	print("CURRENT POSITION: " .. pos)
	print("DELTA POSITION " .. delta_pos)
	print("NEW POSITION " .. (pos + delta_pos))
	print(dt)
	go.set_position(pos + delta_pos)
end

Console Output
DEBUG:SCRIPT: CURRENT POSITION: [280.255066, 146.436356, 0.000000]
DEBUG:SCRIPT: DELTA POSITION [1.666667, 0.000000, 0.000000]
DEBUG:SCRIPT: NEW POSITION [281.921722, 146.436356, 0.000000]
DEBUG:SCRIPT: 0.016666667535901
DEBUG:SCRIPT: CURRENT POSITION: [280.255066, 146.436356, 0.000000]
DEBUG:SCRIPT: DELTA POSITION [1.666667, 0.000000, 0.000000]
DEBUG:SCRIPT: NEW POSITION [281.921722, 146.436356, 0.000000]
DEBUG:SCRIPT: 0.016666667535901
INFO:DLIB: SSDP: Done on address 192.168.2.20

Your code works fine here - my test sprite slowly works it’s way from left to right.

Is your script attached to the correct game object?

If you’re happy to share the whole project, it would be easier to troubleshoot.

2 Likes

Thanks for the quick reply,

I’m not sure how to share the project. Unless i have to give your account access to it.

I have a wizard.go file that is applied to my level.atlas.

Heres how it looks

Okay, no worries. Your collision object may be set to “dynamic” - try setting it to “kinematic” and running the code again.

6 Likes

That fixed it, so dynamic type collision is handled by the physics engine as for kinematic the behavior is defined by my script.

Thanks for your help!

6 Likes