what about this code defines the scale and position of the arm in the draw window?

im asking because i like to know how to move it like to the left, and make another one for the right "im trying to build a game (i obj arms which i plan to import using obj model)"

right now it seems that it is fixed to be in the middle but i dont see any constraints so im not sure what is telling it to start and rotate from the middle

Screenshot (178)

float mx;
float my;
float easing = 0.05;
int radius = 24;
int edge = 100;
int inner = edge + radius;


 strokeWeight(3);
  stroke(trackColor);  
  float dx = mouseX - x;
  float dy = mouseY - y;
  float angle1 = atan2(dy, dx);  
  float tx = mouseX- cos(angle1) * segLength;
  float ty = mouseY - sin(angle1) * segLength;

  dx = tx - x2;
  dy = ty - y2;

  float angle2 = atan2(dy, dx);  

  x = x2 + cos(angle2) * segLength;
  y = y2 + sin(angle2) * segLength;

  segment(x, y, angle1); 
  segment(x2, y2, angle2);

void segment(float x, float y, float a) {
  pushMatrix();
  translate(x, y);
  rotate(a);
  line(0, 0, segLength, 0);
  popMatrix();
  //update(x, y);
}

Answers

  • Your sample doesn't show us the value of segLength, x, y , x2 or y2. Post the full code.

  • JaiJai
    edited April 2016

    the code dont work no more because i was changing variables around looking to change the size and i try to ctr R and hit ctr S instead, so no full code float segLength = 180; float x, y, x2, y2;

  • Answer ✓

    Okay, weird because you seemed to have removed the setup and draw method, that makes no sense, you always need those.

    To answer your original question, it was x2 and y2 who defined the point of origin. So somewhere in your code they must've been defined. Otherwise it would've started in the top left corner. Where did you get this code?

    Anyway, this code should work. This defines a class for tracking arm, letting you easily define more than one, all with the same correct behavior.

    TrackingArm armLeft, armRight;
    
    void setup() {
      size(800,800);
      smooth();
      armLeft = new TrackingArm(0,height/2);
      armRight = new TrackingArm(width,height/2);
    }
    
    void draw() {
      background(255);
      armLeft.drawArm();
      armRight.drawArm();
    }
    
    
    class TrackingArm {
    
      int radius = 24;
      int edge = 100;
      int inner = edge + radius;
      float segLength = 180; 
    
      float xOrigin, yOrigin, x, y;
    
      TrackingArm(float xOrig, float yOrig) {
        xOrigin = xOrig;
        yOrigin = yOrig;
      }
    
      void drawArm() {
        strokeWeight(5);
        stroke(0);  
        float dx = mouseX - x;
        float dy = mouseY - y;
        float angle1 = atan2(dy, dx);  
        float tx = mouseX- cos(angle1) * segLength;
        float ty = mouseY - sin(angle1) * segLength;
    
        dx = tx - xOrigin;
        dy = ty - yOrigin;
    
        float angle2 = atan2(dy, dx);  
    
        x = xOrigin + cos(angle2) * segLength;
        y = yOrigin + sin(angle2) * segLength;
    
        segment(x, y, angle1); 
        segment(xOrigin, yOrigin, angle2);
      }
    
      void segment(float x, float y, float a) {
        pushMatrix();
        translate(x, y);
        rotate(a);
        line(0, 0, segLength, 0);
        popMatrix();
        //update(x, y);
      }
    }
    

    If you don't understand it I recommend reading a couple of tutorials on objects and classes. It makes everything easier in the long run, but it takes a moment to get used to it. Good luck.

  • JaiJai
    edited April 2016

    @alex_d thanks this what i was looking for though i did not think it through at first this is not really the look i was looking for, i guess this look a bit uncanny lol maybe the what would be the shoulders is a bit it high??? in my original post you can see im using the video cam as a visual point of ref "me simulating a human point of view" kinda like VR goggles? you field of view and then theirs you're arms and legs (this is what im looking to replicate) i do have a 3D obj model of this model i made in autoCAD and want to import into this sketch but not sure how to make those arms stick to the sketch you did? any ideas on that type of work flow? any examples on that?

    WIN_20160429_100106

    also how can i extend the arm or segment? your arms aren't really finger tip to finger tip like the one from my 3D model

    AS FOR YOUR QUESTIONS:

    Okay, weird because you seemed to have removed the setup and draw method, that makes no sense, you always need those.

    yes i did for posting but what i did have was based on me guessing not the original meaning the original was lost during me guessing arbitrary value and saved it rather then save a new copy.

    Where did you get this code?

    i found it in processing examples tab, not sure which lib as this was last summer.

  • edited April 2016

    Well, you can change the positioning of the arms I made by looking at the constructor for the object: TrackingArm(float xOrig, float yOrig) Look at void setup() where it says

    armLeft = new TrackingArm(0,height/2);
    armRight = new TrackingArm(width,height/2);
    

    Right now I used the width and height variables that are built into processing, that way you only have to define your sketchwindow's size in size(w,h)

    I'm not sure about importing a 3D model into Processing. I've only generated and exported 3D stuff with Processing. I think Unity is a lot easier for working with existing 3D models but I don't know if you can easily use webcam feeds there..

    If you do manage to import the obj correctly, I think it might be easiest to import the upper- and lower arm separately and then translating them based on these variables: float xOrigin, yOrigin, x, y; Currently, mouseXand mouseYare used to define the target of the arms. But I'm guessing you might want to track something else than your mouse? So you would need to change that variable by another one in the code and assign the correct value in void draw()

    To get a more realistic effect, I would really try to somehow do this in Unity because it has easy support for 3D models and built in Inverse Kinematics http://docs.unity3d.com/Manual/InverseKinematics.html And it might be easier for you if you're already familiar with 3D software. Just gotta check if they have cam support.

  • JaiJai
    edited April 2016

    @alex_d thanks i just noticed what you pointed out in the code ill see if i can re position, NOW is their a way to have the arm travel in the z direction ? im asking since you made the class "cant wait till i know how to do what you just did" = (make a class from just a portion of what was left of the original code) as in now the arm are traveling on x&y so whats up with the Z?

    as for your rest of the unity comments/statements i will say that i stay away from unity's engines my work flow is very specific "Arduino, Processing ,Visual Studios2013, Fusion360 by AutoDesk then 3D printing" they all provide whats needed of my project.

    YES i do have a actual Humanoid Android i been modeling for awhile now and is fully printable so every model is piece by piece "shells/face plates" so yes i will be importing the shoulder alone and so on for the bicep elbow forearm and hand itself. there is a lib called objloader.*; and i have imported one of my dual motor aircraft but im not sure how the whole moving happens tho i do see the code.

    Untitled

    import saito.objloader.*;
    
    OBJModel model ;
    
    float rotX, rotY;
    
    void setup()
    {
        size(800, 600, P3D);
        frameRate(30);
        model = new OBJModel(this, "xwing.obj", "absolute", TRIANGLES);
        model.enableDebug();
    
        model.scale(10);
        model.translateToCenter();
    
        stroke(255);
        noStroke();
    }
    
    
    
    void draw()
    {
        background(0);
        lights();
        pushMatrix();
        translate(width/2, height/2, 0);
        rotateX(rotY);
        rotateY(rotX);
    
        model.draw();
    
        popMatrix();
    }
    
    boolean bTexture = true;
    boolean bStroke = false;
    
    void keyPressed()
    {
        if(key == 't') {
            if(!bTexture) {
                model.enableTexture();
                bTexture = true;
            } 
            else {
                model.disableTexture();
                bTexture = false;
            }
        }
    
        if(key == 's') {
            if(!bStroke) {
                stroke(255);
                bStroke = true;
            } 
            else {
                noStroke();
                bStroke = false;
            }
        }
    
        else if(key=='1')
            model.shapeMode(POINTS);
        else if(key=='2')
            model.shapeMode(LINES);
        else if(key=='3')
            model.shapeMode(TRIANGLES);
    }
    
    void mouseDragged()
    {
        rotX += (mouseX - pmouseX) * 0.01;
        rotY -= (mouseY - pmouseY) * 0.01;
    }
    

    this is what i hope to have it look like at the end cUntitled

    and yes i been working on algorithms for tracking obj and faces fingers and eyes gaze and i have had some help with pointers from this site just like you are pointing things out to me but one step at a time. as you can see by the ship image i know how to bring in obj i just dont know how to make those import be part of a already working sketch like the one you just made maybe with my new sketch i just uploaded of the Xwing emu maybe you can see how i imported and figure out how would you go about making that ship follow those coordinates or constrains you did to your class

    UPDATE: dam i just try to upload the obj files just incase but theres no options to upload such

  • JaiJai
    edited April 2016

    @alex_d i just noticed that altho you made a class for the arm this arms dont have individuality, so how do we go about giving a arm its own movements independent from the other ? i tried using 2 mouse on the same pc and one of the mouse is being ignored by the arm no type of control it only moves with one mouse dont matter which mouse but it will be the first one to move across the draw window

  • you can simulate it with mouse left

    and cursor keyboard right arm

    TrackingArm armLeft, armRight;
    float x1=0, y1=0; 
    
    void setup() {
      size(800, 800);
      smooth();
      armLeft = new TrackingArm(0, height/2);
      armRight = new TrackingArm(width, height/2);
    
      x1=width/2+100;  
      y1=height/2;
    }
    
    void draw() {
      background(255);
    
      fill(0);  // black
    
      // left arm is mouse 
      armLeft.drawArm(mouseX, mouseY);
    
      // right mouse is keyboard 
      armRight.drawArm( x1, y1 );
    
      // show the keyboard point
      fill(255, 2, 2); // red  
      noStroke(); 
      ellipse(x1, y1, 5, 5);
    
      // help text 
      fill(0); // black 
      text("Use mouse for left arm and cursor keys for right arm", 20, 20);
    }
    
    void keyPressed() {
    
      if (key == CODED) {
    
        switch (keyCode) {
    
        case UP:
          y1-=5;
          break;
    
        case DOWN:
          y1+=5;
          break;
    
        case LEFT:
          x1-=5;
          break;
    
        case RIGHT:
          x1+=5;
          break;
        }//switch
      }//if
    }
    
    // ==============================================
    
    class TrackingArm {
    
      int radius = 24;
      int edge = 100;
      int inner = edge + radius;
      float segLength = 180; 
    
      float xOrigin, yOrigin, x, y;
    
      TrackingArm(float xOrig, float yOrig) {
        xOrigin = xOrig;
        yOrigin = yOrig;
      }
    
      void drawArm ( float mX, float mY ) {
    
        strokeWeight(5);
        stroke(0);  
    
        float dx = mX - x;
        float dy = mY - y;
        float angle1 = atan2(dy, dx);  
        float tx = mX- cos(angle1) * segLength;
        float ty = mY - sin(angle1) * segLength;
    
        dx = tx - xOrigin;
        dy = ty - yOrigin;
    
        float angle2 = atan2(dy, dx);  
    
        x = xOrigin + cos(angle2) * segLength;
        y = yOrigin + sin(angle2) * segLength;
    
        segment(x, y, angle1); 
        segment(xOrigin, yOrigin, angle2);
      }
    
      void segment(float x, float y, float a) {
        pushMatrix();
        translate(x, y);
        rotate(a);
        line(0, 0, segLength, 0);
        popMatrix();
        //update(x, y);
      }
    }
    
  • @Chrisir the whole entire point is to liberate you from the entire computer space and its Human Interface Devices

  • I know

  • lol then if i have to use the mouse and the keyboard im not really liberated, thats constrained

  • Yes

    Do you want to use kinect?

  • The point is that Chrisir showed you how to change the code and make them move independently based on method argumends, in this case drawArm(float mx, float my) . That's why he said 'you can simulate it'.

    Right now, the code is simply using mouse and keyboard as a placeholder. If you want it to follow your hands on camera you need to work with Kinect or some kind of blob detection and feed that x & y data into the method's arguments.

    If you want to be able to write code like this, I suggest you work on your basics first. I also tried learning by doing, but a lot of these things will only make sense when you understand the fundamentals of Java and Object Oriented Programming (OOP). I recommend you do this course: https://www.udacity.com/course/intro-to-java-programming--cs046 or something similar. It's a bit boring at times but I guarantee you will understand Java & Processing a LOT better afterwards.

    I personally don't know how to translate this code to 3D space, that's why I was suggesting Unity since it does inverse kinematics for you. (ie. you give the target to the code and it will move the arm correctly to that point). So look into "inverse kinematics algorithms/theory" if you want to do it yourself in processing. Will be math heavy though. There's no reason why it isn't doable though. And if you have a kinect you can even get the x y z values from your hands etc.

  • Thanks for helping

    If you want to use kinect you could re-post my code in the kinect section of this forum and ask for guidance there

  • JaiJai
    edited May 2016

    guys im using a regular webcam thats it, and what im looking todo can be done even without a kinect "you just have to make up for what is being lacked by doing more coding" as for the comment made about IK/FK i know about this techniques and i will tell you that i have NO issues doing everything IK does without using the heavy math involved using C++ in Arduino for regular DC motors and Servos as well.

    @Chrisir i see what you did so since your just doing placeholders like Alex said then how would i go about using a second mouse? as in how do input that into the sketch ? i dont think is done the same way we are calling THIS camera while making a object out of a class Video ?

    with this algorithm im able to use all of this mouses with no issues and i can even use my eye to move one of these mouse while others with my hand and others with my face similar to how Alex described the whole this about placeholder.

    mouse.Swallow = true
    fakemouse.DirectInputX = mouse1.DirectInputX
    fakemouse.DirectInputY = mouse1.DirectInputY
    mouse.LeftButton = mouse1.LeftButton
    mouse.RightButton = mouse1.RightButton
    mouse.MiddleButton = mouse1.MiddleButton
    mouse.XButton1 = mouse1.XButton1
    mouse.XButton2 = mouse1.XButton2
    
    if mouse2.present but not var.shown2 then
      cursor2.visible = true
      var.Mouse2x = mouse.CursorPosX
      var.Mouse2y = mouse.CursorPosY
      var.shown2 = true
    endif
    
    if mouse3.present but not var.shown3 then
      cursor3.visible = true
      var.Mouse3x = mouse.CursorPosX
      var.Mouse3y = mouse.CursorPosY
      var.shown3 = true
    endif
    
    if mouse4.present but not var.shown4 then
      cursor4.visible = true
      var.Mouse4x = mouse.CursorPosX
      var.Mouse4y = mouse.CursorPosY
      var.shown4 = true
    endif
    
    if mouse2.present then
      var.deltax2 = Delta(mouse2.DirectInputX)
      var.deltay2 = Delta(mouse2.DirectInputY)
    
      var.velocity = ([(smooth(var.deltaX2) / 24ms) in inches per second, (smooth(var.deltaY2) / 24ms) in inches per second] )
      var.speed = |var.velocity|
      if var.speed <= 0.2 then
        var.deltax2 = var.deltax2 * 2
        var.deltay2 = var.deltay2 * 2
      endif
      if var.speed > 0.2 and var.speed <= 0.4 then
        var.deltax2 = var.deltax2 * 4
        var.deltay2 = var.deltay2 * 4
      endif
      if var.speed > 0.4 and var.speed <= 1.25 then
        var.deltax2 = var.deltax2 * 7
        var.deltay2 = var.deltay2 * 7
      endif
      if var.speed > 1.25 and var.speed <= 3.9 then
        var.deltax2 = var.deltax2 *12 //* 7
        var.deltay2 = var.deltay2 *12 //* 7
      endif
      if var.speed > 3.9 then
        var.deltax2 = var.deltax2 * 20// * 13.77
        var.deltay2 = var.deltay2 * 20// * 13.77
      endif
      var.Mouse2x = var.Mouse2x pixels + (var.deltax2 in pixels)
      var.Mouse2y = var.Mouse2y pixels + (var.deltay2 in pixels)
      if var.Mouse2x < screen.DesktopLeft then var.Mouse2x = screen.DesktopLeft
      if var.Mouse2x > screen.DesktopRight then var.Mouse2x = screen.DesktopRight
      if var.Mouse2y < screen.DesktopTop then var.Mouse2y = screen.DesktopTop
      if var.Mouse2y > screen.DesktopBottom then var.Mouse2y = screen.DesktopBottom
    endif
    Cursor2.PosX = var.Mouse2x
    Cursor2.PosY = var.Mouse2y
    Cursor2.LeftButton = mouse2.LeftButton
    Cursor2.RightButton = mouse2.RightButton
    Cursor2.MiddleButton = mouse2.MiddleButton
    
    if mouse3.present then
      var.deltax3 = Delta(mouse3.DirectInputX)
      var.deltay3 = Delta(mouse3.DirectInputY)
    
      var.velocity = ([(smooth(var.deltaX3) / 24ms) in inches per second, (smooth(var.deltaY3) / 24ms) in inches per second] )
      var.speed = |var.velocity|
      if var.speed <= 0.2 then
        var.deltax3 = var.deltax3 * 2
        var.deltay3 = var.deltay3 * 2
      endif
      if var.speed > 0.2 and var.speed <= 0.4 then
        var.deltax3 = var.deltax3 * 4
        var.deltay3 = var.deltay3 * 4
      endif
      if var.speed > 0.4 and var.speed <= 1.25 then
        var.deltax3 = var.deltax3 * 7
        var.deltay3 = var.deltay3 * 7
      endif
      if var.speed > 1.25 and var.speed <= 3.9 then
        var.deltax3 = var.deltax3 *12 //* 7
        var.deltay3 = var.deltay3 *12 //* 7
      endif
      if var.speed > 3.9 then
        var.deltax3 = var.deltax3 * 20// * 13.77
        var.deltay3 = var.deltay3 * 20// * 13.77
      endif
      var.Mouse3x = var.Mouse3x pixels + (var.deltax3 in pixels)
      var.Mouse3y = var.Mouse3y pixels + (var.deltay3 in pixels)
      if var.Mouse3x < screen.DesktopLeft then var.Mouse3x = screen.DesktopLeft
      if var.Mouse3x > screen.DesktopRight then var.Mouse3x = screen.DesktopRight
      if var.Mouse3y < screen.DesktopTop then var.Mouse3y = screen.DesktopTop
      if var.Mouse3y > screen.DesktopBottom then var.Mouse3y = screen.DesktopBottom
    endif
    Cursor3.PosX = var.Mouse3x
    Cursor3.PosY = var.Mouse3y
    Cursor3.LeftButton = mouse3.LeftButton
    Cursor3.RightButton = mouse3.RightButton
    Cursor3.MiddleButton = mouse3.MiddleButton
    
    if mouse4.present then
      var.deltax4 = Delta(mouse4.DirectInputX)
      var.deltay4 = Delta(mouse4.DirectInputY)
    
      var.velocity = ([(smooth(var.deltaX4) / 24ms) in inches per second, (smooth(var.deltaY4) / 24ms) in inches per second] )
      var.speed = |var.velocity|
      if var.speed <= 0.2 then
        var.deltax4 = var.deltax4 * 2
        var.deltay4 = var.deltay4 * 2
      endif
      if var.speed > 0.2 and var.speed <= 0.4 then
        var.deltax4 = var.deltax4 * 4
        var.deltay4 = var.deltay4 * 4
      endif
      if var.speed > 0.4 and var.speed <= 1.25 then
        var.deltax4 = var.deltax4 * 7
        var.deltay4 = var.deltay4 * 7
      endif
      if var.speed > 1.25 and var.speed <= 3.9 then
        var.deltax4 = var.deltax4 *12 //* 7
        var.deltay4 = var.deltay4 *12 //* 7
      endif
      if var.speed > 3.9 then
        var.deltax4 = var.deltax4 * 20// * 13.77
        var.deltay4 = var.deltay4 * 20// * 13.77
      endif
      var.Mouse4x = var.Mouse4x pixels + (var.deltax4 in pixels)
      var.Mouse4y = var.Mouse4y pixels + (var.deltay4 in pixels)
      if var.Mouse4x < screen.DesktopLeft then var.Mouse4x = screen.DesktopLeft
      if var.Mouse4x > screen.DesktopRight then var.Mouse4x = screen.DesktopRight
      if var.Mouse4y < screen.DesktopTop then var.Mouse4y = screen.DesktopTop
      if var.Mouse4y > screen.DesktopBottom then var.Mouse4y = screen.DesktopBottom
    endif
    Cursor4.PosX = var.Mouse4x
    Cursor4.PosY = var.Mouse4y
    Cursor4.LeftButton = mouse4.LeftButton
    Cursor4.RightButton = mouse4.RightButton
    Cursor4.MiddleButton = mouse4.MiddleButton
    

    i can tell you this code cant be adopted work in Processing unless we build classes and obj for all of this and even so i feel this mystery about Java and their lib, is like Processing hides things from the user where as in Arduino is easy to access the lib H files Cpp files and so on, enough of how i feel about Java in Processing.

    as for this sketch we been talking about width and height should serve WELL with depth while X&Y will suffice for moving/panning on 2D space while W&H will be good for +/- in 3D space "i already did this with Processing and Arduino with Servos and DC motors on another application, SO that being said what im looking to do here is no diff at all, this is just a simulator if anything.

    WIN_20160430_122415

    as you can see i already did the whole obj uploading using the obj lib and mapped the obj arms "4 obj model per arm" im working on the XML for hand recognition so that i can map my hands to the simulators obj's since i dont find anything in openCV for processing where someone already did the hand harrcascade files or at least a finger xml, i just need to work out the height and width of this sketch so that i can get my translation in the Z axis for the3D space

  • I can't help you with that

  • JaiJai
    edited May 2016

    draft is not clearing after you post

  • @Chrisir how do i add a 3rd limb ? i would like to know what to consider when rotating or translating on Z while being able to rotate on x&y "you know' like our hands"

  • Answer ✓

    ??

    do you mean z as in 3D

  • @Chrisir yes sir...

  • Ask alex_d

    Like 2 Segments on each side but in 3D?

  • yes, i mean they are arms so they should be able to translate in Z "closer to screen or farther away from screen" i have a code somewhere from this company named DoBOT ? i think is called and is a robotic arm like the kuka and they used processing to make the arm move about in xy and z so it is possible

    @alex_d have you have any clue ?

  • How do you intend to capture the depth data from your hands on a regular webcam? This is not easy, just capturing the x y position of your hands alone is gonna be quite hard. That's why I'm suggesting Kinect, so you have an easy way of getting the z data.

    And sorry, I don't know how to turn this code into 3D or how to add a limb, not my expertise.

  • And if you want to use a separate mouse, this might work: http://www.lagers.org.uk/gamecontrol/index.html

  • thanks Alex, maybe i can use the cross hair of the xbox control altho i will not be physically touching any remotes or mouse, BUT you asked how will i be getting Z? well doing what you said you call hard hard is not a reason to not do something now you know and the fact that you even knew about the x&y suggest you know more then what your expertise is saying lol

  • edited May 2016

    Because getting 2D info from a normal camera can be done with blob detection -> http://www.v3ga.net/processing/BlobDetection/ (but it needs high contrast, etc) so you can use that.

    Getting depth data from a normal camera however, is not easily done, even impossible I'd say. There is no way for the camera to know which pixel is further than the other. That's why they invented 3D cameras.

    When I say not my expertise, I mean my math isn't good enough for that :p

  • Jay, buy a kinect

  • well my math is good so if thats what it takes then im going to work out the algorithms for depth thanks tho

  • No I meant you need math to add a 3d limb..

    For calculating depth from a normal camera maybe you could use some kind of photogrammetry technique

    But I really don't know anything about how that works, so you're on your own there. Good luck man, prove us wrong :)

  • there is a lib for image recognition called opencv

  • lol funny Chris , but hey thanks for that link Alex.

  • funny? Why is it funny?

  • because i know about opencv and i dont see how is opencv is going to do anything easier? or work for that matter we are adding obj files here trying to translate in 3d space and track hands .

  • doesn't opencv allow recognition of hands maybe?

  • JaiJai
    edited May 2016

    @alex_d

    Lots of people are requesting this as an asset or Github project. I'm not gonna commit to ever releasing it,

    clearly there was no point in sharing this link, tho i will say i do have a leapMotion but it will not work for me because this application is not for anyone asses to be seating next to the pc or near application and to use the leap you have to be close to a range.

    i mean com'on his clearly just showing off it even says it Show-Off

    @Chrisir yes it does But no one has share or made any xml file for such hand.

  • can't believe no one has said "gee why don't we make a hand xml" ridiculous serious now?!

    so far this is what i found so far.

    Capture

    i mean are you shitting me someone actually took the time to make a clock cascade ??? really lol

  • Are those 3D Models?

  • JaiJai
    edited May 2016

    lol how should i know chris? im the one in question and as for the files, well those ext arent 3D models last time i checked, besides why would opencv have anything to do with 3D modeling obj's?

    wait are you ref to the red arms in the video feed? if so yes those are obj files

Sign In or Register to comment.