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

2012/5/29

Object Chain Movement

1
2
3
kartikitrak kartikitrak

2012/5/29

#
Hello all! I have come across a problem that I see as a difficult problem in my eyes. I would like to have 3 different classes of objects that at random are chosen. One object of a class would be a green triangle, another would be a blue square and another would be a red circle. I want to chose the objects at random(easy part, already achieved) and form a chain with them. When I say chain I mean their object borders touch and they follow the path of the previous object infront of them. Similiar to the game snakes but it would be a set length(for example the length of the chain would be 8, and the length wouldn't change until level is changed) and they move in the path of the previous object infront of them. I am also trying to make the path to be curved to make the game feel better as rigid turns don't look nice without the use of trigonometry. Something that would be awesome, if there was a way for objects to follow a certain predetermined path that can adapt to very maps without the use of coordinates. Is there a way for objects to follow a certain colour on the screen? For example if there is a visual pathway that is the colour blue, have objects touching blue follow the path and go a certain direction. Here is a small visual I quickly made in paint. The colour orbs represent different objects of different enemy classes. The curved line is their path they follow. Code guidance is welcome but not needed. http://i48.tinypic.com/rcj0k5.jpg
erdelf erdelf

2012/5/29

#
Nice idea, i think a path would be possible. Why not make the path parts as an actor and let the orbs check where an object of the path is. In the actor class is a method that could be helpfull
getOneObjectAtOffset(int dx, int dy, java.lang.Class cls)
it returns if there is an object at relative coordiantes. maybe you should make something like the following
if(getOneObjectAtOffset(1, 0, Path.class))
{
  setLocation(1,0);
}
if(getOneObjectAtOffset(-1, 0, Path.class))
{
  setLocation(-1,0);
}
if(getOneObjectAtOffset(0, 1, Path.class))
{
  setLocation(0,1);
}
if(getOneObjectAtOffset(0, -1, Path.class))
{
  setLocation(0,-1);
}
kartikitrak kartikitrak

2012/5/29

#
Thanks for the reply! Now would this allow dynamic path making. Meaning if I decided to make multiple levels would this adapt to each level or would I need to custom code the solution. For clarification: Basically the code that you provided allows the enemy objects to follow the path of the path, correct?
MatheMagician MatheMagician

2012/5/29

#
I believe you could use getWorld().getColorAt() instead. That way instead of having to add objects, you could just paint a blue line on the world. You would then change the background image to change the path. This code is set to blue. Make sure the color you use to make the line has an RGB value of 0,0,255 and a transparency of 255. Or just modify the code to whatever color your heart desires :)
Color Blue = new Color(0,0,255,255);
if(getColorAt(getX()+1, getY()).equals(Blue))  
{  
  setLocation(1,0);  
}  
if(getColorAt(getX()-1, getY()).equals(Blue))  
{  
  setLocation(-1,0);  
}  
if(getColorAt(getX(), getY()+1).equals(Blue))  
{  
  setLocation(0,1);  
}  
if(getColorAt(getX(), getY()-1).equals(Blue))  
{  
  setLocation(0,-1);  
}  
MatheMagician MatheMagician

2012/5/29

#
Oops, forgot to add getWorld()
Color Blue = new Color(0,0,255,255);  
if(getWorld().getColorAt(getX()+1, getY()).equals(Blue))    
{    
  setLocation(1,0);    
}    
if(getWorld().getColorAt(getX()-1, getY()).equals(Blue))    
{    
  setLocation(-1,0);    
}    
if(getWorld().getColorAt(getX(), getY()+1).equals(Blue))    
{    
  setLocation(0,1);    
}    
if(getWorld().getColorAt(getX(), getY()-1).equals(Blue))    
{    
  setLocation(0,-1);    
}
kartikitrak kartikitrak

2012/5/29

#
Sick! Two ways to do this. Now could you use the colour path finding to move at different speeds and if there is a gap between two objects, the object behind the one infront speeds up and connects to the front object? For example: *** *** --> ****** The space represents a gap and when there is a gap the objects at the back speed up and connect with the front objects.
kartikitrak kartikitrak

