Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Tybobobo committed Jul 5, 2017
0 parents commit 11f107c
Show file tree
Hide file tree
Showing 11 changed files with 332 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License (MIT)

Copyright (c) 2017 Ivan P. Skodje

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Vehicle Controller 2D Plugin for Godot Engine
![](icon.png)

A 2D top-down vehicle controller node.
A demo scene is included.

**Contributions to this addon is encouraged.**

## How to implement in your project

1. Search for the 'VehicleController2D' Node and place it in your Scene.
2. Add your own Sprite so that you can see what is going on
3. Select the VehicleController2D node and make sure to add the Input Map, or write your own custom Input Map names.
4. Configure from the Editor to fit your needs

## Screenshot
![](https://i.imgur.com/EaS5zjf.png)
![](https://i.imgur.com/gYSPj44.png)


## License

MIT License (MIT)

Copyright (c) 2017 Ivan P. Skodje

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
21 changes: 21 additions & 0 deletions addons/vehicle_controller_2d/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License (MIT)

Copyright (c) 2017 Ivan P. Skodje

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
87 changes: 87 additions & 0 deletions addons/vehicle_controller_2d/demo/demo.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
[gd_scene load_steps=5 format=1]

[ext_resource path="res://addons/vehicle_controller_2d/demo/track.png" type="Texture" id=1]
[ext_resource path="res://addons/vehicle_controller_2d/top_down_vehicle.gd" type="Script" id=2]
[ext_resource path="res://addons/vehicle_controller_2d/icon.png" type="Texture" id=3]
[ext_resource path="res://addons/vehicle_controller_2d/demo/vehicle.png" type="Texture" id=4]

[node name="demo" type="Control"]

focus/ignore_mouse = false
focus/stop_mouse = true
size_flags/horizontal = 2
size_flags/vertical = 2
margin/left = 0.0
margin/top = 0.0
margin/right = 40.0
margin/bottom = 40.0

[node name="race_track" type="Sprite" parent="."]

transform/pos = Vector2( 2745.82, 175.938 )
transform/scale = Vector2( 5, 5 )
texture = ExtResource( 1 )

[node name="VehicleController2D" type="RigidBody2D" parent="."]

input/pickable = false
collision/layers = 1
collision/mask = 1
mode = 0
mass = 1.0
friction = 1.0
bounce = 0.0
gravity_scale = 1.0
custom_integrator = false
continuous_cd = 0
contacts_reported = 0
contact_monitor = false
sleeping = false
can_sleep = true
velocity/linear = Vector2( 0, 0 )
velocity/angular = 0.0
damp_override/linear = -1.0
damp_override/angular = -1.0
script/script = ExtResource( 2 )
__meta__ = {
"_editor_icon": ExtResource( 3 )
}
input_steer_left = "steer_left"
input_steer_right = "steer_right"
input_accelerate = "accelerate"
input_break = "break"
acceleration = 18
max_forward_velocity = 2000
drag_coefficient = 0.99
steering_torque = 3.75
steering_damp = 8
can_drift = true
wheel_grip_sticky = 0.85
wheel_grip_slippery = 0.99
drift_extremum = 250
drift_asymptote = 20

[node name="sprite" type="Sprite" parent="VehicleController2D"]

texture = ExtResource( 4 )

[node name="camera_2d" type="Camera2D" parent="VehicleController2D"]

anchor_mode = 1
rotating = false
current = true
zoom = Vector2( 3, 3 )
limit/left = -10000000
limit/top = -10000000
limit/right = 10000000
limit/bottom = 10000000
drag_margin/h_enabled = true
drag_margin/v_enabled = true
smoothing/enable = false
smoothing/speed = 5.0
drag_margin/left = 0.2
drag_margin/top = 0.2
drag_margin/right = 0.2
drag_margin/bottom = 0.2


Binary file added addons/vehicle_controller_2d/demo/track.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added addons/vehicle_controller_2d/demo/vehicle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added addons/vehicle_controller_2d/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions addons/vehicle_controller_2d/plugin.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[plugin]

name="Vehicle Controller 2D"
description="A Vehicle Controller Node for top-down games that allow you to easily add driving vehicles in your games."
author="Ivan P. Skodje"
version="1.0"
script="vehicle_controller_2d.gd"
141 changes: 141 additions & 0 deletions addons/vehicle_controller_2d/top_down_vehicle.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
extends RigidBody2D

# ---------------------------------------------------------
# QUICK HOW-TO:
# - Make your changes in the Editor by selecting the Node
# - Add Input Map for all input strings in the Editor
# - NB: If you do not use Joystick Left/Right Triggers update Input code.
# - NB: If you do not use Left Joystick update Input code.
# ---------------------------------------------------------

# Input Map Strings - Must be set in Project Settings
export (String) var input_steer_left = "steer_left"
export (String) var input_steer_right = "steer_right"
export (String) var input_accelerate = "accelerate"
export (String) var input_break = "break"

# Joystick Deadzone Thresholds
var stick_min = 0.07 # If the axis is smaller, behave as if it were 0

# Driving Properties
export (int) var acceleration = 18
export (int) var max_forward_velocity = 2000
export (float, 0, 1, 0.001) var drag_coefficient = 0.99 # Recommended: 0.99 - Affects how fast you slow down
export (float, 0, 10, 0.01) var steering_torque = 3.75 # Affects turning speed
export (float, 0, 20, 0.1) var steering_damp = 8 # 7 - Affects how fast the torque slows down

# Drifting & Tire Friction
export (bool) var can_drift = true
export (float, 0, 1, 0.001) var wheel_grip_sticky = 0.85 # Default drift coef (will stick to road, most of the time)
export (float, 0, 1, 0.001) var wheel_grip_slippery = 0.99 # Affects how much you "slide"
export (int) var drift_extremum = 250 # Right velocity higher than this will cause you to slide
export (int) var drift_asymptote = 20 # During a slide you need to reduce right velocity to this to gain control
var _drift_factor = wheel_grip_sticky # Determines how much (or little) your vehicle drifts

# Vehicle velocity
var _velocity = Vector2(0, 0)

# Start
func _ready():
# Top Down Physics
set_gravity_scale(0.0)

# Added steering_damp since it may not be obvious at first glance that
# you can simply change angular_damp to get the same effect
set_angular_damp(steering_damp)

# Enable fixed process
set_fixed_process(true)


# Fixed Process
func _fixed_process(delta):
# Drag (0 means we will never slow down ever. Like being in space.)
_velocity *= drag_coefficient

# If we can drift
if(can_drift):
# If we are sticking to the road
if(_drift_factor == wheel_grip_sticky):
# If we exceed max stick velocity, begin sliding on the road
if(get_right_velocity().length() > drift_extremum):
_drift_factor = wheel_grip_slippery
# print("SLIDING!")
# If we are sliding on the road
else:
# If our side velocity is less than the drift asymptote, begin sticking to the road
if(get_right_velocity().length() < drift_asymptote):
_drift_factor = wheel_grip_sticky
# print("STICKING!")

# Add drift to velocity
_velocity = get_up_velocity() + (get_right_velocity() * _drift_factor)

# Accelerate
if(Input.is_action_pressed(input_accelerate)):
# TODO: Find a better way to handle this instead of hard-coding the check for Triggers
var axis = Input.get_joy_axis(0, 7) # Right Trigger
if(axis == 0):
axis = 1 # Set it to 1 since we are not using the trigger

_velocity += get_up() * acceleration * axis
# Break / Reverse
elif(Input.is_action_pressed(input_break)):
# TODO: Find a better way to handle this instead of hard-coding the check for Triggers
var axis = Input.get_joy_axis(0, 6) # Left Trigger
if(axis == 0):
axis = 1 # Set it to 1 since we are not using the trigger

_velocity -= get_up() * acceleration * axis

# Prevent exceeding max velocity
#
# This is done by getting a Vector2 that points up
# (the vehicle's default forward direction),
# and rotate it to the same amount our vehicle is rotated.
# Then we keep the magnitude of that direction which allows
# us to calculate the max allowed velocity in that direction.
var max_speed = (Vector2(0, -1) * max_forward_velocity).rotated(get_rot())
var x = clamp(_velocity.x, -abs(max_speed.x), abs(max_speed.x))
var y = clamp(_velocity.y, -abs(max_speed.y), abs(max_speed.y))
_velocity = Vector2(x, y)

# Torque depends that the vehicle is moving
var torque = lerp(0, steering_torque, _velocity.length() / max_forward_velocity)

# Steer Left
if(Input.is_action_pressed(input_steer_left)):
# TODO: Find a better way to handle this instead of hard-coding the check for Left Stick Axis
var axis = Input.get_joy_axis(0, 0) # Left Stick Axis
if(axis < stick_min):
axis = 1 # Set it to 1 since we are not using the left stick

set_angular_velocity(-torque * abs(axis))

# Steer Right
elif(Input.is_action_pressed(input_steer_right)):
# TODO: Find a better way to handle this instead of hard-coding the check for Left Stick Axis
var axis = Input.get_joy_axis(0, 0) # Left Stick Axis
if(axis < stick_min):
axis = 1 # Set it to 1 since we are not using the left stick

set_angular_velocity(torque * abs(axis))

# Apply the force
set_linear_velocity(_velocity)

# Returns up direction (vehicle's forward direction)
func get_up():
return Vector2(cos(get_rot() + PI/2.0), sin(get_rot() - PI/2.0))

# Returns right direction
func get_right():
return Vector2(cos(get_rot()), sin(-get_rot()))

# Returns up velocity (vehicle's forward velocity)
func get_up_velocity():
return get_up() * _velocity.dot(get_up())

# Returns right velocity
func get_right_velocity():
return get_right() * _velocity.dot(get_right())
12 changes: 12 additions & 0 deletions addons/vehicle_controller_2d/vehicle_controller_2d.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
tool
extends EditorPlugin

# On Scene Tree Enter
func _enter_tree():
# Load the Top Down Vehicle controller (since we have no other options yet)
add_custom_type("VehicleController2D", "RigidBody2D", preload("top_down_vehicle.gd"), preload("icon.png"))

# On Scene Tree Exit
func _exit_tree():
# Clean-up
remove_custom_type("VehicleController2D")
Binary file added icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 11f107c

Please sign in to comment.