Jump to content

Disposed meshes still colliding


TechSlave
 Share

Recommended Posts

Here i am again

 

Couldn't make the collision....

look http://techslave.com.br/games/spacemission/

for(var e = 0; e < ENEMYS.length; e++) {
        var en = ENEMYS[e];
        en.position.x += 5; //Velocidade de movimento dos inimigos
        if(en.position.x > 500) {
          en.dispose();
          ENEMYS.splice(e, 1);
          e--;
        }
        if (en.intersectsMesh(BULLETS, true)){<------- how should i do that?
          en.dispose();
        }
      }

      for(var b = 0; b < BULLETS.length; b++) {
        var cb = BULLETS;
        cb.position.z += 8;
        if(cb.position.z > 800) {
          cb.dispose();
          BULLETS.splice(b, 1);
          b--;
        }
      }

i couldn't think of a way to create a link between the enemies and the bullet, they just spawn in their own function, how should i do that?

Link to comment
Share on other sites

nice lava texture!

tried to copy it but got an error... later that

..

tried this 

      /*
      for(var j = 0; j < 20; j++) {
        for(var i = 0; i < 30; i++){
          if(ENEMYS[j] === null && BULLETS === null){

          }
          else{
            var hit = BULLETS.intersectsMesh(ENEMYS[j]);
          console.log(hit);
          }
          */

http://playground.babylonjs.com/#16MDIB#0 

Link to comment
Share on other sites

:)  You can copy the playground to your home... using Get .ZIP button, if you wish.

This scene has some other issues.  After quick examination... I'd say too much stuff is happening inside the two beforeRender funcs.  VERY rare to have two beforeRender funcs, but I guess it's legal. 

FPS is low.  Flashing boundingBox lines on enemy cubes... indicate that continuous spawn/dispose is happening there... not too good.  This scene needs a rather major re-think.  I hope you don't take offense... I'm on your side all the way. 

I'm hoping a forum helper with more shoot-em-up game-coding experience than I... comes to the rescue.  :)  Be patient, if you can.  thx.

 

Link to comment
Share on other sites

Hi again.  I'm going to ping @Temechon and see if he'll help us fix our playground for optimal performance.  It looks like you might have based some of your code on the Toad Attack demo.  That is exactly where I browsed-to... to learn about "enemy pooling" and "shot pooling".  Is the term/phenomena of "pooling"... the correct terminology for this?  *shrug*

I am trying to make @royibernthal teach me about "pooling", but I think he sees my thick skull and slow-learning issues.  :)  He's scared of my stupidity.  (So is my dog.)  :D

Pooling is done instead-of disposing, correct?  In a shooter like this... maybe 8 total shot-mesh needed?  And perhaps 8 total enemy ships?

And then what?  Perhaps two pool-based calls in the render loop... serviceShotPool() and serviceEnemyPool()?   (Also serviceMyShip() too, of course.)

(service == update as used above)

I have SO MUCH to learn.  *sigh*  My brain is full!  Ow!  :)

Link to comment
Share on other sites

One two loops 

for(var b=0; b<bullets.length;b++){

var cb = bullets;

enemey loop....

}

 

or which would be more ideal is bind a callback to a hit response on the bullets when you spawn them and test if it's an enemy of so dispose both and splice the arrays.

 

 

im on my phone sorry for partial response.

make sure your only using one before render loop, correct structure would be one loop, if you want to split it up just make functions that are called by the loop.

Link to comment
Share on other sites

  • 2 weeks later...

Hello guys

@Pryme8

still on this, couldn't manage to make things make sense...

 

was checking the labels, the bullets are undefined and i have zillions of cubes, that way i can't check the collisions because i can't name them, like undefined.intersectsMesh(cube9999)

the bullets are just spawned, not "created", i mean, they just get there, get out, unnamed, and i think if i manage to destroy the cube in some way, it will respawn at the other side.

should i change the logic?

http://playground.babylonjs.com/#21XTKA

2016-09-25.png

Link to comment
Share on other sites

Guys, i need help.... again!

i've managed to destroy the enemies, but this brought problems back.

http://playground.babylonjs.com/#16MDIB#1

i did this:

      for(var b = 0; b < BULLETS.length; b++) {
        var cb = BULLETS;
        cb.position.z += 8;
        if(cb.position.z > 500) {
          cb.dispose();
          BULLETS.splice(b, 1);
          b--;
        }
        for(var t = 0; t < ENEMYS.length; t++) {
          var te = ENEMYS[t];

          if(cb.intersectsMesh(te)) {
           // cb.position.z = 790;
            te.dispose();
            score = score+1;
            console.log('Score: ' + score)

          }
        }
      }

-if i hold spacebar, the enemies stop spawning, couldn't figure why;

-one bullet destroys everything in its path;

-it counts 8 points per enemy.

if i dispose() or cb.position.z = 790 the bullet, it just stays there, bringing back the same fucking bug i had before...

 

please help me, i'm going crazy!

