Velocity of another Game Object?

I’m working on aiming at a moving target.

I have a (hashed) ID of the separate target game object, but it’s linear_velocity resides on its #collisionobject component.

What’s the prescribed way to get velocity of a separate GO in this case knowing ID?

It seems to me that you cannot read the velocity of the colliding object from another object.

So the way to obtain this information is to send a message requesting it and, on the other object, send back a message with this data.

There seem to be a lot of guessing here, but what would help us understand the problem is if you shared how you’re trying to get the velocity.

We currently have two ways.

Old (not yet deprecated) API reference (physics)

local velocity = go.get(url, "linear_velocity")

If you have a valid url, you should be able to get the velocity of any collision object.

New api (box2d) API reference (b2d.body)

local body = b2d.get_body(url)
local velocity = b2d.body.get_linear_velocity(body)

It would be helpful to know which of the approaches you tried and the errors you received, to better understand the problem.

1 Like

Reading this further, the question is more like “how do I create a url for my collision object?”

As you only have the ID, you need to rely on using same name of the collision object component:

local url = msg.url(nil, id_hash, "collisionobject")

I recommend reading this manual

3 Likes

I apologize for the misinformation in the previous answer. I actually didn’t know that it was possible to access the collision object data of other objects directly.

At least now I learned how to do that too.

1 Like

I think that’s what I was looking for. I knew about msg.url, but didn’t realize it could also take the hashed ID and reference a specific component by name. Thanks!

You mentioned that go.get linear_velocity was to be depreciated. Does that also apply to 3D? My application is actually using 3D physics.

Eventually, yes. But currently, there is no bullet3d Lua module to replace it with, so it will still be used for some more time.

1 Like