1

I'm having trouble with my game movement. I have either incorrectly used sin and cos to calculate the true X,Y to move the screen (character stays centered) or the calculation takes too long and doesn't refresh fast enough. The below code is implemented in a game loop that uses window.requestAnimationFrame to continue looping.

Sometimes it moves in the right direction and sometimes it moves in the opposite of what I would expect, but this behavior is not consistent.

My angles range in both positive and negative (0-360) depending if the avatar is turning clockwise or counter (this part of the code registers correctly). I have tested battleField object set methods to make sure they correctly set the top and left. I have tested battleField object methods to make sure it returns the correct top and left values (in integer, no unit indicator like 'px');

Any thoughts or suggestions are much appreciated.

let battleField = {
    id: 'battleField',
  speed: 5,
  get speedTop() {
    let rotateNum = parseInt(rotateAngle,10);
    let radians=rotateNum*Math.PI/180
    let trueTop=userAvatar.speed*(Math.sin(radians))
    return trueTop;
  },
  get speedLeft() {
    let rotateNum = parseInt(rotateAngle,10);
    let radians=rotateNum*Math.PI/180
    let trueLeft=userAvatar.speed*(Math.cos(radians))
    return trueLeft
  },
  get element() {
    let elm = document.getElementById(this.id);
    return elm;
  },
  get bottom() {
    let elmBottom = parseInt(window.getComputedStyle(this.element).bottom, 10);
    return elmBottom;
  },
  get top() {
    let elmTop = parseInt(window.getComputedStyle(this.element).top, 10);
    return elmTop;
  },
  get left() {
    let elmLeft = parseInt(window.getComputedStyle(this.element).left, 10);
    return elmLeft;
  },
  get right() {
    let elmRight = this.left + this.width;
    return elmRight;
  },
  get width() {
    let elmWidth = parseInt(window.getComputedStyle(this.element).width, 10);
    return elmWidth;
  },
  get height() {
    let elmHeight = parseInt(window.getComputedStyle(this.element).height, 10);
    return elmHeight;
  },
  set setBottom(value) {
    this.element.style.bottom = value + 'px';
  },
  set setLeft(value) {
    this.element.style.left = value + 'px';
  },
  set setRight(value) {
    this.element.style.left = (value - 50) + 'px';
  },
  set setTop(value) {
    this.element.style.top = (value) + 'px';
  },
  get maxRight() {
    if(this.left<screenArea.width-this.width){
        return true;
    }
  },
  get maxLeft() {
    if(this.left>=0){
        return true;
    }
  },
  get maxTop() {
    if(this.top>=0){
        return true;
    }
  },
  get maxBottom() {
    if(this.top<screenArea.height-this.height){
        return true;
    }
  }
}
let userAvatar = {
  speed:5
}
let rotateAngle="15deg";

function loop(){
let rotateNum = parseInt(rotateAngle,10);
let radians=rotateNum*Math.PI/180
let newTop= battleField.top + (Math.sin(radians) * userAvatar.speed);
let newLeft= battleField.left - (Math.cos(radians) * userAvatar.speed);
battleField.setTop = newTop;
battleField.setLeft = newLeft;
window.requestAnimationFrame(loop);
}
window.requestAnimationFrame(loop);
sychordCoder
  • 230
  • 3
  • 14
  • Should `battleField.setTop = newTop;` instead be `battleField.setTop(newTop);`, and same for `setLeft`? Just guessing. – Scott Sauyet Feb 25 '21 at 20:52
  • 1
    hi scott, they are setup as set methods so it doesn't pass arguments. battleField={set setTop(value) { this.element.style.top = (value) + 'px'; }} and yes I have a simple method for getting the element using document.getElementById. Should it not be set as a set method? I could make it a normal method with an argument but I'm not sure that would help. – sychordCoder Feb 25 '21 at 20:56
  • But does this update the `battlefield.top` property used to calculate `newTop`? Again, I don't see enough here to offer a real answer, just guesses. – Scott Sauyet Feb 25 '21 at 20:59
  • I added the entire battlefield object so you can take a look. I appreciate the attention! You can ignore get speedTop and get speedLeft, this was an experiment to have it calculated within the object. – sychordCoder Feb 25 '21 at 21:04
  • I'm afraid I don't see anything specific. But I also don't really understand the overall structure. I don't know what's supposed to be moving, this item called `battlefield`? That would be strange. I don't know how `this.element` is set. But my biggest concern is that this is mixing together the logical model of positions and vectors with the display logic. That always makes code seem convoluted to me. – Scott Sauyet Feb 25 '21 at 21:51
  • I added the loop so you can see what I have in the game loop. There is a virtual joystick divided into 15 degree segments. When you click on one it assigns rotateAngle with the correct angle. – sychordCoder Feb 25 '21 at 21:56

0 Answers0