Link to comment
Share on other sites

Calm down... this is all self-inflicted.  :)

First, you have a ton of crap in your renderLoop, so even if someone WAS willing to react to your panic, they would quickly be discouraged away-from helping you... just because of the dis-organization of your coding style.  And it is that disorganization... that is making you go crazy.

If you really really want to get your game-coding crap together... carefully look at this...

http://playground.babylonjs.com/#21XTKA#2

It's still broken.  In fact, more broken than before.  But look at the way the code is organized.  Look at the render loop.  Nice and clean... easy to turn-on/off function calls... everything is saner, yes?  Just stare at the code... don't worry that it's broken.  And you need to go look at more of Temechon's games... get OOPy.

You have a fairly big project going here, and it is not AT ALL unusual that you would be going crazy at this time.  Many others do the same... because the code has gotten unwieldy (out of control and messy).  The result is a difficult-to-debug monster.  I don't mean to hurt your feelings, and I'm sure not an expert at code organization, but... your playground is not in a condition that helpers find appealing.

You can say... "oh, I'm SO CLOSE, I'm SO CLOSE!"... but no, you're not... because you have "spaghetti code".  Ask any helper on the forum which playground they would rather debug. Yours, where almost everything works, or mine, where almost nothing works.  They'll say mine, because... it's easier to troubleshoot.  If a helper wants to turn-off ship spawning and ONLY work with bullets... it's easy in my version.  Just remark-out the line in the renderLoop about serviceEnemies.  It's much easier to concentrate on a single section of the game... because we made it "modular".

Can you get MY playground... working again... without piling a bunch of crap into the renderLoop again?  If so, do it... you won't regret it.  Make even more "re-usable" functions... spawnBullet(), spawnEnemy(), deleteEnemy(), deleteBullet(), moveShipLeft(), moveShipRight(), etc, etc.  Tiny functions... each doesn't do much.  And remember... you can (and will) call little functions from within other little functions. 

Be angry with me if you must, but I am telling you something that will make your life, and forum helper lives... much nicer.  I promise.

Link to comment
Share on other sites

I will help as soon as I can, I am currently developing a BJS scene for a paid client and have 3 websites on my plate right now as well. I feel bad just leaving you with this.

Quick bit of advice there are better ways to do this then Im about to mention but it will work

In your renderloop have a for loop that iterates over every bullet in your array (you can even stick it in the loop that moves them) and fire off a collision test then, if it returns true you know what bullet and the collision even should have the target.

oh and stop using meshIntersect, and get some colliders involved.

https://gamedevelopment.tutsplus.com/tutorials/webgl-physics-and-collision-detection-using-babylonjs-and-oimojs--cms-24114

Link to comment
Share on other sites

@Wingnut How could i be angry at you man? You're helping me!

Not the help i want, but the help i need... like Batman hahaha

I appreciate a lot your effort, my code is now far easier to understand, even for myself haha... will move on with that piece of art you did.

 

@Pryme8 I thought oimo.js was used for physics-based collision, with gravity and things like that... thought my project was easier than that... i was wrong lol

i'll follow your advices, thank you again for the help, let's see what i can get, thank you in advance!

btw, it's far easier to find help on collisions camera vs mesh than mesh vs mesh, it's like everyone just builds first person shooters ¬¬'

Link to comment
Share on other sites

Hi TS!  What a great attitude you have... very kind of you.  I'm sure glad you're not grumpy with me.  I really like your enthusiasm.

And, yes, I think Pryme8 is mistaken.  You don't need a physics engine for this, and it would slow down your game-play.  Your left-right ship movement is nice, your shot repeat rate is nice... I think you are right on-target, so far.  Just a little shot management and enemy management to do... and you'll be successful.

I'm no game programmer, or much of a programmer at all, but I can tell you what Temechon might do.  He would write special classes.  He would write a shotClass, an enemyClass, and a playerClass... and try to make them "generic"... re-usable in many future games. 

After he coded and included these special classes, you might see code like... var myPlayer = new playerClass(name, mesh, position, rotation, scaling, animation, behaviors, whatever, scene)... or perhaps var newShot = new shotClass(blah, blah, blah, etc)... things like that.

These playerClass, shotClass, and enemyClass would be re-usable game after game... because they are versatile/flexible.  There are many parameters/args used when constructing one of these special classes, and with those many parameters... comes versatility.  Lots of knobs to set... to tell the constructor HOW to build the player, shot, or enemy.  Then the returned object itself... might have lots of 'defines' on it... as in... lots of properties and methods... all useful for THAT type of class.  But many of those would come from BJS, too.

For example, his enemyClass objects would likely have a .mesh property, or perhaps a .meshAssociatedWithThis property... that contains the name of the mesh, or a reference to the actual mesh... that is used for this enemy class.  Later, if you want to change the shapes for all your enemy craft, you could simply change the class definition itself... making a different mesh be generated when new enemyMesh() was called.  Or, you could include the mesh shape in the construction... such as var myEnemy = new enemyClass(name, mesh, color, size, blah, blah, scene).

