Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Entity (1) #14

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
* The MIT License
*
* Copyright 2018 The OpenNARS authors.
*
* 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.
*/
package com.opennars.applications.crossing;

import org.opennars.entity.TruthValue;

import java.util.List;

/**
* Component for the behaviour of an entity
*/
public class BehaviourComponent {
private final EnumType type;

// initial angle (for the pedestriant)
public double initialAngle;

public float maxSpeed = 2.0f;



public BehaviourComponent(final EnumType type) {
this.type = type;
}

public void tick(Entity entity, List<Street> streets, List<TrafficLight> trafficLights, List<Entity> entities, TruthValue truth, long time) {
if (type == EnumType.CAR) {
carTick(entity, streets, trafficLights, entities, truth, time);
}
else if (type == EnumType.PEDESTRIAN) {
carTick(entity, streets, trafficLights, entities, truth, time);



entity.angle+=(Util.rnd.nextFloat()*0.1-0.05);
//ok pedestrian, don't go on grass
boolean forPedestrians = false;
for(Street street : streets) {
if(!street.forCarsOnly && entity.posX > street.startX && entity.posX < street.endX && entity.posY > street.startY && entity.posY < street.endY) {
forPedestrians = true;
break;
}
}


if(!forPedestrians) {
entity.angle = initialAngle;
entity.posX = entity.prevX;
entity.posY = entity.prevY;
}
}
}


static protected void carTick(Entity entity, List<Street> streets, List<TrafficLight> trafficLights, List<Entity> entities, TruthValue truth, long time) {
if(truth != null) {
return;
}

boolean accelerate = true;
for (TrafficLight l : trafficLights) {
if (Util.distance(entity.posX, entity.posY, l.posX, l.posY) < l.radius) {
if (l.colour == l.RED) {
if (Util.rnd.nextFloat() > 0.3 && ((entity.tag.equals("car") && !entity.carIgnoreTrafficLight) || (entity.tag.equals("pedestrian") && !entity.pedestrianIgnoreTrafficLight))) {
entity.velocity *= 0.5;
accelerate = false;
}
}
}
}
for (Entity e : entities) {
boolean collidable = !(entity.tag.equals("pedestrian") && e.tag.equals("pedestrian"));
if (e != entity && collidable) {
double nearEnough = 10;
for (double k = 0; k < nearEnough; k += 0.1) {
double pXNew = entity.posX + k * Math.cos(entity.angle);
double pYNew = entity.posY + k * Math.sin(entity.angle);
if (Util.distance(pXNew, pYNew, e.posX, e.posY) < nearEnough) {
entity.velocity *= 0.8;
accelerate = false;
}
}
}
}

if (accelerate && entity.velocity < entity.behaviour.maxSpeed) {
entity.velocity += 0.02;
}

double aX = Math.cos(entity.angle);
double aY = Math.sin(entity.angle);
entity.posX += aX * entity.velocity;
entity.posY += aY * entity.velocity;

double epsilon = 1;
if (entity.posY < 0) {
entity.posY = 1000 - epsilon;
//this.id = entityID++;
}
if (entity.posY > 1000) {
entity.posY = epsilon;
//this.id = entityID++;
}
if (entity.posX < 0) {
entity.posX = 1000 - epsilon;
//this.id = entityID++;
}
if (entity.posX > 1000) {
entity.posX = epsilon;
//this.id = entityID++;
}
}

public enum EnumType {
CAR,
PEDESTRIAN,
}

}
52 changes: 0 additions & 52 deletions src/main/java/com/opennars/applications/crossing/Car.java

This file was deleted.

35 changes: 28 additions & 7 deletions src/main/java/com/opennars/applications/crossing/Crossing.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ public void setup() {
trafficLights.add(new TrafficLight(trafficLightID++, trafficLightRadius/2, 500, 500 - trafficLightRadius, 1));
int cars = 4; //cars and pedestrians
for (float i = 0; i < cars/2; i += 1.05) {
entities.add(new Car(entityID++, 500 + streetWidth - Util.discretization+1, 900 - i * 100, 0.3, -PI / 2));
entities.add(new Car(entityID++, 500 + Util.discretization, 900 - i * 100, 0.3, PI / 2));
entities.add(makeEntity("car", entityID++, 500 + streetWidth - Util.discretization+1, 900 - i * 100, 0.3, -PI / 2));
entities.add(makeEntity("car", entityID++, 500 + Util.discretization, 900 - i * 100, 0.3, PI / 2));
}
int pedestrians = 4;//4;
for (float i = 0; i < pedestrians/2; i += 1.05) {
entities.add(new Pedestrian(entityID++, 900 - i * 100, 500 + streetWidth - Util.discretization, 0.3, 0));
entities.add(new Pedestrian(entityID++, 900 - i * 100, 500 + Util.discretization, 0.3, -PI));
entities.add(makeEntity("pedestrian", entityID++, 900 - i * 100, 500 + streetWidth - Util.discretization, 0.3, 0));
entities.add(makeEntity("pedestrian", entityID++, 900 - i * 100, 500 + Util.discretization, 0.3, -PI));
}
/*for (TrafficLight l : trafficLights) { //it can't move anyway, so why would the coordinates matter to NARS?
String pos = Util.positionToTerm(l.posX, l.posY);
Expand Down Expand Up @@ -126,7 +126,7 @@ public void draw() {

// tick
for (Entity ie : entities) {
ie.tick();
ie.tick(streets, trafficLights, entities, null, 0);
}


Expand All @@ -141,10 +141,10 @@ public void draw() {
if(showAnomalies) {
for (Prediction pred : disappointments) {
Entity e = pred.ent;
if(e instanceof Car) {
if(e.tag.equals("car")) {
fill(255,0,0);
}
if(e instanceof Pedestrian) {
if(e.tag.equals("pedestrian")) {
fill(0,0,255);
}
this.text("ANOMALY", (float)e.posX, (float)e.posY);
Expand Down Expand Up @@ -190,6 +190,27 @@ public void mouseDragged() {
viewport.mouseDragged();
}

public static Entity makeEntity(final String tag, int id, double posX, double posY, double velocity, double angle) {
if (tag.equals("car")) {
Entity entity = new Entity(id, posX, posY, velocity, angle, "car", new BehaviourComponent(BehaviourComponent.EnumType.CAR));
entity.behaviour.maxSpeed = 2;
return entity;
}
else if (tag.equals("pedestrian")) {
final float pedestrianScale = 0.75f;

Entity entity = new Entity(id, posX, posY, velocity, angle, "pedestrian", new BehaviourComponent(BehaviourComponent.EnumType.PEDESTRIAN));
entity.behaviour.initialAngle = angle;
entity.behaviour.maxSpeed = 1;
entity.scale = pedestrianScale;
return entity;
}
else {
return null;
}
}


public static void main(String[] args) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
Expand Down
Loading