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

  • SEARCH
  • Home
  • 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 9141159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:36:33+00:00 2026-06-17T09:36:33+00:00

How do I generate objects on a map, without them occupying the same space

  • 0

How do I generate objects on a map, without them occupying the same space or overlapping on a HTML5 Canvas?

X coordinate is randomly generated, to an extent. I thought checking inside the array to see if it’s there already, and the next 20 values after that (to account for the width), with no luck.

var nrOfPlatforms = 14,
platforms = [],
platformWidth = 20,
platformHeight = 20;
var generatePlatforms = function(){
  var positiony = 0, type;
  for (var i = 0; i < nrOfPlatforms; i++) {
    type = ~~(Math.random()*5);
    if (type == 0) type = 1;
    else type = 0;
    var positionx = (Math.random() * 4000) + 500 - (points/100);
    var duplicatetest = 21;
    for (var d = 0; d < duplicatetest; d++) {
      var duplicate = $(jQuery.inArray((positionx + d), platforms));
      if (duplicate > 0) {
        var duplicateconfirmed = true;
      }
    }
    if (duplicateconfirmed) {
      var positionx = positionx + 20;
    }
    var duplicateconfirmed = false;
    platforms[i] = new Platform(positionx,positiony,type);
  }
}();

I originally made a cheat fix by having them generate in an area roughly 4000 big, decreasing the odds, but I want to increase the difficulty as the game progresses, by making them appear more together, to make it harder. But then they overlap.

In crude picture form, I want this

....[]....[].....[]..[]..[][]...

not this

......[]...[[]]...[[]]....[]....

I hope that makes sense.

For reference, here is the code before the array check and difficulty, just the cheap distance hack.

var nrOfPlatforms = 14,
platforms = [],
platformWidth = 20,
platformHeight = 20;
var generatePlatforms = function(){
  var position = 0, type;
  for (var i = 0; i < nrOfPlatforms; i++) {
    type = ~~(Math.random()*5);
    if (type == 0) type = 1;
    else type = 0;
    platforms[i] = new Platform((Math.random() * 4000) + 500,position,type);
  }
}();

EDIT 1

after some debugging, duplicate is returning as [object Object] instead of the index number, not sure why though

EDIT 2

the problem is the objects are in the array platforms, and x is in the array object, so how can I search inside again ? , that’s why it was failing before.
Thanks to firebug and console.log(platforms);

platforms = [Object { image=img,  x=1128,  y=260,  more...}, Object { image=img,  x=1640,  y=260,  more...} etc
  • 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-06-17T09:36:34+00:00Added an answer on June 17, 2026 at 9:36 am

    You could implement a while loop that tries to insert an object and silently fails if it collides. Then add a counter and exit the while loop after a desired number of successful objects have been placed. If the objects are close together this loop might run longer so you might also want to give it a maximum life span. Or you could implement a ‘is it even possible to place z objects on a map of x and y’ to prevent it from running forever.

    Here is an example of this (demo):

    //Fill an array with 20x20 points at random locations without overlap
    var platforms = [],
        platformSize = 20,
        platformWidth = 200,
        platformHeight = 200;
    
    function generatePlatforms(k) {
      var placed = 0,
          maxAttempts = k*10;
      while(placed < k && maxAttempts > 0) {
        var x = Math.floor(Math.random()*platformWidth),
            y = Math.floor(Math.random()*platformHeight),
            available = true;
        for(var point in platforms) {
          if(Math.abs(point.x-x) < platformSize && Math.abs(point.y-y) < platformSize) {
            available = false;
            break;
          }
        }
        if(available) {
          platforms.push({
            x: x,
            y: y
          });
          placed += 1;
        }
        maxAttempts -= 1;
      }
    }
    
    generatePlatforms(14);
    console.log(platforms);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am aware that you can generate create scripts to generate database objects, but
I have an XSD file which is used to generate some objects which are
I need to generate a couple of objects from lists in Javascript. In Python,
I'd like to generate a number of objects (in C++) based on the amount/number
I am trying to generate on-the-fly PDF reports from Java objects. I have not
My basic problem is to generate 2d renders of 3d objects, such as one
I have to generate XML in the below format <objects> <items> <item =delete> <searchfields>
I'm looking for a library/framework to generate/parse TXT files from/into Java objects. I'm thinking
.Net4 Entity Framework, N-Tier (so objects are detatched) I have 2 objects generated Database
I'm trying to generate GOFrame objects to generate a gene ontology mapping in R

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.