Jump to content

Changing Origin Angle of Sprite from Left to Top


jjwallace
 Share

Recommended Posts

Hello, I am building an Oceanic game and need to utilize the water surface.  I am swimming fish through the sea however at angle '0' I want to fish to be pointing up and not left.

I do this by creating the angle 90 degrees:

this.xSpeed = Math.cos((this.fishBody.angle+90)/180*Math.PI) * -this.pSpeed;
        this.ySpeed = Math.sin((this.fishBody.angle+90)/180*Math.PI) * - this.pSpeed;

However, I run into problems down the road with AI

Fish.prototype.avoid = function(fishBodx, FishBodY, xsp,ysp){
    var targetAngle = this.game.math.angleBetween(
         xsp, ysp, fishBodx, FishBodY
    );

    if (this.fishBody.rotation !== targetAngle) {
        var delta = (targetAngle) - (this.fishBody.rotation);

        this.TURN_RATE = 3;
        // Keep it in range from -180 to 180 to make the most efficient turns.
        if (delta > Math.PI) delta -= Math.PI * 2;
        if (delta < -Math.PI) delta += Math.PI * 2;

        if (delta > 0) {
            // Turn clockwise
            this.fishBody.angle += this.TURN_RATE;
        } else {
            // Turn counter-clockwise
            this.fishBody.angle -= this.TURN_RATE;
        }

        // Just set angle to target angle if they are close
        if (Math.abs(delta) < this.game.math.degToRad(this.TURN_RATE)) {
            this.fishBody.rotation = targetAngle;
        }
    }
}

What is a good solution to this?

Link to comment
Share on other sites

One possibility (I might be terribly off though, since the entire answer is speculative):

I have made an example as an attempt to describe your problem.

https://jsfiddle.net/samid737/m99avfrb/ 

I made a fish and a surface above it, and the fish can move freely under water I assumed.Then I assumed that your fish executing your "avoid" function all the time to simulate surfacing to the water. In my case, the fish is pointing perfectly upwards (perpendicular at least, thats what matters) at 0 degree angle against the surface as you can see .

By the way this was an exercise for me as a first step towards understanding the use of prototype functions, so I implemented my own functions for a Fish and enemyFish (you probably have more fish in the game, thats why your prototype function exists?). I tried to understand your post, but its hard to give a meaningful answer if we don't get enough information to provide a meaningful answer. Can you provide more details?

Another possible solution:

I don't know which physics system you are using, but you could try to invert the length and width of the physics body of your fish (if its a rectangle though, but you can always customize your physical body) to match the "0" degree angle you mentioned:

https://phaser.io/docs/2.3.0/Phaser.Physics.Arcade.Body.html#setSize for arcade

http://phaser.io/docs/2.4.3/Phaser.Physics.P2.Body.html#setRectangle for p2 physics

 

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