Understanding Velocity Acceleration Force
Demonstration of velocity, acceleration, and force. The relationship between them. When to use one versus others in game development. Implementation in Buildbox Engine and Unity Engine.
Downloads:
For Buildbox you can download bbasset file, drag and drop it in your projects world window. A "VAF Nodes v_1.0" object will be added to your asset list, you can go inside node-editor and copy over the node you what to use. To copy select the node you copying, and press "Command+C" (Mac) or "Ctrl+C" (Windows). Go to your asset and past by pressing "Command+V" (Mac) or "Ctrl+V" (Windows)
Velocity
Buildbox 3 script:
Required: Vector3D "Velocity" attribute and 1 input
let _velocity;
let _physics;
function init(){
_velocity = this.attribute('Velocity');
_physics = this.entity().physics();
}
function signal(name, value){
if(value){
let vel = _physics.linearVelocity();
if(_velocity.x != null){
vel.x = _velocity.x;
}
if(_velocity.y != null){
vel.y = _velocity.y;
}
if(_velocity.z != null){
vel.z = _velocity.z;
}
_physics.setLinearVelocity( vel.x, vel.y, vel.z );
}
}
Unity code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Velocity : MonoBehaviour
{
private Rigidbody2D r2d;
public Vector2 velocity;
void Start()
{
r2d = GetComponent();
r2d.velocity = velocity;
}
}
Acceleration
Buildbox 3 script:
Required: Vector3D "Acceleration" attribute and 1 input
let _acceleration;
let _physics;
let _go = false;
function init(){
_acceleration = this.attribute('Acceleration');
_physics = this.entity().physics();
}
function update(dt){
if(_go){
let vel = _physics.linearVelocity();
if(_acceleration.x != null){
vel.x += _acceleration.x*dt;
}
if(_acceleration.y != null){
vel.y += _acceleration.y*dt;
}
if(_acceleration.z != null){
vel.z += _acceleration.z*dt;
}
_physics.setLinearVelocity( vel.x, vel.y, vel.z );
}
}
function signal(name, value){
_go=value;
}
Unity code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Acceleration : MonoBehaviour
{
private Rigidbody2D r2d;
public Vector2 acceleration;
void Start()
{
r2d = GetComponent();
}
void FixedUpdate()
{
r2d.AddForce(acceleration * r2d.mass);
}
}
Force
Buildbox 3 script:
Required: Vector3D "Force" attribute and 1 input
let _force;
let _physics;
let _go = false;
function init(){
_force = this.attribute('Force');
_physics = this.entity().physics();
}
function update(dt){
if(_go){
let vel = _physics.linearVelocity();
let mass = _physics.mass();
if(_force.x != null){
vel.x += _force.x*dt/mass;
}
if(_force.y != null){
vel.y += _force.y*dt/mass;
}
if(_force.z != null){
vel.z += _force.z*dt/mass;
}
_physics.setLinearVelocity( vel.x, vel.y, vel.z );
}
}
function signal(name, value){
_go=value;
}
Unity code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Force : MonoBehaviour
{
private Rigidbody2D r2d;
public Vector2 force;
void Start()
{
r2d = GetComponent();
}
void FixedUpdate()
{
r2d.AddForce(force);
}
}