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

2014/10/7

Color in alpha values?

Entity1037 Entity1037

2014/10/7

#
I'm trying to make a circle in a black image transparent with alpha 200 so you can see through the image, but it doesn't work. How do I do it?
danpost danpost

2014/10/7

#
Entity1037 wrote...
I'm trying to make a circle in a black image transparent with alpha 200 so you can see through the image, but it doesn't work. How do I do it?
Pixel by pixel. When drawing an image onto another, the alpha values never decrease. You are adding color (whether transparent or not) to existing color, not subtracting from what already exists.
Entity1037 Entity1037

2014/10/11

#
I've got my lighting engine almost working, however when I insert more than one object, for some reason it lights all objects with the newest object's luminosity and color values. Could someone please help me? I have no idea what's going on. Here's my code:
import greenfoot.*;
import java.awt.*;
import java.util.List;

public class LightingOverlay extends Actor
{
    public void addedToWorld(World world){
        GreenfootImage base = new GreenfootImage(getWorld().getWidth(),getWorld().getHeight());
        base.setColor(Color.BLACK);
        base.fill();
        setImage(base);
        
        setLocation(getWorld().getWidth()/2,getWorld().getHeight()/2);
    }
    public void act()
    {
        //reset image
        GreenfootImage image = getImage();
        image.setColor(Color.BLACK);
        image.fill();
        //Do lighting
        List matter = getWorld().getObjects(Matter.class);
        if (!matter.isEmpty()){
            for (int l=0; l<matter.size(); l++){
                Actor object = (Matter) matter.get(l);
                lightPlayerView(object.getX(), object.getY(), ((Matter)object).getLuminosity(), ((Matter)object).getRedLight(), ((Matter)object).getGreenLight(), ((Matter)object).getBlueLight());
            }
        }
    }
    public void lightPlayerView(int x, int y, int luminosity, int redLight, int greenLight, int blueLight)
    {
        GreenfootImage image = new GreenfootImage(getImage());
        for (int i=0; i<image.getWidth(); i++){
            for (int j=0; j<image.getHeight(); j++){
                int intensity = luminosity - (int) Math.sqrt((i-x)*(i-x)+(j-y)*(j-y)); //how close point of lighting is to the origin of light
                if (intensity>0){
                    Color c = image.getColorAt(i,j);
                    int r = c.getRed(), g = c.getGreen(), b = c.getBlue(), a = c.getAlpha();
                    int alpha = a - intensity;
                    r += redLight*intensity/luminosity;
                    g += greenLight*intensity/luminosity;
                    b += blueLight*intensity/luminosity;
                    if (alpha>255)alpha=255;
                    if (r>255)r=255;
                    if (g>255)g=255;
                    if (b>255)b=255;
                    image.setColorAt(i,j,new Color(r,g,b,alpha));
                }
            }
        }
        setImage(image);
    }
}
Entity1037 Entity1037

2014/10/11

#
Also, I should probably mention: the distance the light travels is the same as the luminosity.
danpost danpost

2014/10/11

#
It would probably help if you post your Matter class. Also, the constructor of the object you added when all objects changed (and any methods used during the execution of that constructor).
Entity1037 Entity1037

2014/10/11

#
import greenfoot.*;

public class RedLight extends Matter
{
    public RedLight(){
        luminosity = 100;
        blueLight = 255;
    }
}
The luminosity stuff is at the bottom of the class.
import greenfoot.*;
import java.util.List;

public class Matter extends SpriteSheet
{
    public void shadowImagery(boolean s){}
    
    /**
                                      |||NOTICE|||
    Using setLocation more than once within a matter subclass may cause glitchy floor
    hit detection
    
    Copy everything between this blue comment and the next one to apply
    physics to a class (I will eventually apply elasticity properties so
    it's more realistic)
    
    I would like to note that the physics should prevent the Matter
    from one direction once blocks to the right of the player have been
    detected, and at no point should the matter EVER move if there are
    objects directly next to it in the direction it's trying to move
    (Seems like a duh note, but it's just laying out how the engine should
    function, and it is there to make sure that no one programms something that
    goes against this function)
    
                              Made by Entity1037
    */
    
