We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Asteroids-like Control System
Page Index Toggle Pages: 1
Asteroids-like Control System (Read 393 times)
Asteroids-like Control System
Dec 10th, 2008, 8:51am
 
this is a follow up to my other topic "URGENT collision detection and movement"

i need to devise a way to control a sphere's rotation with the left and right arrow keys and to use some form of a marker to show where the sphere is "pointing", then i need to use the up arrow to make it move in that direction. it should have some "friction" to slow it down if the up is released and while up is pressed it accelerates. It also needs to bounce off another ball controlled by WASD (two player bumper balls) Ive gotten a lot of help but i still need that control system. Any ideas on how to approach this. I have been experimenting with PVector but i dont know how to keep a marker on each sphere that roates around the ball to show the direction its pointing..
Re: Asteroids-like Control System
Reply #1 - Dec 10th, 2008, 10:57am
 
last thing and i'm going to bed i swear!

this should help:

Code:

float angle = 0;

void setup(){
size(200,200);
stroke(255);
}

void draw() {
background(0);
line(100,100,100+cos(angle)*50,100+sin(angle)*50);
}

void keyPressed(){
if (key == CODED) {
if (keyCode == LEFT){
angle -= .1;
print(angle);
}
if (keyCode == RIGHT) {
angle += .1;
print(angle);
}
}
}
Re: Asteroids-like Control System
Reply #2 - Dec 10th, 2008, 11:20am
 
really kicking myself for doing this instead of sleeping, but sometimes i just get obsessed! lucky for you.

Code:

PVector y_axis;
Ball ball_1, ball_2;

void setup()
{
size(800,800);
smooth();
ball_1 = new Ball(color(255,0,0),50,100, random(-5,5), random(-5,5), 20);
ball_2 = new Ball(color(0,0,255),200,200,random(-5,5), random(-5,5), 15);
y_axis = new PVector(0,1);
}

void draw()
{
background(0);
smooth();
ball_1.update();
ball_2.update();
ball_1.drawBall();
ball_2.drawBall();
}

class Ball
{
private color ball_color;
private PVector position, velocity;
private int radius;
private float angle;

Ball(color ball_color_, float x, float y, float x_v, float y_v, int radius_){
position = new PVector(x,y);
velocity = new PVector(x_v,y_v);
ball_color = ball_color_;
radius = radius_;
}

void update(){
position.add(velocity);
if(position.x < 0)
position.x = width;
if(position.x > width)
position.x = 0;
if (position.y < 0)
position.y = height;
if(position.y > height)
position.y = 0;
velocity.limit(velocity.mag()*.99);//friction
}

void up() {
velocity.x += cos(angle);
velocity.y += sin(angle);
velocity.limit(10);
}

void down() {
velocity.x -= cos(angle);
velocity.y -= sin(angle);
velocity.limit(10);

}

void left() {

angle -= .1;
}

void right() {
angle += .1;
}

/*PVector extend() {

}*/

void drawBall() {
fill(ball_color);
ellipse(position.x, position.y, radius, radius);
stroke(255);
line(position.x, position.y, position.x + cos(angle)*30, position.y + sin(angle)*30);
noStroke();
}
}

void keyPressed() {

switch (key) {
case CODED:
switch (keyCode) {
case UP:
ball_1.up();
break;
case DOWN:
ball_1.down();
break;
case LEFT:
ball_1.left();
break;
case RIGHT:
ball_1.right();
break;
}
break;
case 'w':
ball_2.up();
break;
case 's':
ball_2.down();
break;
case 'a':
ball_2.left();
break;
case 'd':
ball_2.right();
break;
}
}


puts all that stuff together. please don't just submit my code as your homework, but OWN it by understanding it thoroughly and modifying it. enjoy!
Re: Asteroids-like Control System
Reply #3 - Dec 10th, 2008, 1:10pm
 
rebirth wrote on Dec 10th, 2008, 11:20am:
really kicking myself for doing this instead of sleeping, but sometimes i just get obsessed! lucky for you.

Lol! Last night I went to bed thinking how to compute the collision of a ball with an obstacle (bound or other sphere), tinkering with interfaces, abstract classes, vectors and angles...
I suppose we are real geeks... Happy
Re: Asteroids-like Control System
Reply #4 - Dec 11th, 2008, 9:35am
 
you guys have been amazing help, I did not have time to plan out the details but I presented it as a work of another person and I explained the crpa out of it. I learned so much from you guys, and I really appretiate all the work you guys put into it. Rebirth and Philho ROCK!
Re: Asteroids-like Control System
Reply #5 - Dec 11th, 2008, 9:39am
 
I GOT AN A!!! we were suppose dot eithe rpresent code written by others or make our own, I presented and explained everything with out asking any further questions. I spent all of last night commenting every line and I actually taught the proff somehting he did not know of at the time. Again, thanks for all the help guys!!!
Re: Asteroids-like Control System
Reply #6 - Dec 11th, 2008, 11:50am
 
Nice! And good move. I have heard that some stupid students just present works by others as their own, without even changing it.
Your way is much better, and you took time to do an in deepth analysis of good code (rebirth's one, of course), which is a great way to learn. That, and hacking beyond the base, of course.
Congrats.
Re: Asteroids-like Control System
Reply #7 - Dec 11th, 2008, 7:16pm
 
hey! i'm really glad to hear that!  congrats on your A.  and thank you for giving me the opportunity to explore an interesting concept. hope you stick around the processing forums and keep at it!
Page Index Toggle Pages: 1