Making ellipses apear from outside the screen and go to the center.

edited February 2017 in Library Questions

Hi,

I'm making an astroid shooting game. I have a fixed OSC driven turret in the center of the screen and I want astroids to randomly spawn out side the screen and move to the center. I'm stuck with this problem for two days and tried a lot to figure it out, but i can't come to a working concept. Do you have any suggestions to make this a reality. It would be awesome if this could be coupled with the float deg, because I send this parameter to an other part of the system to forward the rotation.

This is my code so far with a not properly working astroid method:

import oscP5.*;
import netP5.*;

OscP5 oscP5;
NetAddress pilot, gunner;

PImage img;
int shoot;
float deg;
float astsize = random(30,70);
float astspeed;
float astposx = random(0,500);
float astposy = random (0,500);

void setup() {
  size (1000, 1000);

  oscP5 = new OscP5(this,12002);
  pilot = new NetAddress("localhost",12001);
  gunner = new NetAddress("localhost",12000);

  deg = -90;
  img = loadImage("turret upperview 2.png");
} //setup()

void draw(){
  background(20);
  stroke(255);
  //line(0, height/2, width, height/2);
  //line(width/2, 0, width/2, height);

  //turret visuals
  pushMatrix();
  translate (width/2, height/2);
  rotate(radians((float)deg));
  if (shoot == 1) {
  stroke (255,0,0);
  } else {
   stroke(20); 
  }
  strokeWeight(2);
  line (0, 5, height, 5);
  line (0, -7, height, -7);
  image (img, -21.125, -17.5, img.width/2, img.height/2);
  popMatrix();

  astroid();
} //draw()

void astroid() {
 astspeed = 0.5;

 astposx = astposx + astspeed;
 astposy = astposy + astspeed;

  pushMatrix();
    translate(width/2, height/2);
    ellipse( astposx, astposy, astsize,astsize);
  popMatrix();
} //astroid()

void oscEvent(OscMessage theOscMessage){

  //Receiving message
  String msgType =  theOscMessage.addrPattern();

  if(msgType.equals("/shoot")){
    int shootValue = theOscMessage.get(0).intValue(); 
    shoot = shootValue; 
  }
  if(msgType.equals("/deg")){
    float degValue = theOscMessage.get(0).floatValue();   
    deg = degValue; 
  }

  //Forwarding
  OscMessage degForward = new OscMessage("/deg");
  degForward.add(deg);
    oscP5.send(degForward, gunner); 

  OscMessage shootForward = new OscMessage("/shoot");
  shootForward.add(shoot);
    oscP5.send(shootForward, pilot);  
} // oscEvent()

ps. The turning and shooting of the turret is controlled in other patches (NetAddress pilot and gunner)

Answers

  • Answer ✓

    I am not totally sure about how you interpret the value of your angle. Here there is a demonstration. An asteroid is generated outside the sketch and it moves toward the center based on angle. Check the status alive to see if the asteroid has left the sketch. If it does, you could remove it or re-init its position.

    Kf

    float deg=-90;
    final int MAXVEL=5;
    
    asteroid ast0;
    
    void setup() {
      size(600, 600);
      ast0=new asteroid(radians(deg));
      fill(255);
      //frameRate(15);
    }
    
    
    void draw() {
      background(0);
      translate(width/2,height/2);
    
      ast0.draw();
      if(ast0.alive==false)
      ast0.init(random(360));
    }
    
    //Positio of this class assumes center of the sketch is (0,0)
    class asteroid {
    
      float x, y, ar;
      float vx, vy;
      boolean alive;
    
      asteroid(float ang) {
        init(ang);
      }
    
      void init(float ang){
        float vel=random(1,MAXVEL);
        x=int(cos(ang)*width/2);
        y=int(sin(ang)*height/2);
        vy=-vel*sin(ang);    
        vx=-vel*cos(ang); 
        ar=int(random(5, 75));
        alive=true;
        //println(toString());    
      }
    
      @ Override
      String toString(){
        String s="x="+x;
        s+="\ny "+y;
        s+="\nvx "+vx;
        s+="\nvy "+vy;
        s+="\n";
        return s;
      }
    
    
      void draw() {
        x+=vx;
        y+=vy;
        ellipse(x, y, ar, ar);
        println(x+" "+y);
    
        if (abs(x)>(width/2+ar) || abs(y)>(height/2+ar))
          alive=false;
      }
    }
    
  • kfrajer, you are the best!

    I understand this sketch, except for the part that begins with @ Override. What does that do? And I use the float deg for the rotation and I send that number to my other patches (a topdown view and a first person view). They know the turret rotation via deg, so I would like to have a deg-like value for the incomming astroid so I can display it in the first person view. Like an astroid is incomming at 230 degrees (with 0 degrees being right up for topdown view).

    Again thank you very much for helping me. :)

  • Fo override, check https://docs.oracle.com/javase/tutorial/java/IandI/override.html

    For the value deg, I suggest you print it on the screen and see how it relates to the asteroid trajectory in the sketch. I believe the angle definition, assuming the reference point is at the center of the sketch, is like this: 0 degrees at 3 o'clock. Then +90/-90 degrees at 6/12 o'clock. 180 degrees at 9 o'clock.

    Kf

  • Defining the asteroid movement in terms of the way the player is pointing doesn't make a lot of sense, not least because the player can move / rotate.

    But it's easy enough to work out the angle between the two objects, and therefore if the gun is pointing at the asteroid, using their x, y and atan2(), see reference.

    The correct spelling of asteroid is 'asteroid'. 8)

Sign In or Register to comment.