    double xmove=0,ymove=0,gravityTimer=0;
    int frictionAmountMax = 2; //How many cycles until the object slows
    //down due to friction
    int xFrictionAmount=0;
    int yFrictoinAmount=0;
    String gravityDirection = "down";
    boolean gravity = true; //Just in case you want to switch gravity   ;D
    Class[] objects = {Box.class}; //All objects that can be collided with
    
    public void gravity(boolean g){gravity=g;}
    public void gravityDirection(String d){gravityDirection = d;}
    boolean onGround = false;
    
    int xca = 0; //How much xmove changes (Example: "xmove=xca" where "xca=-xmove")
    int yca = 0; //How much ymove changes (Example: "ymove=yca" where "ycs=-ymove")
    /**
       This method must be called every cycle before "physics()" is called!!!
       Protip: place it in the "act()" method!
       Protip#2: If you would like, you can just call this method at the beginning of "physics()".-
       -This will make it the same speed altering values apply to all matter subclasses
    */
    public void setSpeedAlteringValues(double X, double Y){xca=(int)X; yca=(int)Y;} //Lets you set speed altering values
    
    
    int x=getImage().getWidth()/2; //Half of the width of the hitbox
    int y=getImage().getHeight()/2; //Half of the height of the hitbox
    public void setHitBoxHalfValues(int X, int Y){x=X; y=Y;} //Lets you set hit box dimensions
    public int getHitBoxXHalfValue(){return x;} //Allows you to obtain hit box values of actors in "Matter"
    public int getHitBoxYHalfValue(){return y;} //Allows you to obtain hit box values of actors in "Matter"
    
    int xSpeedLimit=10;//This is self explanitory (caps how fast subclasses of matter can travel on the X axis)
    int ySpeedLimit=10;//Again, self explanitory
    public void setSpeedLimits(int xl, int yl){xSpeedLimit=xl; ySpeedLimit=yl;} //Lets you set the speed limit
    
    boolean particals=false;
    boolean wasGrounded=false;
    String collide = "";
    public void particals(boolean p){particals=p;}
    Actor collidedActor;
    int collidedGroundX=0;
    int collidedGroundY=0;
    boolean movehold=false;
    
