syed samoon Posted November 29, 2017 Share Posted November 29, 2017 Hi, i need a help. how to set a magnetic collectables for collecting the coin. like in subway surfer Quote Link to comment Share on other sites More sharing options...
Wingnut Posted November 29, 2017 Share Posted November 29, 2017 Hi SS! Ooooh, excellent challenge! Here's a little video of what is needed... https://www.youtube.com/watch?v=fRLQSlh0qWA Perhaps a 2-step thing: #1 - Detecting when coin is near-enough... to be affected by the magnetic field. #2 - Animating/flying the coin to/toward the constantly-moving player/magnetic-field center. For #1: Putting an invisible sphere around the player... which represents the size of the magnetic field... could be used with intersectsMesh() function, I suppose. (detection of coin within magnetic field influence area). For #2: I have no idea. Using the standard BJS animation system to move the coin TO a moving target (the magnet)... will likely not work. After coin -> magneticField intersection occurs, the coin will likely need to continuously do player.position.subtract(coin.position). This math (or its .negate()/inverse), should provide a FRESH coin direction-to-player... every frame (or faster). How to get the coin to move in the direction-to-player (a little bit) for EACH render frame... is a challenge. Let's listen to others' ideas, now. My synopsis/methods might not be the best way. So, let's ask for better ways, and/or perhaps someone will "flesh out" my idea, and try some test scenes. We could use this test scene... to do experiments with "coin sucking" https://playground.babylonjs.com/#36UHXZ#13 Caution: Each coin is currently parented to ground (might be important-to-know... for testers). Party on! Quote Link to comment Share on other sites More sharing options...
aWeirdo Posted November 29, 2017 Share Posted November 29, 2017 i think i would calculate distance instead of intersection, (personal preference) and if within preset "magnetic" distance, parent to magnet (for automatic follow) then move the coin in the direction of the magnet. RaananW and Wingnut 2 Quote Link to comment Share on other sites More sharing options...
RaananW Posted November 30, 2017 Share Posted November 30, 2017 Yep, this is what I would do exactly. Check distance using a simple iterator. If the distance is lower than a certain threshold, create a ray between the two, and apply a (constant) linear velocity in this direction. Which is kind'A what magnet is doing Quote Link to comment Share on other sites More sharing options...
Wingnut Posted November 30, 2017 Share Posted November 30, 2017 Hi guys. Wouldn't player.position.subtract(coin.position) be much faster than ray-casting? And aWeirdo's set-coin.parent-to-player... immediately, would be important, too, I think. Otherwise, the player could be moving forward SO FAST, that he out-runs the animation/velocity steps, and the coin would get (slowly) left behind. Player's speed out-runs the coin's catch-up speed. I think the position subtraction thing... gets you BOTH needed values. You get a direction to/from the coin/player, AND a magnitude (magnitude == distance), right? Maybe. Lots of Wingnut 'speculation' happening, here. Quote Link to comment Share on other sites More sharing options...
aWeirdo Posted November 30, 2017 Share Posted November 30, 2017 i think if you set parent, all you have to do really is reduce/increase coin.positions until it's 0,0,0 Wingnut 1 Quote Link to comment Share on other sites More sharing options...
Wingnut Posted November 30, 2017 Share Posted November 30, 2017 Yep, I think so, aW. But parenting could be a problem IF user wants to incorporate physics. Why put physics on the coins? *shrug* I dunno. If someone can steer me into converting vec3 magnitude... into a single distance float, then I'll try an updated/new demo. Thoughts? Smart coins? Put a beforeRender on each coin... and make them constantly "watch" if/not they are within the mag-field distance threshold? If so, dispose it, for now? Yeah. A crap-load of coins, constantly measuring their distance/direction-to-player. Probably smarter to use scene.beforeRender and iterate thru the collection of coins, eh? *nod*. I have a tendency towards autonomous "smart mesh" robots/mesh-behaviors. Not sure WHY I have that tendency. Quote Link to comment Share on other sites More sharing options...
aWeirdo Posted November 30, 2017 Share Posted November 30, 2017 well,a registerBeforeRender on mesh is just another function being executed during babylon's loop i think, so a single loop,might be better than to have two loops (babylon loop) and loop inside scene.registerBeforeRender.. just a optimization issue i suppose so not very important atm edit; just quickly checked up on it, and it's an observer, so when beforeRender is being processed notifyObservers runs a new loop. so still two loops. anyway, distance. // Returns the distance (float) var dist = BABYLON.Vector3.Distance(vector3_a, vector3_b); Wingnut 1 Quote Link to comment Share on other sites More sharing options...
Wingnut Posted November 30, 2017 Share Posted November 30, 2017 Hey, thx aW! Nice. https://playground.babylonjs.com/#36UHXZ#14 Line 58 sets magnetic field radius, which is used to show the temporary magnetic field "proxy ellipsoid" (woah, dirty talk, eh?) The super dooper looper pooper is in lines 126-134... coded in Genuine Wingnut Slop-Code. Works gooooood. Who's doing the coin animating, and the little fuzzy particle-puff of success? I can't do that part, cuz.. unions... ya know? heh aWeirdo 1 Quote Link to comment Share on other sites More sharing options...
aWeirdo Posted November 30, 2017 Share Posted November 30, 2017 there ya go, had to drop the parenting to board because of the rotations.. too much work adding offsets to stop the coins going up and down https://playground.babylonjs.com/#36UHXZ#16 Arte and Wingnut 2 Quote Link to comment Share on other sites More sharing options...
Wingnut Posted November 30, 2017 Share Posted November 30, 2017 Excellent! thx aW! https://playground.babylonjs.com/#36UHXZ#17 Throw-in a little coin rotation at line 138, eh? Arte 1 Quote Link to comment Share on other sites More sharing options...
Pryme8 Posted November 30, 2017 Share Posted November 30, 2017 You should make them have more force the closer they are to the collector, so they move slowly if they are far away and if your closer they zoom fast to give it a more inertia based feel. Quote Link to comment Share on other sites More sharing options...
Arte Posted November 30, 2017 Share Posted November 30, 2017 I tried to collect all coins... before bed! No luck.. I added some coin rotation line: 127-128 https://playground.babylonjs.com/#36UHXZ#18 Good job guys! Wingnut 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.