And... there's other ways.  In many ways, an enemy craft and a player craft... have some similar characteristics.  So, you could make a class named genericCraft, and it could have two subClasses named friendlyCraft() and enemyCraft()... both share SOME properties and methods that are inherited from genericCraft.  Later... you might want to build a tankCraft class... and guess what?  Just add ANOTHER subClass beneath genericCraft, and IT automatically inherits all the powerful properties and methods FROM genericCraft. 

See, you just added a tank to your game, yet... you didn't need to fully flesh/stock it with functions and properties... because it is a subClass of genericCraft.  It inherited a bunch of power (props and methods)... from its superClass (the class above it).  No need to re-write that code.  You made the code on genericCraft.... usable by MANY game vehicles.  Write once, use everywhere.  :)

Ok, that's enough Object Oriented Programming talk for one day.  I'm not even CLOSE to being pro at OOP, but I understand the principles, and the overwhelming great reasons to code in that way.  Believe it or not, once you get a "toolbox" of re-usable game object classes (and Temechon's website gives us a crap-load of them for free)... it is quite conceivable to release a new video game... every 2-3 days.  That's pretty amazing.  Get a great toolbox of nicely-reusable classes, and it can be done.

But, no hurry or pressure.  It's best to ease-into OOP.  At first glance, Temechon's game code looks like cave paintings.  But after staring at it for awhile... you'll realize... it is a game-generating machine.  Learn to drive that machine, and you are spitting-out fresh games every half-hour, if you want.  :)

How 'bout, for now, no more than 20 enemy and 20 shots?  Would that be a good amount of both... for testing?  I think so.  I'll be here, so will Pryme8 and others.  We will play with your shot management and enemy management methods... get them working smooth as silk.  I hope you understand the reason for "modular".  You know you will have things right... when your game code reads like an English book.

var updateAll(scene) {
    updateShots(scene);
    checkCollisions(scene);
    updateEnemies(scene);
    updatePlayerShip(scene);
}

See how nice that looks, and it's easy to find where the problem is, eh?  The more like this, the better (imho).  In the old days, in different programming languages, these were often called "jump tables"... and they were/are real popular.  A pro-coder friend used to say to me... NO MORE THAN 8 lines of code in ANY function.  :)  Most times, such rules CAN be followed... and results in great code. 

In the early days of "lots of little functions", scope is sometimes a problem.  But you have set your ENEMY and BULLETS vars... in a global way (not established inside a function... but established outside... in main-line code).  This should help you with scope problems.  A scope problem would look-like "BULLETS is undefined" error... when trying to do something with the BULLETS array... while coding inside-of the updateShots() or checkCollisions() functions.  As you likely know... vars that get defined inside of a function... are not usable/findable outside of that function.  Those are called local variables, because they are used locally, within the function, ONLY FOR THAT FUNCTION.  Conversely, global variables can be used-within and seen-within almost all functions.  Scope issues get easier... the more you code... and eventually, disappear (because you've turned pro).  :)

I hope I have not told you anything incorrect. :)  Hopefully, my friends will correct my mistakes.  I'm still learning, too.  Be well, talk soon.

Link to comment
Share on other sites

https://pryme8.github.io/LSBAG/
Left- A, Right- D, Fire - Space.
You can still look around with the mouse as well...

https://github.com/pryme8/LSBAG


Not the most efficient way to do any of this... but it works sorry I cant explain more, I only had about a hour and half to do this for you today so yea... should get you there though!

@TechSlave

Link to comment
Share on other sites

Holy crap :blink:

you just did what i'm trying for months in an hour and half!

and it's amazing!

i don't know what to say... i'll just study your code until i learn everything... you even did things that i couldn't do but was procrastinating, like the model of the ship (got bugs when importing from Blender).

Thank you very much! You're the man!

or The Ape!

Link to comment
Share on other sites

  • 1 month later...

@Pryme8Hello! 

How can i attach a model to the balls? i've tried putting the model on a library like 

 //Loader------------------------------------------------------------------------------------

    BABYLON.OBJFileLoader.OPTIMIZE_WITH_UV = true;
    var loader = new BABYLON.AssetsManager(scene);
    var shipTask = loader.addMeshTask("player_ship", "", "./", 'ship.OBJ');
    var enemTask = loader.addMeshTask("enemy", "", "./", 'asteroid.babylon');
    //Loader------------------------------------------------------------------------------------
    //enemy

    enemTask.onSuccess = function(task) {
      LIBRARY['enemy'] = enemTask.loadedMeshes[0];
      LIBRARY['enemy'].scaling.x = 0.1;
      LIBRARY['enemy'].scaling.y = 0.1;
      LIBRARY['enemy'].position = new BABYLON.Vector3(1000, 1000, 1000);
    };

but if i attach it on the balls they vanish... tried many ways, just failed at alll of them

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...