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

2012/3/25

motionXdenom and motionXaccum

sermanas sermanas

2012/3/25

#
Hello, can someone explane to me what this means in greenfoot game?. Im trying create brick's game and i find some game examples, but i dont understand who's the hell is "denom" and "accum". Thanks alot who help me. :)
danpost danpost

2012/3/25

#
Where did you come across those terms? It might be helpful to include that bit of information, so it could be examined to answer your question.
sermanas sermanas

2012/3/25

#
im find this terms in bricks game --> ball code, its look like that:
public class Kamuoliukas extends Actor
{
    public static int BALLSIZEX = 10;
    public static int BALLSIZEY = 10;

    private int motionX;
    private int motionXdenom;
    private int motionXaccum;
    
    private int motionY;
    private int motionYdenom;
    private int motionYaccum;
    
    private int ballSpeed;

    public Kamuoliukas()
    {
        motionX = -20;
        motionXdenom = 10;
        motionY = -20;
        motionYdenom = 10;
        ballSpeed = (int) Math.sqrt(motionX * motionX + motionY * motionY);
    }
        
    public static Bounds getBounds(Actor go)
    {
        Bounds b = new Bounds();
        int width = go.getImage().getWidth();
        int height = go.getImage().getHeight();
        b.lx = go.getX() - width / 2;
        b.ty = go.getY() - height / 2;
        b.rx = b.lx + width;
        b.by = b.ty + height;
        return b;
    }
public void act(){

 //here you can create the behaviour of your object
        int newX = getX() + motionX / motionXdenom;
        int newY = getY() + motionY / motionYdenom;
        motionXaccum += motionX % motionXdenom;
        motionYaccum += motionY % motionYdenom;
        
        // X direction adjustments
        if (motionXaccum >= motionXdenom) {
            newX++;
            motionXaccum -= motionXdenom;
        }
        else if (motionXaccum < 0) {
            newX--;
            motionXaccum += motionXdenom;
        }

        // Y direction adjustments
        if (motionYaccum >= motionYdenom) {
            newY++;
            motionYaccum -= motionYdenom;
        }
        else if (motionYaccum < 0) {
            newY--;
            motionYaccum += motionYdenom;
        }

        // bounce off walls
        if (newX < BALLSIZEX / 2) {
            newX = BALLSIZEX / 2;
            motionX = -motionX;
        }
        else if (newX + BALLSIZEX / 2 >= BrickWorld.SIZEX) {
            newX = BrickWorld.SIZEX - BALLSIZEX / 2;
            motionX = -motionX;
        }
        
        if (newY < BALLSIZEY / 2) {
            newY = BALLSIZEY / 2;
            motionY = -motionY;
        }
        else if (newY + BALLSIZEY / 2 >= BrickWorld.SIZEY) {
            newY = BrickWorld.SIZEY - BALLSIZEY / 2;
            motionY = -motionY;
        }
        
        setLocation(newX, newY);
        int centerX = getX();
        int centerY = getY();
}
Okey i understand what means motionX, ballspeed, math.sqrt and ect.. but what is this denom and accum.. and sorry for bad English.
danpost danpost

2012/3/25

#
I believe what the coder did, was instead of using 'double' or 'float' variables, they used 'int' instead; so, to smooth out the motion, added the 'motionXdenom' variable ('denom' meaning: the denominator, as of a fraction) and used values for 'motionX' and others that were 'motionXdenom' times the normal. The 'motionXaccum' variable appears to refer to acceleration. Most programmers would just use 'speed' or 'velocity', and 'acceleration'; but maybe this one decided that since they were not the true values (being motionXdenum times there true values), he would not use those terms.
sermanas sermanas

2012/3/25

#
Thanks a lot danpost, you really helped :)
davmac davmac

2012/3/25

#
Actually, I can explain this completely, because I wrote that code :) Danpost is correct that 'denom' is short for 'denominator' as in a fraction. 'accum' is short for accumulator, because it accumulates the part of the fraction that wasn't translated into movement. It is, therefore, keeping track of position. Look at it this way: - the ball speed is given by 'motion / denom', eg motionX / motionXdenom - the ball position is getX() + (accum / denom), where accum / denom is always less than 1 Eg. if the horizontal speed is 2/5, and the x position is 0 exactly then: - motionX = 2 - motionXdenom = 5 - motionXaccum = 0 If you follow the code during act() (I edited your post to use tags) you'll see that 29. results in newX = 0 (i.e. the ball doesn't move this time) 41. motionXaccum = 2 The ball position is now newX + motionXaccum / motionXdenom = 0 + 2/5 = 2/5. In the next round we'll have: 29. newX = 0 (still) 41. motionXaccum = 4 The ball position is now 4/5. And the round after that: 29. newX = 0 41. motionXaccum = 6 At this point line 45 kicks in: if (motionXaccum >= motionXdenom) { newX++; motionXaccum -= motionXdenom; } As motionXaccum (6) is now >= motionXdenom (5), the lines inside the conditional block are executed. newX becomes 1, and motionXaccum also becomes 1 ( = 6 - 5). So, the ball now moves to the right by 1. The position is now 1+1/5. If you continue the process you'll see that the ball moves to the right by 1 in 2 of every 5 act() rounds, that is, its speed is exactly 2/5.
danpost danpost

2012/3/25

#
I guess I had the right reasoning behind the 'motionXdenom' usage, but I should have looked more closely at the 'motionXaccum' variable (something had to save the fractional part to facilitate the smoothness of motion). Thanks for the clarification, davmac.
You need to login to post a reply.