Hi all!
Im here again looking for help from you super genius
Im building a mini (Ztupid) game so I can learn some physics object with PVector and Collision in 2D dimension.
The game is pretty simple, There is a Platform/Bar and a Ball upon it, the bar start with a random inclination (+1 pitch or -1 pitch) and the ball start rolling on the inclinated side. Using A & D you can rotate The Platform in order to keep the ball balanced upon the Platform or at least you try to dont drop it.
I cant figure out how build the collision and simulate the ball drop based on G-force and his mass.
If somebody can help me maybe just with the collission or the drop, would be super Awesome!
Here a screenshot of the basic concept:
Here the code:
Acrobat (Main class):
import java.text.DecimalFormat;
DecimalFormat df = new DecimalFormat("#.##");
int FPS = 20;//Initial frame rate
int howMuchFPS = 10;//FPS to increase every + key pressed
int attempts = 1;
//physics constants
final float GRAVITY = 0.9801;
//GLOBAL VARs
public static float pitchLeft = 0;
public static float pitchRight = 0;
Ball ball = new Ball();
Platform plat = new Platform();
float angle = 0.05;
void setup()
{
//on startup
size(1000, 700);
frameRate(FPS);
rectMode(CENTER);
}
void draw()
{
background(40);
fill(255);
stroke(255);
// line(0, 250, 250, 250);
line(0, 500, 1000, 500); //Line(x1, y1, x2, y2)
textSize(25);
fill(0);
text("FPS: " + FPS, 480, 600);
textSize(35);
text("PitchL: " + df.format(pitchLeft), 100, 100);
text("PitchR: " + df.format(pitchRight), 700, 100);
// ball.move();
ball.show();
plat.move();
plat.show();
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
void keyPressed()
{
switch(key)
{
case 'a'://Go left //<>//
plat.spin = -angle;
break;
case 'd'://Go right //<>//
plat.spin = angle;
break;
case 'r':
plat.rotation = 0;
pitchLeft = 0;
pitchRight = 0;
break;
//FRAMERATE SETUP
case '+'://speed up frame rate
FPS += howMuchFPS;
frameRate(FPS);
break;
case '-'://slow down frame rate
if (FPS > 10) //Check framerate > 10
{
FPS -= howMuchFPS;
frameRate(FPS);
}
break;
case 'p':
FPS += 50;
frameRate(FPS);
break;
case 'm':
FPS -= 50;
frameRate(FPS);
break;
}
}
void keyReleased()
{
switch(key)
{
case 'a'://Go left //<>//
plat.spin = 0;
break;
case 'd'://Go right //<>//
plat.spin = 0;
break;
}
}
Platform Class
class Platform
{
PVector position;
PVector velocity;
float spin; // Value of Spin
float rotation; // The Bar current rotation
int x, y;
//Contsructors
Platform()
{
position = new PVector();
velocity = new PVector();
x = 0;
y = 0;
spin = 0;
rotation = 0;
//Get initial random inclination
float rand = random(-1, 1);
if(rand > 0)
rotation += 0.05;
else
rotation -= 0.05;
}
//GET & SET-----------------------------------------------------------------------
PVector getPosition()
{return position;}
void setPosition(PVector Position)
{position = Position;}
PVector getVelocity()
{return velocity;}
void setVelocity(PVector Velocity)
{velocity = Velocity;}
int getX()
{return x;}
void setX(int X)
{x = X;}
int getY()
{return y;}
void setY(int Y)
{x = Y;}
float getSpin()
{return spin;}
void setSpin(float Spin)
{spin = Spin;}
float getRotation()
{return rotation;}
void setRotation(float Rotation)
{rotation = Rotation;}
//METHODS-----------------------------------------------------------------------------
//Spin platform
public void rotatePlatform()
{
rotation += spin;
//SET TEXT INFOs
/* if(spin > 0)
{
pitchLeft = rotation;
pitchRight = -rotation;
}
else if(spin < 0)
{
pitchRight = rotation;
pitchLeft = -rotation;
} */
}
public void move()
{
rotatePlatform();
}
public void show()
{
translate(width/2, height/2 + 150);
rotate(rotation);
rect(0, 0, 500, 20, 5);
}
}
Ball Class
class Ball
{
PVector position;
PVector velocity;
PVector acceleration;
float mass;
int x, y;
Ball()
{
x = 0;
y = 0;
mass = 0.0;
position = new PVector();
velocity = new PVector();
acceleration = new PVector();
}
//GET & SET--------------------------------------------------------------------
PVector getPosition()
{return position;}
void setPosition(PVector Position)
{position = Position;}
PVector getVelocity()
{return velocity;}
void setVelocity(PVector Velocity)
{velocity = Velocity;}
PVector getAcceleration()
{return acceleration;}
void setAcceleration(PVector Acceleration)
{acceleration = Acceleration;}
int getX()
{return x;}
void setX(int X)
{x = X;}
int getY()
{return y;}
void setY(int Y)
{x = Y;}
float getMass()
{return mass;}
void setMass(float Mass)
{mass = Mass;}
//METHODS----------------------------------------------------------------------
void move()
{
// TEST DROPPING
acceleration = new PVector(0, 0.98); //G-force
velocity.add(acceleration);
position.add(velocity);
//acc.setMag(0.1);
}
void show()
{
//TODO
//Follow G-force based on Platform inclination
//ellipse(X, Y, width, height) Y = Half height + half this.radius - half bar radius
ellipse(500, 450 + 25 - 12, 50, 50);
}
}