Spheres Oval Shaped, and Glitching a bit too.

So basically I thought it would be fun to add some 3D into things, make it look a bit neater. However, when I changed my ellipses into spheres, they started to act strange. Almost everyone developed or develops an egg shape to it. Not only that but it now decides to also jump out of the confining boxes a bit at times....

All I did was comment out the ellipses, and add the spheres so this is terribly odd. Thank you for any info and help you may present.

Here are some screens: (that one where it's out may or may not be a glitching one, as I do have the red radius slightly larger than the physical checking point. and you know how it may jump in a bit before bouncing out)

1 2

Using an AMD Quad Core A8 APU with Radeon Graphics (Had a frameBuffer error when tried on a laptop, will try on a mac laptop latter, and other computers)

Related Code: (Sorry about some of the explanatory notes, it's my first time for some of this stuff)

Main Tab:

final int unitRadius = 20;
final int enemyRadius = unitRadius * 2;
int enemyNum = 4;

Display display;
Enemy[] enemy = new Enemy[enemyNum]; //When we initialyze the object, we do so like an array, because it's an array now.


void setup()
{
  size(1280, 720, P3D);
  display = new Display();

  for(int i = 0; i < enemyNum; i++) //And this is how we make each one. so instead of enemy1 = new Enemy();, enemy2 = new....
  {
    enemy[i] = new Enemy();
  }

}

void draw()
{
  //println(frameRate);
  if(!mousePressed) //Pause the simulation, mostly for debugging
  {
    display.drawBackground();

    for(int i = 0; i < enemyNum; i++) //For all the enemies
    {
      enemy[i].move(); //AKA update
      enemy[i].drawEnemy();
    }
    checkCollisions();
  }

}

void checkCollisions()
{  
   for(int i = 0; i < enemyNum; i++) //Checks all enemies in enemy array, the standard wall hit protocol
   {
      if(enemy[i].location.x > 600 - enemyRadius) //Hits right wall
      {
        enemy[i].location.x = 600 - enemyRadius;
        enemy[i].velocity.x *= -1;
      }
      else if(enemy[i].location.x < 0 + enemyRadius) //Hits left wall
      {
        enemy[i].location.x = 0 + enemyRadius;
        enemy[i].velocity.x *= -1;
      }

      if(enemy[i].location.y > 600 - enemyRadius) //Hits bottom
      {
        enemy[i].location.y = 600 - enemyRadius;
        enemy[i].velocity.y *= -1;
      }
      else if(enemy[i].location.y < 0 + enemyRadius) //Hits top
      {
       enemy[i].location.y = 0 + enemyRadius;
       enemy[i].velocity.y *= -1;
      }
    }

    //Enemy-enemy collision checking
    for(int t = 0; t < enemyNum; t++) //Check all enemies
    {
      for(int g = 0; g < enemyNum; g++) //Against all other enemies
      {
        if(t == g) //Excluding itselfs
        {
          continue;
        }


        int distance = int(enemy[t].location.dist(enemy[g].location)); //get the distance between the two


        if(distance < enemyRadius * 2) //if the distance is less than twice the radius (one's, plus the other's) we have a collision
        {
          int overlap = enemyRadius * 2 - distance; //Calculate that overlap 

          enemy[t].location.x += overlap/2;  //Get rid of the overlap (usually even adds some space in the axis with more distance, in case it's a straight on hit)
          enemy[g].location.x -= overlap/2;
          enemy[t].location.y += overlap/2;
          enemy[g].location.y -= overlap/2;
        }


      }
    }


}

// A bunch of old code for what is up there
//println("enemy[t] location is " + int(enemy[t].location.x) + "," + int(enemy[t].location.y));
          //println("enemy[g] location is " + int(enemy[g].location.x) + "," + int(enemy[g].location.y));


          ////////////int overlap = enemyRadius * 2 - distance; //Calculate that overlap  
          //println("Overlap between enemy[", t, "] and enemy[", g, "] is ", overlap);
          //println("Distance between enemy[", t, "] and enemy[", g, "] is ", distance);



          //int distanceX = int(enemy[t].location.x - enemy[g].location.x) - enemyRadius*2; //same as vextor
          //int distanceY = int(enemy[t].location.y - enemy[g].location.y) - enemyRadius*2;
          //println("distanceX between enemy[", t, "] and enemy[", g, "] is ", distanceX);
          //println("distanceY between enemy[", t, "] and enemy[", g, "] is ", distanceY);



          ///////////////enemy[t].location.x += overlap/2;  //Works
          ///////////////enemy[g].location.x -= overlap/2;
          ///////////////enemy[t].location.y += overlap/2;
          ///////////////enemy[g].location.y -= overlap/2;

          /*enemy[t].location.x += distanceX;  //Works Not
          enemy[g].location.x -= distanceX;
          enemy[t].location.y += distanceY;
          enemy[g].location.y -= distanceY;*/


          //PVector direction = PVector.sub(enemy[t].location, enemy[g].location); //Works Not


          //enemy[t].location.add(direction); //Works Not
          //enemy[g].location.sub(direction);


          //println("enemy[t] location is " + int(enemy[t].location.x) + "," + int(enemy[t].location.y));
          //println("enemy[g] location is " + int(enemy[g].location.x) + "," + int(enemy[g].location.y));

Enemy Class:

class Enemy
{
  PVector location;
  PVector velocity;
  float time = 0;


  Enemy() //Default Contructor
  {
    location = new PVector(random(600 - enemyRadius/2),random(600 - enemyRadius/2));
    velocity = new PVector(random(-4, 4),random(-4, 4)); 
  }


  Enemy(int locX, int locY) //Choose Location
  {
    location = new PVector(locX, locY);
    velocity = new PVector(random(-4, 4),random(-4, 4)); 
  }


  Enemy(int locX, int locY, float velX, float velY) //Choose Both
  {
    location = new PVector(random(-4, 4),random(-4, 4));
    velocity = new PVector(velX, velY); 
  }





  void move() //AKA update
  {
    location.add(velocity); //Add velocity (AKA speed) to location

  }

  void drawEnemy()
  {
    noStroke();
    lights();

    pushMatrix();

      translate(location.x, location.y, 0); //Move the spheres

      fill(0);
      sphere(enemyRadius-6); 

      time += .5;
      fill(255, 0, 0, (127 + sin(time) * 32)); //Pulsating Effect
      sphere(enemyRadius+2);

    popMatrix();

    //Used to be:
    /*
    ellipseMode(RADIUS);
    noStroke();

    time += .5;
    fill(255, 0, 0, (127 + sin(time) * 32));
    ellipse(location.x, location.y, enemyRadius+2, enemyRadius+2);

    fill(0);
    ellipse(location.x, location.y, enemyRadius-6, enemyRadius-6);*/

  }
}

Display Class (if you wanna run it without commenting out the display object stuff):

class Display //Not much here yet
{
  Display()
  {

  }

  void drawBackground()
  {

    //Background
    rectMode(CORNERS);
    fill(127);
    rect(0, 0, width, height);

    //Lines
    stroke(50);
    strokeCap(SQUARE);
    strokeWeight(8);
    line(0, 604, width, 604);
    line(604, 0, 604, 604);

    //World
    noStroke();
    fill(255);
    rect(0, 0, 600, 600);
    //WorldGrid

    //Title
    textAlign(CENTER, CENTER);
    textSize(50);
    text("SANNDELS LITE", (width+600)/2, 30);
    textSize(15);
    text("Simple Artificial Neural Network and Darwinism Evolution Life Simulator", (width+600)/2, 65);
  }

}

Answers

Sign In or Register to comment.