Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7519227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:52:01+00:00 2026-05-30T01:52:01+00:00

I’m building a game using HTML5 canvas. You can find it here, along with

  • 0

I’m building a game using HTML5 canvas.

You can find it here, along with the source code: http://www.techgoldmine.com.

I’d make a jsFiddle, but in all honesty my attention span is too short (and myself mostly too stupid) to learn how it works.

I’m currently stuck at a function that looks at the positioning of certain elements on either side of the canvas and moves them so that the y-axis area they cover does not overlap. I call them turbines, but thin white rectangles would be more accurate. I suggest refreshing a few times to visually understand what’s going on.

This is the function that spawns the turbines:

function gameStateNewLevel(){
    for (var i = 0; i < 4; i++){
        turbine = {};
        turbine.width = 10;
        turbine.height = 150;
        turbine.y = Math.floor(Math.random()*600)
        if (Math.random()*10 > 5){
            turbine.side = leftSide;
        }else{
            turbine.side = rightSide;
        }
        turbine.render  = function (){
            context.fillStyle = "#FFFFFF"
    context.fillRect(turbine.side, turbine.y, turbine.width,turbine.height);
        }
        turbine.PositionTop = turbine.y;
        turbine.PositionBottom = turbine.y + turbine.height;

        turbines.push(turbine);

    }
    context.fillStyle = "#FFFFFF"       
    switchGameState(GAME_STATE_PLAYER_START);

}  

So far I’ve built (with the help of you wonderful people) a function (that is part of a loop) picking out each of these turbines, and starts comparing them to one another. I’m completely stumped when it comes to understanding how I’ll get them to move and stop when needed:

function updateTurbines(){
var l = turbines.length-1; 
    for (var i = 0; i < l; i++){
    var tempTurbine1 = turbines[i];
     tempTurbine1.PositionTop = tempTurbine1.y; 
     tempTurbine1.PositionBottom = tempTurbine1.y +    tempTurbine1.height;
     for (var j = 0; j < l; j++) { 
      var tempTurbine2 = turbines[j];
      tempTurbine2.PositionTop = tempTurbine2.y;
      tempTurbine2.PositionBottom = tempTurbine2.y + tempTurbine2.height;
      if ((tempTurbine1 !== tempTurbine2) && FIXME == true){
       if(tempTurbine1.PositionBottom >=    tempTurbine2.PositionTop){
                        turbines[j].y -=2;
                                            //A while loop breaks the browser :(

                    }

                }
            }FIXME = false;
        }
}

Any ideas or requests for additional explanation and info are more than welcome. I also have a feeling I’m severely over complicating this. Goddamn my head hurts. Bless you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T01:52:02+00:00Added an answer on May 30, 2026 at 1:52 am

    I’m afraid your code is a little bit messy do I decided to begin with a clean slate.

    • Use getters/setters for bottom and right. You can calculate them given the left/width and top/height values, respectively. This will save you from altering the complementary variable right when modifying e.g. left.
    • You seem to be looking for a collison detection algorithm for rectangles. This is quite easy if the rectangles have the same x-coordinate – two such rectangles do not collide if the bottom of the first is above the top of the other, or if the top of the first is under the bottom of the other. Use this algorithm along with a while loop to generate a new turbine as long as they collide.

    This is what I ended up with (it’s a separate piece of code as I stated, so you’ll have to blend it into your game): http://jsfiddle.net/eGjak/249/.

    var ctx = $('#cv').get(0).getContext('2d');
    
    var Turbine = function(left, top, width, height) {
        this.left = left;
        this.top = top;
        this.width = width;
        this.height = height;
    };
    
    Object.defineProperties(Turbine.prototype, {
        bottom: {
            get: function() {
                return this.top + this.height;
            },
            set: function(v) {
                this.top = v - this.height;
            }
        },
        right: {
            get: function() {
                return this.left + this.width;
            },
            set: function(v) {
                this.left = v - this.width;
            }
        }
    });
    
    var turbines = [];
    
    function turbineCollides(tn) {
        for(var i = 0; i < turbines.length; i++) {
            var to = turbines[i];
            // they do not collide if if one's completely under
            // the other or completely above the other
            if(!(tn.bottom <= to.top || tn.top >= to.bottom)) {
                return true;
            }
        }
    
        return false; // this will only be executed if the `return true` part
                      // was never executed - so they the turbine does not collide
    }
    
    function addTurbine() {
        var t, h;
    
        do { // do while loop because the turbine has to be generated at least once
    
            h = Math.floor(Math.random() * 300);
            t = new Turbine(0, h, 15, 100);
    
        } while(turbineCollides(t));
    
        turbines.push(t);
    }
    
    // add two of them (do not add more than 4 because they will always collide
    // due to the available space! In fact, there may be no space left even when
    // you've added 2.)
    addTurbine();
    addTurbine();
    
    function draw() {
        ctx.fillStyle = "black";
        ctx.fillRect(0, 0, 400, 400);
        ctx.fillStyle = "white";
    
        for(var i = 0; i < turbines.length; i++) {
            var turbine = turbines[i];
            ctx.fillRect(turbine.left, turbine.top,
                         turbine.width, turbine.height);
        }
    }
    
    draw();​
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
Seemingly simple, but I cannot find anything relevant on the web. What is the
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.