artheads's Forum Posts

  • Yes, known problem with fonts as dop2000 said. Use vertical alignment - Top, it helps in most cases.

  • So, the Separating Axis Theorem (SAT)...

    Hmm... interesting! Many thanks R0J0hound!

  • I don’t understand the question to have a rectangle instead of a square.

    Hi R0J0hound, thanks for your reply and contribution to the community of course!

    I managed to break all ties with the layout size, but I can't realize how to do the same spatial grid but not for circles or squares, but for rectangle sprites instead, which can change their angle during the gameplay? Now my rectangle sprites have like an "invisible field" on its longer sides that pushing out other similar sprites even before collision, but I'd like to keep it within the sprite rectangle. Is my explanation clear enough?

    const SIZE = 144, radius = 144;
    let grid = [];
    let objs = runtime.objects.sprite.getAllInstances();
    
    function calculateDistance(obj1, obj2) {
     return Math.sqrt((obj2.x - obj1.x) ** 2 + (obj2.y - obj1.y) ** 2);
    }
    
    function applyForce(obj1, obj2, dist) {
     let separation = Math.max(0, radius - dist);
     if (separation > 0) {
     let ang = Math.atan2(obj2.y - obj1.y, obj2.x - obj1.x);
     let force = separation / 12.5;
     obj1.offsetPosition(-force * Math.cos(ang), -force * Math.sin(ang));
     obj2.offsetPosition(force * Math.cos(ang), force * Math.sin(ang));
     }
    }
    
    globalThis.spatialGrid = function() {
     objs = runtime.objects.sprite.getAllInstances();
    
     for (let i = 0; i < objs.length; i++) {
     for (let j = i + 1; j < objs.length; j++) {
     let obj1 = objs[i];
     let obj2 = objs[j];
     let dist = calculateDistance(obj1, obj2);
     applyForce(obj1, obj2, dist);
     }
     }
    };
    
  • Hi everyone, I can't reply in the old post made by R0J0hound with a great example

    So, the question is what if I have a rectangle object instead of square, and can we avoid relying on layout size or its angle and work just with the necessary sprite?

    The code from this example:

    	const SIZE=150, radius=150, layoutW=runtime.layout.width, layoutH=runtime.layout.height,
    	 COUNT=Math.ceil(layoutW/SIZE)*Math.ceil(layoutH/SIZE);
    let grid=[];
    for(let i=0; i<COUNT; i++) 
    	grid.push([]); //init
    function hash(x,y){
    	return ((Math.floor(x/SIZE) + Math.floor(y/SIZE)*Math.ceil(layoutW/SIZE))%COUNT+COUNT)%COUNT;
    }
    function add2grid(obj,i){
    	if(grid[i].length==0 || grid[i][grid[i].length-1]!==obj) 
    		grid[i].push(obj);
    }
    globalThis.spatialGrid=function(){
    	for(let i=0; i<COUNT; i++) 
    		grid[i].length=0; //clear
    	let objs = runtime.objects.sprite.getAllInstances();
    	for(let i=0; i<objs.length; i++){
    		let obj = objs[i];
    		add2grid(obj, hash(obj.x-radius/2, obj.y-radius/2));
    		add2grid(obj, hash(obj.x+radius/2, obj.y-radius/2));
    		add2grid(obj, hash(obj.x-radius/2, obj.y+radius/2));
    		add2grid(obj, hash(obj.x+radius/2, obj.y+radius/2));
    	}
    	for(let i=0; i<objs.length; i++){
    		let obj1 = objs[i], h=hash(obj1.x, obj1.y);
    		for(let j=0; j<grid[h].length; j++){
    			let obj2 = grid[h][j];
    			if(obj1===obj2) {
    				grid[h][j]=null;
    				continue;
    			}
    			if(obj2===null) 
    				continue;
    			let dist=Math.sqrt((obj2.x-obj1.x)**2+(obj2.y-obj1.y)**2);
    			dist=Math.max(0, radius-dist)/10;
    			//dist=(radius*(Math.max(0, radius-dist)/radius)**0.5)/5;
    			//dist=radius/(dist/dist/10;
    			if(dist>0){
    				let ang=Math.atan2(obj2.y-obj1.y, obj2.x-obj1.x);
    				obj1.offsetPosition(-dist*Math.cos(ang), -dist*Math.sin(ang));
    				obj2.offsetPosition(dist*Math.cos(ang), dist*Math.sin(ang));
    			}
    		}
    	}
    };
    
  • Also, you can put all events of this layout in a group, then deactivate it when leaving the layout, and activate when back.

  • should not exceed 4096 pixels.

    Yeah, I understand this. Thank you, bro!

  • Not sure, but I think this is not possible, because even your loading screen its images and scripts or events needs to be loaded at first.

  • Thanks dop2000!

    the full image will be pretty big (2048x3072px).

    As I understood he says: "The size of the object in the layout doesn't matter" and then later he probably means sprite: "cannot be over 4096 pixels". Don't you think?

    So don't add more than 6-8 tiles to the tilemap.

    Do you mean add 6-8 to the tileset or to the tilemap object on the layout?

  • What should be better in terms of mobile performance for large layouts design:

    A) Tileset of 3-5 tiles total, with each tile of 1024x1024px and entire tilemap size of 15000x15000px (single but large object);

    B) Single sprite 1024x1024 with 3-5 animation frames placed across layout (many big but static objects).

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Quick workaround with events:

    Set variable to Array.AsJSON

    Then just use that variable in script.

  • Hi, I'm trying to get an array as JSON in JavaScript, but it seems like the Array API doesn't provide the method, so how to achieve this?

    const myArray = runtime.objects.myArray.getFirstInstance();
    const myArrayJSON = myArray.getAsJSON(); // My dream but not reality?!
    

    Tagged:

  • Wow, thank you all guys!

    R0J0hound special thanks! I like your approach in the first example.

    I'll be back if I get the desired effect with Canvas too, but if not, I'll probably use your approach #1 with a little tweak allowing it to stretch by its axis.

    Happy New Year! :)

  • Thanks, R0J0! I'm trying to do it with Canvas instead :))

  • This wobbler should... wobble and also stretch by centrifugal force, but tend to return to its initial angle, length and curve, like a spring does. Is the physics the only way?

    I'm able to create a straight "line" from sprites with physics, that behave pretty much like this, but I can't realize how to give a proper angle to each piece, so they will form a curve line like on the image "at rest".

    The second problem is I'd like only the load-tip (a ball on the wobbler's end) to collide with other objects, but not the wobbler's "line" itself, collisions should be disabled for the "line" - and that seems impossible using physics?