# area2d steering

Attention Topic was automatically imported from the old Question2Answer platform.

Dear Brothers,

Now I am at work and cant send you code, so the problem is trying to make a top down view tank I can move it rotate but cant move it to rotated direction, I am totally rookie, found some tutorials but they are based rigidbody2d and my node is area2d so I cant implement it because it based on forces mine is vector, could any body show me simple code.

Hakan

Take a look at the GDQuest Car Tutorial here:

Code:

It uses a kinematic body, but no forces so you should be able to adapt it to your needs.

It was written for 2.14 but only needs minimal changes to work in in 3.0 e.g. fixed_process => physics_process and move => move_and_collide etc.

Yes sir, I saw this tutorial but I think it uses forces,
many thanks, I will adapt it to 3 for sure.

kocagozhkn | 2018-03-14 15:12

Hi friends I couldn’t make it. is there any advice.

``````extends Sprite

var direction = Vector2()
var speed = 0
var acc = 1800
var decc = 3000

var motion = Vector2()
var target_motion = Vector2()
var steering = Vector2()

var Skin = null
var target_angle = 0

const MASS = 2
const MAX_SPEED = 600

# Called every time the node is added to the scene.
# Initialization here
set_process(true)

func _process(delta):
direction = Vector2()
if Input.is_action_pressed("ui_up"):
direction.y = -1
elif Input.is_action_pressed("ui_down"):
direction.y = 1
if Input.is_action_pressed("ui_right"):
direction.x = 1
elif Input.is_action_pressed("ui_left"):
direction.x = -1

if direction != Vector2():
speed += acc * delta
else:
speed -= decc * delta

speed = clamp(speed, 0, MAX_SPEED)

target_motion = speed * direction.normalized()*delta

steering = target_motion - motion

if steering.length() > 1:
steering = steering.normalized()

motion += steering / MASS

if speed == 0:
motion = Vector2()

position = motion

if motion != Vector2():
target_angle = atan2(motion.x , motion.y)-PI/2
rotation = target_angle
``````

kocagozhkn | 2018-03-14 18:24

Here is the code correctly formatted and converted to 3.0. The first issue you had was some of your code was outside the process block.

You should also set your node up as a Node2D on the base which you move and a Sprite as a child which will do the visual rotations etc. Although I would personally go with a KinematicBody2D as the base so you get things like collision detection etc. already setup.

``````extends Node2D

var direction = Vector2()
var speed = 0
var acc = 1800
var decc = 3000

var motion = Vector2()
var target_motion = Vector2()
var steering = Vector2()

var Skin = null
var target_angle = 0

const MASS = 2
const MAX_SPEED = 600

Skin = get_node("Sprite")

func _process(delta):

direction = Vector2()

if Input.is_action_pressed("ui_up"):
direction.y = -1

elif Input.is_action_pressed("ui_down"):
direction.y = 1

if Input.is_action_pressed("ui_right"):
direction.x = 1

elif Input.is_action_pressed("ui_left"):
direction.x = -1

if direction != Vector2():
speed += acc * delta
else:
speed -= decc * delta

speed = clamp(speed, 0, MAX_SPEED)

target_motion = speed * direction.normalized()*delta

steering = target_motion - motion

if steering.length() > 1:
steering = steering.normalized()

motion += steering / MASS

if speed == 0:
motion = Vector2()

position += motion

if motion != Vector2():
target_angle = atan2(motion.x , motion.y)-PI/2
rotation = target_angle
``````

Yog_Shoggoth | 2018-03-14 18:59

I am sorry but not working properly. Could you revise it for KinematicBody2d, because of I didnt make it.

kocagozhkn | 2018-03-14 19:28

Here you go. Note to get the KinematicBody2D to work properly, you’ll need to modify the scene struture as follows:

|KinematicBody2D
|-CollisionShape2D
|–Sprite

``````extends KinematicBody2D

var direction = Vector2()
var speed = 0
var acceleration = 1800
var decceleration = 3000

var motion = Vector2()
var target_motion = Vector2()
var steering = Vector2()
const MASS = 2

const MAX_SPEED = 600

var Skin = null
var target_angle = 0

Skin = get_node("Sprite")

func _physics_process(delta):
direction = Vector2()

if Input.is_action_pressed("ui_up"):
direction.y = -1
elif Input.is_action_pressed("ui_down"):
direction.y = 1

if Input.is_action_pressed("ui_left"):
direction.x = -1
elif Input.is_action_pressed("ui_right"):
direction.x = 1

if direction != Vector2():
speed += acceleration * delta
else:
speed -= decceleration * delta

speed = clamp(speed, 0, MAX_SPEED)

target_motion = speed * direction.normalized() * delta
steering = target_motion - motion

if steering.length() > 1:
steering = steering.normalized()

motion += steering / MASS

if speed == 0:
motion = Vector2()

print(motion)

move_and_collide(motion)

if motion != Vector2():
target_angle = atan2(motion.x, motion.y) - PI/2
Skin.rotation = target_angle
``````

Yog_Shoggoth | 2018-03-14 20:41