This site requires JavaScript, please enable it in your browser!
Greenfoot back
ryanprescott
ryanprescott wrote ...

2012/12/11

Pivot Points? Attached Objects

ryanprescott ryanprescott

2012/12/11

#
Hey Guys, I am working on making an arm simulation that I can control, but I am having trouble finding or creating a method for attaching objects and creating a pivot or joint so that the shoulder will move around one point rather than the middle of the image. Any suggestions or code that would help and correspond to my task is very appreciated. -Ryan P.
Kyle273 Kyle273

2012/12/11

#
Are you looking for an arbitrary point or just the edge of an image? You can always cheat a bit and extend the arm's image so your point becomes the middle of the image. Otherwise, you'll have to do a bit of trig to get your offset length.
danpost danpost

2012/12/11

#
Kyle273 wrote...
Are you looking for an arbitrary point or just the edge of an image? You can always cheat a bit and extend the arm's image so your point becomes the middle of the image. Otherwise, you'll have to do a bit of trig to get your offset length.
I would not call that cheating. I believe that the simplest way is usually the best way. I feel that using what you have available to you, to accomplish what you want to do, is part of the art of programming. Letting the system work for you is 'smart' programming! BTW, my Pendulum Demo does exactly that: the length of the image of the arm is just about twice the arms length.
ryanprescott ryanprescott

2012/12/11

#
Ok, I was thinking exactly the same thing by extending the arm's image, which I may in fact have to do. The only thing is now I was wondering how to get the shoulder of the arm to be attached to the forearm so that when the shoulder moves the arm will move with it. I am having a very hard time, as I have tried to just set the rotation of the forearm to equal the shoulder, but then when I go to rotate the arm just makes a big blurred circle as if it was leaving its previous locations on the screen. Any suggestions?
ryanprescott ryanprescott

2012/12/11

#
Danpost, may I see the code or video behind your pendulum demo?
danpost danpost

2012/12/11

#
I do not think that that scenario is going to help much as far as what you are trying to do; there is no connection between object in it (just an object that rotates at a single point in the world). In fact, it creates the image from scratch (there is no image file).
ryanprescott ryanprescott

2012/12/11

#
ok, no worries. Any ideas on how to connect images (in theory) or make one rotate with the other, but still be able to move on its own like a human arm would? Thanks.
danpost danpost

2012/12/11

#
Create seperate actor classes for each part. Add to each one an instance List<Actor> array to hold the appendages (the parts that do not have further appendages do not need the list). Create the appendages for each part in the constructor of the class which that part extends from and add them to its list. Add an 'addedToWorld(World world)' method to add the listed objects into the world and call a method that you will add to each one (those with lists) called something like 'fixAppendages'. These methods will locate to listed appendages to there proper location dependent on its location. Sample class follows (the class shown only shows that basic set-up of the class; there is no movement code or anything included).
import greenfoot.*;
import java.util.List;
import java.util.ArrayList;

public class UpperLeg extends Actor
{
    List<Actor>parts = new ArrayList<Actor>();
    
    public UpperLeg()
    {
        LowerLeg lowerLeg = new LowerLeg();
        parts.add(lowerLeg);
        GreenfootImage image = new GreenfootImage(50, 180);
        image.fillOval(0, 80, 49, 99);
        setImage(image);
    }
    
    public void addedToWorld(World world)
    {
        world.addObject(parts.get(0), 0, 0);
        fixAppendages();
    }

    public void fixAppendages()
    {
        Joint joint = new Joint();
        getWorld().addObject(joint, getX(), getY());
        joint.setRotation(getRotation()+90);
        joint.move(90);
        parts.get(0).setLocation(joint.getX(), joint.getY());
        ((LowerLeg) parts.get(0)).fixAppendages();
        getWorld().removeObject(joint);
    }
}
The Joint class is just used to create a temporary object to be used to locate the joint (it is an empty class except for setting its image to a transparent (pixel-sized) point.
You need to login to post a reply.