# [Solved] Best Way to Move an Object Towards a Point

I’m trying to implement a simple movement of an object towards a given point (currently I have to use rigidBodyControl, ideally I’d use character control). The general Idea that I’ve come up with is:

1. decide point to move towards
2. use the spatial.lookAt() method to set the vector3f
3. use spatial.getLocalRotation() to get the vector3f
4. use setLinearVelocity() to move the spatial

This seems like a very roundabout way of doing it, not to mention the fact that getLocalRotation() returns a quaternion, not a vector3f, so there would have to be some way of converting it…

Is there a better way to do this??? (I’ve read through the javadocs I thought were relevant, and the physics docs)

It is different with RigidBodyControl and CharacterControl. With CharacterControl you can use setWalkDirection. If you do not want to change rotation of your object but just move it towards a point you do not have to mess with the rotation. You can stick to vectors.

Vector math: https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:math_for_dummies

@chessmaster942 , sorry man, but you cannot use setLinearVelocity()/setPhysicsRotation() to move an object constantly. It was discussed here:

http://hub.jmonkeyengine.org/groups/free-announcements/forum/topic/jme-simple-examples-project?topic_page=2&num=15

Example how to move and rotate riggidBody can be found here:

(see NPCPhysics and ShipPhysics)

@mifth

Oh, ok, I see how to do it, where did you find the source code for those examples, they really helped.

@cmeyer

Thanks for the resources on physics. I didn’t look at walking_character, and the math for dummies was a great review.

you can use setLinearVelocity() btw

Ok, I’ll keep that in mind.

@wezrule said:
you can use setLinearVelocity() btw

@normen said only for creation of a physical object. But not apply it constantly for walking.. applyCentralForces() makes the same as setLinearVelosity().

@chessmaster942 said:
@mifth
Oh, ok, I see how to do it, where did you find the source code for those examples, they really helped.

some i did and some did other JME users. If you have any useful JME example, so i can add it there.
mifth said:normen said only for creation of a physical object. But not apply it constantly for walking.. applyCentralForces() makes the same as setLinearVelosity().

I've learned that Normen is usually right :) you should only set forces (force, torque or impulse). Velocities, positions and angles are calculated by Bullet taking all sorts of constraints into account. If you set any of them directly you break the calculations and have to bite the bullet. That's how I learned :^)

It might even work if its only setVelocity as I guess thats whats used for collision/impact calculations in the end. As mifths “tests” showed it works to some extent. But bullet compute the physics effects then to immediately drop them again when you set the velocity afterwards which is kind of pointless and just produces overhead. Thats what the kinematic mode is made for. Also I guess internal messups due to the differing physics results vs velocities will raise their ugly head at some point with setVelocity. As @jmaasing hinted bullet makes a lot of assumptions based on the type of object.

That makes sense, and it sounds like I definatly need to look into using kinematic view more.

for applyCentralForces() to be the same as setLinearVelocity() you need to find exactly the right amount of force to apply each tick to give a constant velocity, they are not the same thing i’m afraid.

i still think setLinearVelocity()/setAngularVelocity() is safe to do, and I have found good applications for it, but do as you wish

@wezrule said:
for applyCentralForces() to be the same as setLinearVelocity() you need to find exactly the right amount of force to apply each tick to give a constant velocity, they are not the same thing i'm afraid.

i still think setLinearVelocity()/setAngularVelocity() is safe to do, and I have found good applications for it, but do as you wish :)

Ok I looked a bit and heres at least one example why setVelocity isn't good, it doesn't translate forces to objects connected via joints: http://www.bulletphysics.org/Bullet/phpBB3/viewtopic.php?f=9&t=2087

yeh, but that is a 4 year old post about a missing feature in btGeneric6DofConstraint, probably fixed by now

out of topic: From this wiki page, i found that you can force bullet into 2D very easily.

http://www.bulletphysics.org/mediawiki-1.5.8/index.php/Code_Snippets#I_want_to_constrain_an_object_to_two_dimensional_movement.2C_skipping_one_of_the_cardinal_axes

rigidBodyControl.setAngularFactor() exists but rigidBodyControl.setLinearFactor() doesn’t, could this be added normen?

@wezrule said:
rigidBodyControl.setAngularFactor() exists but rigidBodyControl.setLinearFactor() doesn't, could this be added normen? :)

Nope cause it doesn't exist in jbullet.

that sucks np we moving to native bullet completely soon right?

@wezrule said:
that sucks :( np we moving to native bullet completely soon right? :)

Not before 3.1, no.

ok thanks

A little more on topic, I was going through the walking character control (https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:walking_character) and read that the setWalkDirection() method didn’t control the direction of travel, simply the speed. So how do I change the direction of travel? For my purpose, I need another character control, that doesn’t rely on camLeft and camDir, so what are you adding to the vector, when you add them to it??? (what is inside camLeft and camDir, direction, rotation, etc.)

The tutorials explain this. The direction the vector is pointing in the move direction, its length is the speed. The collision shape doesn’t rotate but thats irrelevant for spheres and (upright) capsules. You can rotate the attached model using the lookDirection

1 Like

Ok, sorry. I misread it several times. I’m not sure how…