Jump to content

Boomerang effect


Bavragor
 Share

Recommended Posts

Hi,

I'm trying to replicate some sort of boomerang effect on an moving object. In short I'm shooting away a sprite but instead of shooting the sprite away in a straight line, I'd like it to move in an 'arc'.

Regrettably I have no idea how to achieve this sort of movement.

If someone could explain me the theory around such movements, that would be awesome. If you could include a code example, you'd be my hero (of the day). ;)

P.S. I'm using P2 physics.

Thanks in advance.

Link to comment
Share on other sites

Okay, then you want a a circular tween. Theres a very good resource here for tween example: http://gamemechanicexplorer.com/#easing-8

There is also an example of the tween in native JS, incase you want to write a custom function: http://jsfiddle.net/zHhus/

Just for good measure, I went and created an example in the sandbox for you ;) Here's the link! I hope this is what you were looking for! : http://phaser.io/sandbox/eAldhytV

Link to comment
Share on other sites

There is a bounce tween, but bouncing on collision is for physics only. In my experience, the less you use physics, the better your performance is. You can, however, combine the two.. so if you tween the sprite.body.x, instead of sprite.x, then you will move the physical body that Phaser sees and detects collisions on. 

Alternatively, you could write a custom function that increases and decreases the sprites velocity x and y accordingly to form an arc.. but they just feels like over-engineering. Let me know how you get on tweening the sprite.body and if it still isn't the effect you desire, I'll see if I can take a crack at writing a function for it :)

Link to comment
Share on other sites

If you are using p2, is that you want physics. I would use forces. When you throw the boomerang, in each update you add a continuous force in one direction (instead of setting velocities directly). Is like doing sprite.velocity.x += X each frame but instead you can use a vector for it and let the engine do the calculations. See http://phaser.io/docs/2.4.3/Phaser.Physics.P2.Body.html#applyForce

The force will be applied perpendicular to the moving direction, this way:

e0193624e4b3178d90e6d37070031f9b.png

And the 'perfect' way to do it would be setting the perpendicular force left or right depending on the way that the boomerang is thrown (rotating clockwise or counterclockwise) and also the force will be higher or not depending on the speed that is rotating (the faster it rotates, the more perpendicular effect it has).

And, for 'ultra' perfect reality, the boomerang loses angular speed because of friction and such, so each update the perpendicular force will be lesser as the boomerang is rotating at less speed. Also note that a real boomerang perpendicular force is not only based on the rotating speed, but also the moving speed, as a still boomerang rotating at full speed does not move in any direction. But I think that for a game this is overworking (If you are not working on a simulator, as nowadays there are simulators for everything xD)

PD: Sorry if my Photoshop skills astonish you xD

Link to comment
Share on other sites

Why not just use trig and have a rotationSpeed variable and a rotationAcceleration variable? with rotationDegrade = true / false?

rotationSteps = 0.1;
if(boomarang[i].rotationDegrade == false){
    boomarang[i].rotationAcc = rotationSteps;
    if(boomarang[i].rotationAcc > maxRotationAcc){
        boomarang[i].rotationDegrade = true;
    }
}else{
    if(boomarang[i].rotationAcc >0){
        boomarang[i].rotationAcc = -rotationSteps;
        }
}
boomarang[i].rotationSpeed += boomarang[i].rotationAcc;
boomarang[i].x += (Math.cos((Math.PI*(boomarang[i].angle-90))/180) * boomarang[i].varSpeed);
boomarang[i].y += (Math.sin((Math.PI*(boomarang[i].angle-90))/180) * boomarang[i].varSpeed);

 

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...