    long prevTime=0;
    int cycleRate=8;
    public void physics(){
        collidedActor=null;
        wasGrounded=onGround;
        int collisionAmount=0;
        boolean xhold=false;
        boolean yhold=false;
        boolean ground=false;
        GreenfootImage image = new GreenfootImage(getImage());
        //Frame skip movement
        long currentTime = System.currentTimeMillis();  
        long timePassed = currentTime - prevTime;  
        if (prevTime == 0) {  
            timePassed = 0; // if we don't have a prevTime value, assume no time has passed  
        }  
        int actualxmove=(int)((int)xmove*(float)timePassed/cycleRate);
        int actualymove=(int)((int)ymove*(float)timePassed/cycleRate);
        prevTime = currentTime; // prevTime must be declared as an instance variable 
        //Collision detection
        collide="";
        while (collisionAmount<objects.length){
            if (objects[collisionAmount]==null)break;
            int a;
            //Down check
            a=y;
            while (actualymove>=0){
                for (int i=-x+1; i<x-1; i+=(x-2)/5){
                    Actor object = getAnObjectAtOffset(i, a,objects[collisionAmount]);
                    if (object!=null){
                        if (actualymove!=0){
                            if (gravityDirection.equals("down")){
                                collidedGroundX=getX()+i;
                                collidedGroundY=getY()+a;
                            }
                            collide+="down";
                            yhold=true;
                            ymove=yca;
                            actualymove=yca;
                            setLocation(getX(),getY()+a-y);
                        }
                        if (gravityDirection.equals("down")){
                            ground=true;
                            collidedActor=object;
                        }
                        image=new GreenfootImage(object.getImage());
                        break;
                    }
                }
                if (actualymove==0||a>=actualymove+y){break;}else{a++;}
            }
            //Up check
            a=-y;
            while (actualymove<=0){
                for (int i=-x+1; i<x-1; i+=(x-2)/5){
                    Actor object = getAnObjectAtOffset(i, a,objects[collisionAmount]);
                    if (object!=null){
                        if (actualymove!=0){
                            collide+="up";
                            yhold=true;
                            ymove=yca;
                            actualymove=yca;
                            setLocation(getX(),getY()+a+y);
                        }
                        if (gravityDirection.equals("up")){
                            ground=true;
                            collidedActor=object;
                            collidedGroundX=getX()+i;
                            collidedGroundY=getY()+a;
                        }
                        image=new GreenfootImage(object.getImage());
                        break;
                    }
                }
                if (actualymove==0||a<=actualymove-y){break;}else{a--;}
            }
            //Left check
            a=-x;
            while (actualxmove<=0){
                for (int i=-y+1; i<y-1; i+=(y-2)/5){
                    Actor object = getAnObjectAtOffset(a, i,objects[collisionAmount]);
                    if (object!=null){
                        if (actualxmove!=0){
                            collide+="left";
                            xhold=true;
                            xmove=xca;
                            actualxmove=xca;
                            setLocation(getX()+a+x,getY());
                        }
                        if (gravityDirection.equals("left")){
                            ground=true;
                            collidedActor=object;
                            collidedGroundX=getX()+a-1;
                            collidedGroundY=getY()+i;
                        }
                        image=new GreenfootImage(object.getImage());
                        break;
                    }
                }
                if (actualxmove==0||a<=actualxmove-x){break;}else{a--;}
            }
            //Right check
            a=x;
            while (actualxmove>=0){
                for (int i=-y+1; i<y-1; i+=(y-2)/5){
                    Actor object = getAnObjectAtOffset(a, i,objects[collisionAmount]);
                    if (object!=null){
                        if (actualxmove!=0){
                            collide+="right";
                            xhold=true;
                            xmove=xca;
                            actualxmove=xca;
                            setLocation(getX()+a-x,getY());
                        }
                        if (gravityDirection.equals("right")){
                            ground=true;
                            collidedActor=object;
                            collidedGroundX=getX()+a;
                            collidedGroundY=getY()+i;
                        }
                        image=new GreenfootImage(object.getImage());
                        break;
                    }
                }
                if (actualxmove==0||a>=actualxmove+x){break;}else{a++;}
            }
            collisionAmount++;
        }
        //Gravity
        if (ground==false&&onGround==false&&gravity==true) {
            if (gravityDirection.equals("down"))ymove+=0.25;
            if (gravityDirection.equals("up"))ymove-=0.25;
            if (gravityDirection.equals("left"))xmove-=0.25;
            if (gravityDirection.equals("right"))xmove+=0.25;
        }
        onGround=ground;
        image.scale(1,1);
        //Friction
        /**if (ground==true&&xFrictionAmount<frictionAmountMax){
            xFrictionAmount++;
        }
        if (xFrictionAmount==frictionAmountMax&&onGround){
            xFrictionAmount=0;
            if (gravityDirection.equals("up")||gravityDirection.equals("down")){
                if (xmove>0)subxmove--;
                if (xmove<0)subxmove++;
            }
            if (gravityDirection.equals("left")||gravityDirection.equals("right")){
                if (ymove>0)subymove--;
                if (ymove<0)subymove++;
            }
        }*/
        //Speed Limit
        if (xmove>xSpeedLimit)xmove=xSpeedLimit;
        if (xmove<-xSpeedLimit)xmove=-xSpeedLimit;
        if (ymove>ySpeedLimit)ymove=ySpeedLimit;
        if (ymove<-ySpeedLimit)ymove=-ySpeedLimit;
        //Move commands
        if (movehold==false){
            if (xhold==false)setLocation(getX()+actualxmove,getY());
            if (yhold==false)setLocation(getX(),getY()+actualymove);
        }
        //Send Info For ground detection visual
        /*if (ghitBox!=null){
            int ypos=ymove+1;
            if (ypos<1)ypos=1;
            if (!collide.equals("")){
                ((GroundDetectionVisual)ghitBox).changeImage(x*2-8,ypos);
            }else{
                ((GroundDetectionVisual)ghitBox).changeImage(x*2-8,ypos);
            }
            ghitBox.setLocation(getX(),getY()+y+(ymove/2));
        }
        if (hitBox!=null){
            ((HitBoxVisual)hitBox).changeImage(x*2,y*2);
            hitBox.setLocation(getX(),getY());
        }*/
        //Partical effects
        /*if (particals==true){
            int p=(int)ymove;
            if (p<0)p=-p;
            if (p==0)p=1;
            if (collide.equals("down")){
                for (int i=0; i<3*p; i++){getWorld().addObject(new SolidPartical(Greenfoot.getRandomNumber(7)-3,Greenfoot.getRandomNumber(p)-p-1,image),getX()+Greenfoot.getRandomNumber(getImage().getWidth())-getImage().getWidth()/2,getY()+getImage().getHeight()/2);}
            }
            if (collide.equals("up")){
                for (int i=0; i<3*p; i++){getWorld().addObject(new SolidPartical(Greenfoot.getRandomNumber(7)-3,Greenfoot.getRandomNumber(p)+1,image),getX()+Greenfoot.getRandomNumber(getImage().getWidth())-getImage().getWidth()/2,getY()-getImage().getHeight()/2);}
            }
            p=(int)xmove;
            if (p<0)p=-p;
            if (p==0)p=1;
            if (collide.equals("left")){
                for (int i=0; i<3*p; i++){getWorld().addObject(new SolidPartical(Greenfoot.getRandomNumber(p)+1,Greenfoot.getRandomNumber(4),image),getX()-getImage().getWidth()/2,getY()+Greenfoot.getRandomNumber(getImage().getHeight())-getImage().getHeight()/2);}
            }
            if (collide.equals("right")){
                for (int i=0; i<3*p; i++){getWorld().addObject(new SolidPartical(Greenfoot.getRandomNumber(p)-p-1,Greenfoot.getRandomNumber(7)-3,image),getX()+getImage().getWidth()/2,getY()+Greenfoot.getRandomNumber(getImage().getHeight())-getImage().getHeight()/2);}
            }
        }*/
    }

