# Gameshow move object to another object position ?

#### sager

##### Member
how move object to another object position ?
and control with speed and direction

• fredo

#### Gunther

##### Active Member
move object to another without path
B4X:
``thing.body.Position = X2.CreateVec2( x1, y1 )``
this moves the "thing" at position x1,y1 (in the next GameLoop = at once)

#### sager

##### Member
B4X:
``thing.body.Position = X2.CreateVec2( x1, y1 )``
Maybe I did not explain what I want correctly,
I want the enemy move to player Position
i try : template.BodyDef.LinearVelocity
But it gave me different results

#### Gunther

##### Active Member
Well, that is for sure different!

1) calculate the direction vector by subtracting the enemy Position from the Player position
2) normalize the direction vector
3) now your approach is taken: set the LinearVelocity with this new vector from 2)
4) may be it shall go faster or slower then in step 3) you should multiply the new vector 2) with a scalar

You may add a more sofisticated solution by using "easing" (check google on 'easing')

#### sager

##### Member
Well, that is for sure different!

1) calculate the direction vector by subtracting the enemy Position from the Player position
2) normalize the direction vector
3) now your approach is taken: set the LinearVelocity with this new vector from 2)
4) may be it shall go faster or slower then in step 3) you should multiply the new vector 2) with a scalar

You may add a more sofisticated solution by using "easing" (check google on 'easing')
can you give me simple example , thanks

#### Erel

Staff member
• sager and inakigarm

#### Gunther

##### Active Member
give me simple example
Erel modified his Mouse Example and this is the code extract that I explained in post #7:
B4X:
``````Private Sub MoveEnemy
Dim v As B2Vec2 = Mouse.Body.Position.CreateCopy
v.SubtractFromThis(Enemy.Body.Position)
If v.Length > 0.7 Then ' <--- check, if they are too close to each other
v.NormalizeThis
' you could multiply that v by a scalar to change the speed like:
' v.MultiplyThis(1.5) ' >1 = faster, <1 = slower
Enemy.Body.LinearVelocity = v
Enemy.Body.SetTransform(Enemy.Body.Position, ATan2(v.Y, v.X) + cPI / 2) ' <--- turning the enemy in player direction
Else
Enemy.Body.LinearVelocity = X2.CreateVec2(0, 0) ' <--- yes, too close, therefore stop enemy
Enemy.Body.AngularVelocity = 0 ' <--- stops the spinning
End If

End Sub``````

Last edited: