Jump to content

problem with space shooter lab example


Recommended Posts

in this lab example there is something like snap to 45,135, -45, -135 angles. if you active debug mode and try to move to any direction but the ones i said you will notice it. like in the attach i'm aiming at bottom but i'm moving to 45. and it's stuck at 45.
How can i make it to move to the angle i'm aiming for? like this (tried to change velocityFromRotation to velocityFromAngle but nothing changed)

sorry I'm new and trying my best to explain it.

7-31-2018 9-17-40 PM.jpg

tried to change it 

  if (cursors.up.isDown)
    {
        this.physics.velocityFromRotation(ship.rotation, 600, ship.body.acceleration);
    }

 

Link to post
Share on other sites

Try this:

    if (cursors.up.isDown)
    {
        //this.physics.velocityFromRotation(ship.rotation, 600, ship.body.acceleration);
        ship.setVelocity(Math.cos(ship.rotation) * 600, Math.sin(ship.rotation) * 600);  // 1
    }
    else
    {
        ship.setAcceleration(0);
        ship.setVelocity(0,0);   // 2
    }

In physics, you use acceleration to calculate new velocity and velocity to get new position.

If you are driving car and accelerating for some time, your velocity increases. When you yake the foot out of the gas, your acceleration is 0, it does not increase velocity and your velocity is constant (unless there is inplemented something like friction or drag, which will slow you down).

 What you need is immediate change in velocity - it is commented as // 1.

 You can also add line // 2  to immediately stop ship if not holding up key... but it looks ugly (it is like you not only put foot out of gas, but stomped on break in the same time)

Alternatively,  you can change original line to this:

this.physics.velocityFromRotation(ship.rotation, 600, ship.body.velocity);

 

 

Link to post
Share on other sites

Thanks for explanation.

but the problem is when using

this.physics.velocityFromRotation(ship.rotation, 600, ship.body.acceleration);

with second argument you could set  "how long takes to reach the max speed" and setMaxVelocity was the max speed.

 

this.physics.velocityFromRotation(ship.rotation, 600, ship.body.velocity);

but when using this, the second argument is the max speed!!

 

Link to post
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...
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...