	public void move(int movement){
		double radians = Math.toRadians(getRotation());
        xmove = Math.cos(radians)*movement;
        ymove = Math.sin(radians)*movement;
	}
    
    public void removeThis(){
        getWorld().removeObject(this);
    }
    
    /**End of code (don't forget to call the "physics" method)!*/
    
    /**
     * Hit box collision detection code (just in case you have no idea
       how to write your own collision detection code)
       P.S.: I do realize that any hit box bigger than the object's
       image will not regi
    */
    public void detectCollision(){
        Actor matter = getOneIntersectingObject(Matter.class);
        if (matter!=null){
            int XX = ((Matter)matter).getHitBoxXHalfValue();
            int XY = ((Matter)matter).getHitBoxYHalfValue();
            for (int w=matter.getX()-XX; w<=matter.getX()+XX; w++){
                for (int h=matter.getY()-XY; h<=matter.getY()+XY; h++){
                    if (w>getX()-x&&w<getX()+x&&h>getY()-y&&h<getY()+y){
                        //Insert code here
                    }
                }
            }
        }
    }
    
    static int luminosity = 0, redLight = 0, blueLight = 0, greenLight = 0;
    
    public static int getLuminosity(){return luminosity;}
    public static int getRedLight(){return redLight;}
    public static int getBlueLight(){return blueLight;}
    public static int getGreenLight(){return greenLight;}
}
danpost danpost

2014/10/11

#
By having your 'luminosity', 'redLight', 'blueLight' and 'greenLight' fields static, they become class fields, not instance fields. If you want each Matter instance to have its own values for these fields, you need to remove the 'static' qualifier on line 304 as well as from the methods on lines 306 through 309.
Entity1037 Entity1037

2014/10/11

#
Ah! Thank you!!
You need to login to post a reply.