Skip to content

Commit

Permalink
bug fix for sensors with noninteractive and invisble agents
Browse files Browse the repository at this point in the history
  • Loading branch information
klavins committed Mar 17, 2020
1 parent a19cdb6 commit 39f8c41
Show file tree
Hide file tree
Showing 29 changed files with 564 additions and 105 deletions.
6 changes: 3 additions & 3 deletions client/src/enviro.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,12 @@ class Arena extends React.Component {
target_x = origin_x - offset_x,
target_y = origin_y - offset_y;

if ( this.dist(CX,CY, target_x, target_y ) > 35 ) {
if ( this.dist(CX,CY, target_x, target_y ) > 100 ) {
CX = target_x;
CY = target_y;
} else {
CX += 0.2 * (origin_x - offset_x - CX);
CY += 0.2 * (origin_y - offset_y - CY);
CX += 0.075 * (origin_x - offset_x - CX);
CY += 0.075 * (origin_y - offset_y - CY);
}

let center = `scale(${zoom}) translate(${CX} ${CY})`;
Expand Down
8 changes: 7 additions & 1 deletion examples/multiuser/defs/guy.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@
"linear": 40,
"rotational": 600
},
"sensors": [],
"sensors": [
{
"type": "range",
"location": { "x": 17, "y": 0 },
"direction": 0
}
],
"mass": 1,
"controller": "lib/guy.so"
}
10 changes: 10 additions & 0 deletions examples/spawner/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
TARGET := bin/enviro

all:
$(MAKE) -C src all

clean:
$(MAKE) -C src clean



34 changes: 34 additions & 0 deletions examples/spawner/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{

"name": "My Enviro Project",
"ip": "0.0.0.0",
"port": 8765,

"buttons": [],

"agents": [
{
"definition": "defs/guy.json",
"style": { "fill": "azure", "stroke": "black" },
"position": { "x": 0, "y": -200, "theta": 0 }
}
],

"invisibles": [
{
"definition": "defs/coordinator.json",
"rendered": false
}
],

"references": [
{
"definition": "defs/thing.json",
"style": { "fill": "azure", "stroke": "black" },
"position": { "x": 0, "y": 0, "theta": 0 }
}
],

"statics": []

}
1 change: 1 addition & 0 deletions examples/spawner/defs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Put agent definitions in this folder.
6 changes: 6 additions & 0 deletions examples/spawner/defs/coordinator.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "Coordinator",
"type": "invisible",
"description": "Runs the whole show, but isn't rendered",
"controller": "lib/coordinator.so"
}
32 changes: 32 additions & 0 deletions examples/spawner/defs/guy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "Guy",
"type": "dynamic",
"description": "A guy that users can move around the screen",
"shape": [
{ "x": -10, "y": 10 },
{ "x": 10, "y": 10 },
{ "x": 10, "y": 2 },
{ "x": 12, "y": 2 },
{ "x": 12, "y": 6 },
{ "x": 16, "y": 6 },
{ "x": 16, "y": 4 },
{ "x": 14, "y": 4 },
{ "x": 14, "y": -4},
{ "x": 16, "y": -4 },
{ "x": 16, "y": -6 },
{ "x": 12, "y": -6 },
{ "x": 12, "y": -2 },
{ "x": 10, "y": -2 },
{ "x": 10, "y": -10 },
{ "x": -10, "y": -10 },
{ "x": -9, "y": 0 }
],
"friction": {
"collision": 5,
"linear": 40,
"rotational": 600
},
"sensors": [],
"mass": 1,
"controller": "lib/guy.so"
}
19 changes: 19 additions & 0 deletions examples/spawner/defs/thing.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "Thing",
"type": "dynamic",
"description": "Replace this string with a description",
"shape": [
{ "x": -5, "y": 5 },
{ "x": 5, "y": 5 },
{ "x": 5, "y": -5 },
{ "x": -5, "y": -5 }
],
"friction": {
"collision": 5,
"linear": 40,
"rotational": 600
},
"sensors": [],
"mass": 1,
"controller": "lib/thing.so"
}
1 change: 1 addition & 0 deletions examples/spawner/lib/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Shared object libraries will be place here by make.
38 changes: 38 additions & 0 deletions examples/spawner/src/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#Architecture
ARCH := $(shell uname -m)

#Compilers
CC := g++ -std=c++17 -Wno-psabi

#The Target Library

#The Directories, Source, Includes, Objects, Binary and Resources
SRCEXT := cc

# Directories
CHIPDIR := /usr/local/src/Chipmunk2D
ENVIRODIR := /usr/local/src/enviro/server/include

#Flags, Libraries and Includes
CFLAGS := -ggdb -shared -fPIC
INCLUDE := -I $(ENVIRODIR) -I $(CHIPDIR)/include/chipmunk

#Files

TARGETDIR := ../lib
SOURCES := $(wildcard *.cc)
HEADERS := $(wildcard *.h)
TARGETS := $(patsubst %.cc,%.so,$(wildcard *.cc))
FULL_TARGETS := $(addprefix $(TARGETDIR)/, $(TARGETS))

#Default Make
all: $(FULL_TARGETS)

#Clean only Objects
clean:
@$(RM) -rf $(TARGETDIR)/*.so

# Compile
$(TARGETDIR)/%.so: %.cc %.h
$(CC) $(CFLAGS) $(INCLUDE) $< -o $@

6 changes: 6 additions & 0 deletions examples/spawner/src/coordinator.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <iostream>
#include "coordinator.h"

using namespace enviro;

// Put your implementations here
37 changes: 37 additions & 0 deletions examples/spawner/src/coordinator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#ifndef __COORDINATOR_AGENT__H
#define __COORDINATOR_AGENT__H

#include "enviro.h"

using namespace enviro;

class CoordinatorController : public Process, public AgentInterface {

public:
CoordinatorController() : Process(), AgentInterface() {}

void init() {
for ( double x = -200; x <= 200; x += 50) {
for ( double y = 0; y <= 200; y += 50 ) {
add_agent("Thing", x, y, 0, { {"fill", "blue"}});
}
}
}
void start() {}
void update() {}
void stop() {}

};

class Coordinator : public Agent {
public:
Coordinator(json spec, World& world) : Agent(spec, world) {
add_process(c);
}
private:
CoordinatorController c;
};

DECLARE_INTERFACE(Coordinator)

#endif
6 changes: 6 additions & 0 deletions examples/spawner/src/guy.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <iostream>
#include "guy.h"

using namespace enviro;

// Put your implementations here
63 changes: 63 additions & 0 deletions examples/spawner/src/guy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#ifndef __PLAYER_AGENT__H
#define __PLAYER_AGENT__H

#include "enviro.h"

using namespace enviro;

class GuyController : public Process, public AgentInterface {

public:
GuyController() : Process(), AgentInterface(), v(0), omega(0) {}

void init() {
watch("keydown", [&](Event &e) {
auto k = e.value()["key"].get<std::string>();
if ( k == "w" ) {
v = v_m;
} else if ( k == "s" ) {
v = -v_m;
} else if ( k == "a" ) {
omega = -omega_m;
} else if ( k == "d" ) {
omega = omega_m;
}
});
watch("keyup", [&](Event &e) {
auto k = e.value()["key"].get<std::string>();
if ( k == "w" || k == "s" ) {
v = 0;
} else if ( k == "a" ) {
omega = 0;
} else if ( k == "d" ) {
omega = 0;
}
});
}
void start() {}
void update() {
track_velocity(v,omega,10,400);
label(std::to_string((int) x()) + ", " + std::to_string((int)y()),20,20);
emit(Event("guy_position", { { "x", x()}, {"y", y() }}));
}
void stop() {}

double v, omega;
double const v_m = 30, omega_m = 1;
double const magnitude = 200;

};

class Guy : public Agent {
public:
Guy(json spec, World& world) : Agent(spec, world) {
add_process(c);
}
private:
GuyController c;

};

DECLARE_INTERFACE(Guy)

#endif
6 changes: 6 additions & 0 deletions examples/spawner/src/thing.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <iostream>
#include "thing.h"

using namespace enviro;

// Put your implementations here
40 changes: 40 additions & 0 deletions examples/spawner/src/thing.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#ifndef __THING_AGENT__H
#define __THING_AGENT__H

#include "enviro.h"

using namespace enviro;

class ThingController : public Process, public AgentInterface {

public:
ThingController() : Process(), AgentInterface(), guy_x(0), guy_y(0) {}

void init() {
watch("guy_position", [&](Event& e) {
guy_x = e.value()["x"];
guy_y = e.value()["y"];
});
}
void start() {}
void update() {
omni_move_toward(guy_x, guy_y, 0.1);
}
void stop() {}

double guy_x, guy_y;

};

class Thing : public Agent {
public:
Thing(json spec, World& world) : Agent(spec, world) {
add_process(c);
}
private:
ThingController c;
};

DECLARE_INTERFACE(Thing)

#endif
Loading

0 comments on commit 39f8c41

Please sign in to comment.