Loading...
Processing Forum
Recent Topics
All Forums
Screen name:
mt.carner
mt.carner's Profile
2
Posts
0
Responses
0
Followers
Activity Trend
Last 30 days
Last 30 days
Date Interval
From Date :
To Date :
Go
Loading Chart...
Posts
Responses
PM
Show:
All
Discussions
Questions
Expanded view
List view
Private Message
SImple Touch Path Sketch
[1 Reply]
21-Jan-2013 08:59 PM
Forum:
Android Processing
I've made a simple sketch that draws a line for an object to follow at an specified speed. I'm curious to see how others would implement this or if there is a commonly used solution for this already.
import android.view.MotionEvent;
PFont junction;
int currentTime;
int lastTime;
Mover mover;
boolean touched;
void setup(){
size( displayWidth, displayHeight );
orientation( PORTRAIT );
strokeWeight(1.5);
mover = new Mover();
junction = createFont("Junction.otf", 32);
textFont( junction , 32);
lastTime = millis();
touched = false;
}
void draw(){
background(255);
mover.display();
mover.update();
fill(0);
text( "Point size: " + mover.motionLine.points.size(), 10, 50);
text( "Frame Rate: " + frameRate, 10, 100);
}
public boolean surfaceTouchEvent( MotionEvent event ){
int action = event.getAction();
switch( action ){
case MotionEvent.ACTION_DOWN: {
currentTime = millis();
if(mover.motionLine.isEnd == true){
mover.isMoving = false;
mover.motionLine.clearLine();
mover.motionLine.isEnd = false;
}
if(dist( event.getX(), event.getY(), mover.position.x, mover.position.y ) < 25){
touched = true;
mover.motionLine.add( mover.position.x, mover.position.y );
}
lastTime = currentTime;
break;
}
case MotionEvent.ACTION_MOVE: {
if(touched){
currentTime = millis();
if((currentTime - lastTime) > 25){
mover.motionLine.add( event.getX(), event.getY() );
lastTime = currentTime;
}
}
break;
}
case MotionEvent.ACTION_UP: {
if(touched){
mover.motionLine.isEnd = true;
mover.startMoving();
touched = false;
}
break;
}
} // end switch
return super.surfaceTouchEvent( event );
} // end surfaceTouchEvent
class MotionLine {
ArrayList<PVector> points;
int max; /* max number of points before reset */
boolean isEnd;
MotionLine(){
isEnd = false;
points = new ArrayList<PVector>();
max = 50;
}
void display(){
for(int i = 1; i < points.size() - 1; i++){
PVector last = points.get( i-1 );
PVector current = points.get( i );
line(last.x, last.y, current.x, current.y);
}
} // end display
void add( float x, float y ){
if( points.size() == max ){
isEnd = true;
}else{
points.add( new PVector(x, y) );
}
} // end add
void clearLine(){
points.clear();
}
void resetLine(){
clearLine();
isEnd = false;
}
} // end MotionLine
class Mover {
PVector position;
PVector velocity;
PVector target;
float speed;
MotionLine motionLine;
boolean isMoving;
int lineSize;
int pointIndex;
Mover(){
position = new PVector(100, 100);
velocity = new PVector(0, 0);
target = new PVector(0, 0);
speed = 2.5;
motionLine = new MotionLine();
isMoving = false;
pointIndex = 1;
}
void display(){
ellipse(position.x, position.y, 50, 50);
motionLine.display();
}
void update(){
if( isMoving ){
position.add( velocity );
if(dist( position.x, position.y, target.x, target.y ) <= speed){
if( pointIndex == lineSize ){
isMoving = false;
motionLine.clearLine();
velocity.set(0, 0, 0);
pointIndex = 1;
}else{
pointIndex++;
getNextTarget( motionLine.points.get( pointIndex ) );
}
}
}
} // end update
void calcVelocity(){
PVector displacement = PVector.sub( target, position );
displacement.normalize();
displacement.mult( speed );
velocity = displacement;
}
void getNextTarget( PVector t ){
target.set(t);
calcVelocity();
}
void startMoving(){
lineSize = motionLine.points.size() - 1;
if(lineSize > 1){
getNextTarget( motionLine.points.get( pointIndex ));
isMoving = true;
}
}
} // end Mover
Multi key input for videogame?
[1 Reply]
09-Jul-2011 12:46 AM
Forum:
Programming Questions
Right now I'm using,
if(keyPressed) {
if(key == 's' || key == 'S') {
// Move
}
}
and so with the WASD keys i can only get movement in one direction. Any help would be appreciated.
«Prev
Next »
Moderate user : mt.carner
Forum