Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   Car Movements Help (https://forums.graalonline.com/forums/showthread.php?t=134265038)

Gunderak 11-15-2011 12:30 PM

Car Movements Help
 
I am working on a "Car" for Graal Ghetto and so far all I have is the movement.
For my example I have added a block image to show the "Car".
Although the movement is working perfectly the acceleration isn't.
The problem is when accelerating if you turn it stops accelerating.
Here is what I have so far.
I am entrusting that the Graal community will NOT steal this, as I have spent about an hour and a half just getting this much.
2330 Characters long and 87 lines.
Thanks.
PHP Code:

/*
Scripted by Gunderak.
Intended to be used on Graal Ghetto and Graal Ghetto ONLY.
You may however study this and try to learn from it.
*/
//#CLIENTSIDE
function onCreated() {
  
//Disables default movement.
  
disabledefmovement();
  
//Speed variables
  
this.speed "1";
  
this.leftrightspeed "0.13";
  
this.movespeed "0";
  
this.acceleration "0.10";
  
//Begins the timeout.
  
onTimeout();
}

function 
GraalControl.onKeyDown(code) {
   
//If the Right-arrow key is pressed.
  
if (code == 37) {
    
//And if the car is moving.
    //This makes car movements seem more realistic.
    
if (this.movespeed 0) {
      
//Raises the angle.
      
this.dir += this.leftrightspeed;
    }
  }
  
//If the left-arrow key is pressed.
  
if (code == 39) {
    
//And if the car is moving.
    //This makes car movements seem more realistic.
    
if (this.movespeed 0) {
      
//Lowers the angle.
      
this.dir -= this.leftrightspeed;
    }
  }
  
//If the up-arrow key is released.
  
if(code == "38"){
  
//Start moving the car and begin to accelerate.
  
this.moving 1;
  }
}

function 
GraalControl.onKeyUp(code){
  
//If the up-arrow key is released.
  
if(code == 38){
   
//Stop moving the car and begin to decelerate.
   
this.moving 0;
  }
}

function 
onTimeout() {
  
//Shows a block image.
  
showimg(3"block.png"player.xplayer.);
  
//Changes the blocks rotation to the correct angle.
  
findimg(3).rotation this.dir;
  
findimg(3).zoom 2;
  
//If the angle is less than the minimum resets it to zero.
  
if (this.dir "-6.25") {
    
this.dir 0;
  }
  
//If the angle is greater than the maximum resets it to zero.
  
if (this.dir "6.25") {
    
this.dir 0;
  }
  
//Just some basic Trigonometry.
  //This part has been removed.
  //If you really want to steal this script you will have to
  //figure the sine and cosine out yourself!
  //If the car isnt accelerating slowly decrease speed.
  
if (this.moving == 0) {
    
this.movespeed -= this.acceleration 3;
  } else {
    if (
this.movespeed this.speed) {
      
this.movespeed += this.acceleration;
    }
  }
  
//If your move speed is less than zero for some reason
  //It will set it back to zero.
  
if (this.movespeed 0) {
    
this.movespeed 0;
  }
  
//Continues to loop through the timeout.
  
settimer(0.05);


Update
Removed the players movement seeming as it isn't necessary.
Also to make it harder for people to steal this script and actually use it.

cbk1994 11-15-2011 02:36 PM

PHP Code:

  if (this.dir "6.25") {
    
this.dir 0;
  } 

Why is 6.25 in quotes? All you're doing is adding more work for the engine by forcing it to be coerced to a number before it can be compared. You're doing the same thing here (among other places):

PHP Code:

  //Speed variables
  
this.speed "1";
  
this.leftrightspeed "0.13";
  
this.movespeed "0";
  
this.acceleration "0.10"

I'm not really sure what you're trying to do with acceleration. I'm assuming you want the longer you hold down the up key, the faster it goes, so you need to accelerate while it's held down, not when it's first pushed.

PHP Code:

//#CLIENTSIDE
const MAX_SPEED 3;
const 
FRICTION 0.1;
const 
ACCELERATION 1.2;

function 
onCreated() {
  
disableDefMovement();
  
this.trigger("timeOut");
}

function 
onTimeOut() {
  
// show car (you should do this in a GANI attribute so you don't have
  // to have this timeout; it'll look smoother for other players too)
  
with (findImg(1)) {
    
image "block.png";
    
    
attachToOwner true;
    
rotation thiso.angle;
  }
  
  
// turn the car
  
if (keyDown(1)) {
    
// turn left
    
this.angle += 0.1;
  } else if (
keyDown(3)) {
    
// turn right
    
this.angle -= 0.1;
  }
  
  
// accelerate or deccelerate
  
if (keyDown(0)) {
    
// increase speed
    
this.speed min(MAX_SPEEDmax(0.1this.speed ACCELERATION));
  } else {
    
// decrease speed
    
this.speed *= (FRICTION);
  }
  
  
player.chat "speed=" this.speed;
  
  
// move the car
  
player.-= sin(this.angle) * this.speed;
  
player.-= cos(this.angle) * this.speed;
  
  
// reset the timeout
  
this.setTimer(0.05);


Something like this is all you need for a basic car. There's no need to complicate it.

And stop worrying about people stealing your scripts. It's not as big of a deal as you might think.

Gunderak 11-15-2011 11:17 PM

Thanks, I wasn't aware of the if(keydown(0)){
I have always wondered if there was something like that.
In flash it's if(key.is.down(KEY.UP)){
And regarding quotes, Im just use to using them.
Il remember in future you dont need them.
Would it be possible to have in a gani the showimg but have it something like this.
PHP Code:

SCRIPT
with
(findimg(1)){
  
image IMAGE;
  
attachtoowner true;
  
rotation thiso.angle;
}
SCRIPTEND 

And then define the car image to use at the top of the script?

Gunderak 11-16-2011 09:47 AM

On an related note, is there any way to show an image and change its rotation serverside without using a gani?
Using a gani if you lag it looks retarded.

Tricxta 11-16-2011 10:23 AM

use a weapon and a showimg statement with an index less the 200, while setting the player gani to only show the head. Should work relatively well :)

and I did something like this a while back so I'll show a player direction formula with you that I nutted out while falling asleep in my maths lecture:
(int((angle+(pi*.25))/(pi*.5))+3)%4

That should work logic wise, no guarantees it'll definately work though

Gunderak 11-16-2011 11:46 AM

Thanks, but I can't seem to get the rotation of the image to show serverside without putting it in a gani.
And putting it in a gani makes it laggy.
As for your equation thanks, it helps.
I never paid attention during any math lectures..
As I can understand it, its something like this.
Integer of the angle plus 3.14 times by 0.25 divided by 3.14 times by 0.5 + 3 % 4.
Im unsure of the %4 though.
I mean it's going to be percentage 4, but how does that make sense?
Maybe it's just the GS2 syntax of how that's set out that I don't understand..

cbk1994 11-16-2011 02:02 PM

Quote:

Originally Posted by Tricxta (Post 1674335)
(int((angle+(pi*.25))/(pi*.5))+3)%4

otherwise getdir(-sin(angle), -cos(angle)) should work.

Gunderak 11-16-2011 02:36 PM

Thanks..
But as I have said, is there any way of getting the images rotation to either show as a gani (SMOOTHLY) even when lagging, or make the findimg(index).rotation = this.angle serverside?
I have tried it in a gani but with the slightest lag it makes it jumpy.

cbk1994 11-16-2011 02:52 PM

Quote:

Originally Posted by Gunderak (Post 1674345)
Thanks..
But as I have said, is there any way of getting the images rotation to either show as a gani (SMOOTHLY) even when lagging, or make the findimg(index).rotation = this.angle serverside?
I have tried it in a gani but with the slightest lag it makes it jumpy.

Post some code. There's a lot of ways to do it with a GANI, and I don't know which you're using.

Gunderak 11-16-2011 11:12 PM

I was doing something like this.
In the main script every 0.05 second on a timeout I was making player.attr[4] the car image and player.attr[5] the angle. then in the gani every 0.05 seconds I was updating the images angle.
something like this.
PHP Code:

SCRIPT
function onPlayerEnters(){
onTimeout();
}
function 
onTimeout(){
  
with(findimg(1)){
    
attachtoowner true;
    
image player.attr[4];
    
rotation player.attr[5];
  }
 
settimer(0.05);
}
SCRIPTEND 


callimuc 11-16-2011 11:51 PM

This might help you
http://forums.graalonline.com/forums...highlight=gani

Crow 11-17-2011 12:26 AM

Quote:

Originally Posted by callimuc (Post 1674390)

To add to this: You could create a single direction gani with a set amount of frames, each having a different angle/rotation. Then you could just set a single gani frame instead of the whole one, like this:
PHP Code:

setAni("mygani[3]"nil); 

There are some niftier solutions, but I'd consider this one anyway.

cbk1994 11-17-2011 12:30 AM

What kind of lag are you seeing with your solution? It looks like it ought to work fine as long as you're putting it in a player attribute, not using it as the player's GANI.

Gunderak 11-17-2011 06:51 AM

When you turn left the angle is in the minus and it just gets jumpy and goes from say 36° to around 46° and it looks really bad.
I think I may have found a fix.
I have made the start angle extremely high, in the millions so that the player would have to turn left about 20,000 times to undo it.

cbk1994 11-17-2011 07:01 AM

Quote:

Originally Posted by Gunderak (Post 1674433)
Well when my ping hits around 200 - 400 (Which it almost always is at) it just gets jumpy and goes from say 36° to around 46° and it looks really bad.

You need to explain yourself better, it's very difficult to help you when I have to ask for clarification three times. Do you mean to say that you see the car "lagging" on your own screen? Or do other players see the choppy rotation?

If it's the latter, then there's not much you can do about it besides inter/extrapolation which is overkill at this point.

Post the code of how you're setting the GANI in the car script.


All times are GMT +2. The time now is 12:33 AM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.