Results 1 to 2 of 2
  1. #1
    EscSequenceAlpha is offline Senior Member
    Join Date
    Apr 2012
    Posts
    115
    Rep Power
    0

    Default Game programming : please help me understand this example from book

    Ok so I bought a book that takes me through creating the astroids game. They gave me code in chapter 3 but I dont quite understand how velocity works... perhaps someone could help explain this to me. Ok so we create the astriods

    Java Code:
    for (int n = 0; n<ASTEROIDS; n++) {
    			ast[n] = new Asteroid();
    			ast[n].setRotationVelocity(rand.nextInt(3)+1);
    			ast[n].setX((double)rand.nextInt(600)+20);
    			ast[n].setY((double)rand.nextInt(440)+20);
    			ast[n].setMoveAngle(rand.nextInt(360));
    			double ang = ast[n].getMoveAngle() - 90;
    			ast[n].setVelX(calcAngleMoveX(ang));
    			ast[n].setVelY(calcAngleMoveY(ang));
    		}
    we set the move angle (lets say its heading north, 0)
    now we calculate "ang" which is move angle - 90 (west, 270)
    we then use ang to calculate the angleMove

    public double calcAngleMoveX(double angle)
    {
    return (double) (Math.cos(angle * Math.PI / 180));
    }

    what is it that this method is returning? (the book just uses terms like update x & y value), is this method returning the updated angle of movement? how do you work that out from current angle - 90?

    now we use this value to calculate velocity (setVel above) - what is velocity, the speed of the object? how do we work that out from the new move angle?

    also, because setvelx and setvely use the same base "calcAngleMoveX(ang)" wont x and y be the same?

    I dont understand any of this stuff... could one you smart guys explain it all in simple terms so I can visualise how the objects are able to move around. points for whoever explains it the clearest
    Legend has it the moderators and senior members of java-forums.org were able to code skyrim using only 701 lines of java... or so the legend goes.

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,058
    Rep Power
    5

    Default Re: Game programming : please help me understand this example from book

    From what I remember about Asteroids on Atari, velocity might be a fixed value, maybe a static field. From this incomplete code, it looks like each Asteroid has an angle field (ang), and methods like calcAngleMoveX(...) use basic trigonometry to convert the polar vector (angle and velocity) into X and Y rates. The setVelX(...) method probably takes the result of that calculation and stores it somewhere.

    Those are kind of awkward method names, but there's one good thing you should notice about them: a method whose name begins with "calc" should, as its name suggests, simply calculate something without changing any class fields, and calcAngleMoveX(...) follows that convention. But when a method name begins with "set", you can assume it changes the value of a field. Preferably a field whose name matches the method name, so a method called setVelX(...) should set a field called velX. You can't really count on everyone naming things that way, but it's a good habit to get into.
    Last edited by kjkrum; 07-14-2012 at 11:00 AM.
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. arcade game programming
    By mike1959 in forum Java Gaming
    Replies: 2
    Last Post: 02-01-2013, 04:17 PM
  2. Programming a Game?
    By gt123 in forum New To Java
    Replies: 4
    Last Post: 01-01-2008, 12:41 PM
  3. Programming an Address book
    By d.anthonii in forum New To Java
    Replies: 0
    Last Post: 12-31-2007, 05:26 PM
  4. java game programming
    By gammaman in forum New To Java
    Replies: 1
    Last Post: 12-25-2007, 01:01 AM
  5. Replies: 1
    Last Post: 08-07-2007, 05:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •