Hey there again, sorry to keep plaguing the forum with my questions but I had another questions. I have another project where I want to combine two other pieces of code into one. I have a little monster that follows the cursor around the screen and I want to be able to grab him, pull him around then when you release the mouse he springs back and forth. Would anyone know how to go about this. PLEASE let me know if you do. I am desperate. Thanks!
Here's my monster that follows the cursor:
Code:float OTTO_X = 900;
float OTTO_Y = 300;
int BLINK_COUNTER = 7;
float GNASH_COUNTER = 0;
int COUNTER = 0;
/* Otto's state of mind 0 = happy, 1 = angry, 2 = bitey */
int OTTO_MENTAL_STATE = 0;
void setup()
{
size(400,400);
smooth();
strokeWeight(2);
}
void draw()
{
background(255);
drawBorder();
float[] ottoMove = computeOttoPos(OTTO_X,OTTO_Y,mouseX,mouseY,OTTO_MENTAL_STATE);
OTTO_X = ottoMove[0] + (1-(2*noise(COUNTER*0.1)))*(0.0*OTTO_MENTAL_STATE);
OTTO_Y = ottoMove[1] + (1-(2*noise(COUNTER)))*(0*OTTO_MENTAL_STATE);
OTTO_MENTAL_STATE = computeMentalState(OTTO_X,OTTO_Y);
drawOttoBody(OTTO_X,OTTO_Y);
drawOttoEyes(OTTO_X,OTTO_Y,OTTO_MENTAL_STATE,BLINK_COUNTER);
if (OTTO_MENTAL_STATE == 2){
drawOttoMouth(OTTO_X,OTTO_Y,GNASH_COUNTER);
}
if (BLINK_COUNTER < 0)
{
BLINK_COUNTER = int(random(50))+100;
}
if(GNASH_COUNTER > 90){
GNASH_COUNTER = 0;
}
BLINK_COUNTER -= 1;
GNASH_COUNTER += 20;
COUNTER += 1;
}
float[] computeOttoPos(float inXVal, float inYVal, float inMouseX, float inMouseY, int mentalState)
{
float moveOTTO[] = {inXVal, inYVal};
float diffX = inMouseX - inXVal;
float diffY = inMouseY - inYVal;
float mag = sqrt((diffX*diffX)+(diffY*diffY));
if(mag != 0)
{
diffX = diffX/mag;
diffY = diffY/mag;
if(mentalState >= 1)
{
diffX *= (mentalState+0);
diffY *= (mentalState+0);
}
moveOTTO[0] = diffX + inXVal;
moveOTTO[1] = diffY + inYVal;
}
return moveOTTO;
}
void drawOttoBody (float OTTO_X, float OTTO_Y)
{
/* Draw Otto's angry little body */
fill(0);
ellipse(OTTO_X,(OTTO_Y-20),98,70);
ellipse(OTTO_X,(OTTO_Y-60),5,30);
}
void drawOttoEyes (float OTTO_X, float OTTO_Y, int OTTO_MENTAL_STATE, int BLINK_COUNTER)
{
/* Draw Otto's eyes, frowning if neccessary, with timed blinks */
if(BLINK_COUNTER > 10){
fill(255);
float LeyeX = OTTO_X-10;
float eyeY = OTTO_Y-50;
float ReyeX = OTTO_X+10;
ellipse(LeyeX,eyeY,15,15);
ellipse(ReyeX,eyeY,15,15);
float LPupilOffsetX = mouseX - LeyeX;
float RPupilOffsetX = mouseX - ReyeX;
float RPupilOffsetY = mouseY - eyeY;
float LPupilOffsetY = mouseY - eyeY;
float magLeft = sqrt((LPupilOffsetX*LPupilOffsetX)+(LPupilOffsetY*LPupilOffsetY));
float magRight = sqrt((RPupilOffsetX*RPupilOffsetX)+(RPupilOffsetY*RPupilOffsetY));
LPupilOffsetX /= magLeft;
LPupilOffsetY /= magLeft;
RPupilOffsetX /= magRight;
RPupilOffsetY /= magRight;
LPupilOffsetX *= 5;
LPupilOffsetY *= 5;
RPupilOffsetX *= 5;
RPupilOffsetY *= 5;
if(OTTO_MENTAL_STATE > 0){
if(LPupilOffsetY < -0){
LPupilOffsetY = -0;
}
if(RPupilOffsetY < -0){
RPupilOffsetY = -0;
}
}
fill(0);
ellipse((LeyeX+LPupilOffsetX),(eyeY+LPupilOffsetY),4,4);
ellipse((ReyeX+RPupilOffsetX),(eyeY+RPupilOffsetY),4,4);
if(OTTO_MENTAL_STATE > 0){
fill(0);
quad((LeyeX-0),(eyeY-0),(LeyeX+0),(eyeY-0),(LeyeX+0),(eyeY-0),(LeyeX-0),(eyeY-0));
quad((ReyeX+0),(eyeY-0),(ReyeX-0),(eyeY-0),(ReyeX-0),(eyeY-0),(ReyeX+0),(eyeY-0));
}
}
}
void drawOttoMouth (float OTTO_X, float OTTO_Y, float GNASH_COUNTER)
{
/* Draw Otto's bitey cake-hole */
float gnashAmount = radians(GNASH_COUNTER);
float topRowY = (OTTO_Y-0) + cos(gnashAmount)*0 + 0;
float bottomRowY = (OTTO_Y-0) - cos(gnashAmount)*0 - 0;
fill(255);
//rect((OTTO_X-30),topRowY,0,(bottomRowY-topRowY));
}
void drawBorder()
{
/* Draw black border around screen */
}
int computeMentalState(float OTTO_X, float OTTO_Y)
{
int mentalStateOut = 3;
float diffX = mouseX - OTTO_X;
float diffY = mouseY - OTTO_Y;
float magnitude = sqrt((diffX*diffX)+(diffY*diffY));
if(magnitude < 150 ){
mentalStateOut = 1;
}
if(magnitude < 50){
mentalStateOut = 3;
}
return mentalStateOut;
}
And here is the "spring" action I want to incorporate:
Code:int num = 3;
Spring[] springs = new Spring[num];
void setup()
{
size(200, 200);
noStroke();
smooth();
springs[0] = new Spring( 70, 160, 20, 0.98, 8.0, 0.1, springs, 0);
springs[1] = new Spring(150, 110, 60, 0.95, 9.0, 0.1, springs, 1);
springs[2] = new Spring( 40, 70, 120, 0.90, 9.9, 0.1, springs, 2);
}
void draw()
{
background(51);
for (int i = 0; i < num; i++) {
springs[i].update();
springs[i].display();
}
}
void mousePressed()
{
for (int i = 0; i < num; i++) {
springs[i].pressed();
}
}
void mouseReleased()
{
for (int i=0; i<num; i++) {
springs[i].released();
}
}
class Spring
{
// Screen values
float xpos, ypos;
float tempxpos, tempypos;
int size = 20;
boolean over = false;
boolean move = false;
// Spring simulation constants
float mass; // Mass
float k = 0.2; // Spring constant
float damp; // Damping
float rest_posx; // Rest position X
float rest_posy; // Rest position Y
// Spring simulation variables
//float pos = 20.0; // Position
float velx = 0.0; // X Velocity
float vely = 0.0; // Y Velocity
float accel = 0; // Acceleration
float force = 0; // Force
Spring[] friends;
int me;
// Constructor
Spring(float x, float y, int s, float d, float m,
float k_in, Spring[] others, int id)
{
[Moderator's note: Renamed from PLEEEEEASE HELP ME to something more explicit/useful/lower case... Added code tags. Code truncated by forum limit!