From 65d78b3ca9ffe88571e43c561a894a47b5196b57 Mon Sep 17 00:00:00 2001 From: Robert Machmer Date: Tue, 12 Jan 2016 15:04:46 +0100 Subject: [PATCH] Fix #1 - Adjust force calculation --- Graphoon/Node.lua | 44 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 10 deletions(-) diff --git a/Graphoon/Node.lua b/Graphoon/Node.lua index 8947777..6180ec7 100644 --- a/Graphoon/Node.lua +++ b/Graphoon/Node.lua @@ -2,14 +2,14 @@ local current = (...):gsub('%.[^%.]+$', ''); local Node = {}; -local FORCE_SPRING = -0.01; -local FORCE_CHARGE = 100000; +local FORCE_SPRING = 0.005; +local FORCE_CHARGE = 200; local FORCE_MAX = 4; -local NODE_SPEED = 8; +local NODE_SPEED = 128; local DAMPING_FACTOR = 0.95; -local DEFAULT_MASS = 0.05; +local DEFAULT_MASS = 3; --- -- @param id - A unique id which will be used to reference this node. @@ -46,17 +46,37 @@ function Node.new( id, x, y, anchor ) ay = clamp( -FORCE_MAX, ay + fy, FORCE_MAX ); end + --- + -- Calculates the manhattan distance from the node's coordinates to the + -- target coordinates. + -- @param tx - The target coordinate in x-direction. + -- @param ty - The target coordinate in y-direction. + -- + local function getManhattanDistance( tx, ty ) + return px - tx, py - ty; + end + + --- + -- Calculates the actual distance vector between the node's current + -- coordinates and the target coordinates based on the manhattan distance. + -- @param dx - The horizontal distance. + -- @param dy - The vertical distance. + -- + local function getRealDistance( dx, dy ) + return math.sqrt( dx * dx + dy * dy ) + 0.1; + end + --- -- Attract this node to another node. -- @param node - The node to use for force calculation. -- function self:attractTo( node ) - local dx, dy = px - node:getX(), py - node:getY(); - local distance = math.sqrt(dx * dx + dy * dy); + local dx, dy = getManhattanDistance( node:getPosition() ); + local distance = getRealDistance( dx, dy ); dx = dx / distance; dy = dy / distance; - local strength = FORCE_SPRING * distance; + local strength = -1 * FORCE_SPRING * distance * 0.5; applyForce( dx * strength, dy * strength ); end @@ -65,12 +85,12 @@ function Node.new( id, x, y, anchor ) -- @param node - The node to use for force calculation. -- function self:repelFrom( node ) - local dx, dy = px - node:getX(), py - node:getY(); - local distance = math.sqrt(dx * dx + dy * dy); + local dx, dy = getManhattanDistance( node:getPosition() ); + local distance = getRealDistance( dx, dy ); dx = dx / distance; dy = dy / distance; - local strength = FORCE_CHARGE * ( mass / ( distance * distance )); + local strength = FORCE_CHARGE * (( mass * node:getMass() ) / ( distance * distance )); applyForce(dx * strength, dy * strength); end @@ -119,6 +139,10 @@ function Node.new( id, x, y, anchor ) mass = nmass; end + function self:getMass() + return mass; + end + return self; end