2012/5/30

#
Please check previous post.
erdelf erdelf

2012/5/30

#
For this you could follow this way: In the Actor of the orbs, you want a boolean, let's say first:
private boolean first;

public Orb(boolean first2)
{
    first = first2;
}
then check with the method
getObjectsInRange(int radius, java.lang.Class cls)
then lets make the act like the following.
public void act()
{
     if(!first)
     {
        List orbs = getObjectsInRange(3, Orb.class);        
        Iterator i = orbs.iterator();
        while(i.hasNext()) {
            Actor a = (Actor) i.next();
            setLocation(a.getX(), a.getY());
                
     }
kartikitrak kartikitrak

2012/5/30

#
This part would simply form the chain of orbs and move them along, correct? After forming the chain, can I use the getColorAt method that MatheMagician provided? Thanks for all the help guys :D
erdelf erdelf

2012/5/30

#
kartikitrak wrote...
This part would simply form the chain of orbs and move them along, correct?
yes
kartikitrak wrote...
After forming the chain, can I use the getColorAt method that MatheMagician provided?
yes I forgot something in the act method
     if(!first)  
     {  
        List orbs = getObjectsInRange(3, Orb.class);          
        Iterator i = orbs.iterator();  
        while(i.hasNext()) {  
            Actor a = (Actor) i.next();  
            setLocation([b]a.getX() - 1, a.getY() - 1[/b]);  
     }  
MatheMagician MatheMagician

2012/5/30

#
The original code me and erdelf gave you doesn't work because it doesn't prevent you from going back and forth. It is also too restrictive on the color parameters. Be very careful to make sure the lines you draw are ondly one pixel in width. Here is the modified code that will work.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

/**
 * Write a description of class Body here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Body extends Actor
{
    int x = 0;
    int y = 0;
    /**
     * Act - do whatever the Body wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
            Color Blue = new Color(0,0,255,255);    
            //if(x == 1)
            if(getWorld().getColorAt(getX()+1, getY()).getRed() < 100&&getWorld().getColorAt(getX()+1, getY()).getGreen() < 100&& x !=-1)      
            {      
                setLocation(getX() + 1,getY());    
                y = 0;
                x = 1;
            }      
            else if(getWorld().getColorAt(getX()-1, getY()).getRed() < 100&& getWorld().getColorAt(getX()-1, getY()).getGreen() < 100&& x !=1)      
            {      
                setLocation(getX()-1, getY());      
                y = 0;
                x = -1;
            } 
           
            else if(getWorld().getColorAt(getX(), getY()+1).getRed() < 100&& getWorld().getColorAt(getX(), getY()+1).getGreen() < 100 &&y != -1)      
            {      
                setLocation(getX(), getY()+1);      
                x = 0;
                y = 1;
            }
            else if(getWorld().getColorAt(getX(), getY()-1).getRed() < 100&&getWorld().getColorAt(getX(), getY()-1).getGreen() < 100&&y !=1)      
            {      
                setLocation(getX(), getY()-1);   
                x = 0;
                y = -1;
            }
    }    
}
Also, I was wondering if you guys would mind me posting this on Greenfoot, since erdelf did work on the code and kartikitrak's idea. I wouldn't like to steal either without your consent.
erdelf erdelf

2012/5/30

#
I was wondering if you guys would mind me posting this on Greenfoot, since erdelf did work on the code and kartikitrak's idea. I wouldn't like to steal either without your consent.
It wouldn't mind me, just post the link of your scenario here. btw. I am not sure if the orbs would build a chain.
MatheMagician MatheMagician

2012/5/30

#
Okay, I'm not sure if I'm going to include the closing gap code, I think I will just make the world position them correctly and they should stay. But they might not. I'll post the link right after I hear from kartikitrak and I upload the scenario.
kartikitrak kartikitrak

2012/5/30

#
Go for it. I think you guys did most of the work and I feel that I should be asking you to use your code. Thanks so much for the help.
MatheMagician MatheMagician

2012/5/30

#
Here is the link to the scenario. http://www.greenfoot.org/scenarios/5315
There are more replies on the next page.
1
2
3