Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7509843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:02:51+00:00 2026-05-29T23:02:51+00:00

I am creating a mini space ship game with a feel similar to escape

  • 0

I am creating a mini space ship game with a feel similar to escape velocity. I cannot seem to get inertia to work correctly.
I am also having problems with the turning functions. If I tap the left or right arrow keys then it does not follow anything similar to the circular type movement that should be happening.

$(function() {
var canvas = Raphael('game', 1000, 800);
var background = canvas.rect(0, 0, 1000, 800);
background.attr("fill", "black");

var ship = canvas.rect(200, 200, 10, 35);
ship.attr("fill", "red");
ship.angle = 0;
ship.turnrate = 4;
ship.maxSpeed = 2;
ship.acc = 0;
ship.accSpeed = 0.25;
ship.vel = [0,0];

var up = 0;
var left = 0;
var right = 0;
var speedx = 0;
var speedy = 0;

function moveShip() {
    if (left == 1) {
        ship.angle = (ship.angle - ship.turnrate) % 360;
    }

    if (right == 1) {
        ship.angle = (ship.angle + ship.turnrate) % 360;
    }

    if (up == 1) {
        if (ship.acc < ship.maxSpeed) {
            ship.acc += ship.accSpeed;
        }

        if (ship.acc > ship.maxSpeed) {
            ship.acc = ship.speed;
        }
    }

    if (up == 0) {
        if (ship.acc > 0) {
            ship.acc -= ship.accSpeed;
        }

        if (ship.acc < 0) {
            ship.acc = 0;
        }
    }

    speedx = ship.vel[0] + ship.acc * Math.sin(ship.angle);
    speedy = ship.vel[1] + ship.acc * Math.cos(ship.angle);

    ship.vel = [speedx, speedy];

    ship.transform("");
    ship.rotate(ship.angle);
    ship.attr({x: ship.vel[0], y: ship.vel[1]});

    $("#stats").text("ship.angle: " + ship.angle
            + " vel[0]: " + ship.vel[0] + " vel[1]: " + ship.vel[1]
    + " ship.speed: " + ship.maxSpeed + " speedx: " + speedx + " speedy: " + speedy);
}

function keyPressed(evt) {
    if (evt.keyCode == 38) {
        up = 1;
    }

    if (evt.keyCode == 37) {
        left = 1;
    }

    if (evt.keyCode == 39) {
        right = 1;
    }
}

function keyReleased(evt) {
    if (evt.keyCode == 38) {
        up = 0;
    }

    if (evt.keyCode == 37) {
        left = 0;
    }

    if (evt.keyCode == 39) {
        right = 0;
    }
}

function gameloop() {
    moveShip();
}

$(document).keydown(keyPressed);
$(document).keyup(keyReleased);
setInterval(gameloop, 30);
});

I have been searching stack overflow and the internet, but most questions out there are to do with the classic Space Invaders type game which does not involve turning or inertia.

Any help would be appreciated, I really just want to have a good understanding of what I am missing.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T23:02:52+00:00Added an answer on May 29, 2026 at 11:02 pm

    Are you sure you want to change the acceleration of a ship with the arrow keys? Remember, acceleration is the rate of change of the velocity. This means, if you have a constant acceleration then you velocity will start to grow and grow and grow. Pretty soon you will have a ship that is going way too fast.

    The results from the chatroom, here for posterity:

    $(function() {
        var canvas = Raphael('game', 1000, 800);
        var background = canvas.rect(0, 0, 1000, 800);
        background.attr("fill", "black");
    
        var ship = canvas.rect(200, 200, 10, 35);
        ship.attr("fill", "red");
        ship.angle = 0;
        ship.turnrate = 4;
        ship.maxSpeed = 0.25;
        ship.acc = 0.25;
        ship.vel = [0,0];
        ship.pos = [500,400];
    
        var up = 0;
        var left = 0;
        var right = 0;
        var speedx = 0;
        var speedy = 0;
    
        function moveShip() {
            if (left == 1) {
                ship.angle = (ship.angle - ship.turnrate) % 360;
            }
    
            if (right == 1) {
                ship.angle = (ship.angle + ship.turnrate) % 360;
            }
    
            if (up == 1) {
                speedx = ship.vel[0] + ship.acc * Math.sin(ship.angle * Math.PI / 180);
                speedy = ship.vel[1] - ship.acc * Math.cos(ship.angle * Math.PI / 180);
    
                ship.vel = [speedx, speedy];
            }
            ship.pos = [ship.pos[0] + speedx, ship.pos[1] + speedy];
    
            ship.transform("");
            ship.rotate(ship.angle);
            ship.attr({x: ship.pos[0], y: ship.pos[1]});
    
            $("#stats").text("ship.angle: " + ship.angle
                    + " vel[0]: " + ship.vel[0] + " vel[1]: " + ship.vel[1]
            + " ship.speed: " + ship.maxSpeed + " speedx: " + speedx + " speedy: " + speedy);
        }
    
        function keyPressed(evt) {
            if (evt.keyCode == 38) {
                up = 1;
            }
    
            if (evt.keyCode == 37) {
                left = 1;
            }
    
            if (evt.keyCode == 39) {
                right = 1;
            }
        }
    
        function keyReleased(evt) {
            if (evt.keyCode == 38) {
                up = 0;
            }
    
            if (evt.keyCode == 37) {
                left = 0;
            }
    
            if (evt.keyCode == 39) {
                right = 0;
            }
        }
    
        function gameloop() {
            moveShip();
        }
    
        $(document).keydown(keyPressed);
        $(document).keyup(keyReleased);
        setInterval(gameloop, 30);
    });​
    

    There were two bugs fixed. A pos attribute was added, so that velocity and position can be updated separately. Also, Math.sin and Math.cos take angles in radians, so we converted the angle for those functions.

    You can play with the results: http://jsfiddle.net/mJcN7/8/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Creating a simple RPG game, first time using XNA. Trying to get my character
I'm creating a mini project that reads a JSON file from Flickr and get's
Long time ago, I was creating a mini ORM using reflection. While reading about
I am creating a website as a mini project which will be used to
I am currently creating a frontend editor for wordpress using TDO Mini Forms and
I'm trying to use the mvc-mini-profiler with EFCodeFirst I'm creating a DbProfiledConnection and passing
I'm trying to use code similar to clrdump to create mini dumps in my
I'm after creating a simple mini-language parser in Python, programming close to the problem
I am creating a wap portal, and wanted to know if Opera Mini supports
Creating a mini-database with access, i came across this problem: For the background, i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.