Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting
FAQ Members List Calendar Today's Posts

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 11-15-2011, 12:30 PM
Gunderak Gunderak is offline
Coder
Gunderak's Avatar
Join Date: Jun 2011
Location: Australia
Posts: 795
Gunderak is on a distinguished road
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.
__________________

Gund for president.

Remote PM {P*}x (Graal813044) from eraiphone -> Stefan: I hav 1 qustion
*Gunderak: he hav 1
*Gunderak: qustion

Last edited by Gunderak; 11-15-2011 at 01:08 PM..
Reply With Quote
  #2  
Old 11-15-2011, 02:36 PM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
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.
__________________
Reply With Quote
  #3  
Old 11-15-2011, 11:17 PM
Gunderak Gunderak is offline
Coder
Gunderak's Avatar
Join Date: Jun 2011
Location: Australia
Posts: 795
Gunderak is on a distinguished road
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?
__________________

Gund for president.

Remote PM {P*}x (Graal813044) from eraiphone -> Stefan: I hav 1 qustion
*Gunderak: he hav 1
*Gunderak: qustion
Reply With Quote
  #4  
Old 11-16-2011, 09:47 AM
Gunderak Gunderak is offline
Coder
Gunderak's Avatar
Join Date: Jun 2011
Location: Australia
Posts: 795
Gunderak is on a distinguished road
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.
__________________

Gund for president.

Remote PM {P*}x (Graal813044) from eraiphone -> Stefan: I hav 1 qustion
*Gunderak: he hav 1
*Gunderak: qustion
Reply With Quote
  #5  
Old 11-16-2011, 10:23 AM
Tricxta Tricxta is offline
The Muffin Man
Tricxta's Avatar
Join Date: Oct 2010
Location: Australia
Posts: 563
Tricxta is just really niceTricxta is just really nice
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
Reply With Quote
  #6  
Old 11-16-2011, 02:02 PM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
Quote:
Originally Posted by Tricxta View Post
(int((angle+(pi*.25))/(pi*.5))+3)%4
otherwise getdir(-sin(angle), -cos(angle)) should work.
__________________
Reply With Quote
  #7  
Old 11-16-2011, 11:46 AM
Gunderak Gunderak is offline
Coder
Gunderak's Avatar
Join Date: Jun 2011
Location: Australia
Posts: 795
Gunderak is on a distinguished road
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..
__________________

Gund for president.

Remote PM {P*}x (Graal813044) from eraiphone -> Stefan: I hav 1 qustion
*Gunderak: he hav 1
*Gunderak: qustion
Reply With Quote
  #8  
Old 11-16-2011, 02:36 PM
Gunderak Gunderak is offline
Coder
Gunderak's Avatar
Join Date: Jun 2011
Location: Australia
Posts: 795
Gunderak is on a distinguished road
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.
__________________

Gund for president.

Remote PM {P*}x (Graal813044) from eraiphone -> Stefan: I hav 1 qustion
*Gunderak: he hav 1
*Gunderak: qustion
Reply With Quote
  #9  
Old 11-16-2011, 02:52 PM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
Quote:
Originally Posted by Gunderak View Post
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.
__________________
Reply With Quote
  #10  
Old 11-16-2011, 11:12 PM
Gunderak Gunderak is offline
Coder
Gunderak's Avatar
Join Date: Jun 2011
Location: Australia
Posts: 795
Gunderak is on a distinguished road
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 
__________________

Gund for president.

Remote PM {P*}x (Graal813044) from eraiphone -> Stefan: I hav 1 qustion
*Gunderak: he hav 1
*Gunderak: qustion
Reply With Quote
  #11  
Old 11-16-2011, 11:51 PM
callimuc callimuc is offline
callimuc's Avatar
Join Date: Nov 2010
Location: Germany
Posts: 1,015
callimuc is a splendid one to beholdcallimuc is a splendid one to beholdcallimuc is a splendid one to beholdcallimuc is a splendid one to beholdcallimuc is a splendid one to behold
This might help you
http://forums.graalonline.com/forums...highlight=gani
__________________
MEEP!
Reply With Quote
  #12  
Old 11-17-2011, 12:26 AM
Crow Crow is offline
ǝɔɐɹq ʎןɹnɔ
Crow's Avatar
Join Date: Dec 2006
Location: Germany
Posts: 5,153
Crow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond repute
Quote:
Originally Posted by callimuc View Post
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.
Reply With Quote
  #13  
Old 11-17-2011, 12:30 AM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
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.
__________________
Reply With Quote
  #14  
Old 11-17-2011, 06:51 AM
Gunderak Gunderak is offline
Coder
Gunderak's Avatar
Join Date: Jun 2011
Location: Australia
Posts: 795
Gunderak is on a distinguished road
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.
__________________

Gund for president.

Remote PM {P*}x (Graal813044) from eraiphone -> Stefan: I hav 1 qustion
*Gunderak: he hav 1
*Gunderak: qustion

Last edited by Gunderak; 11-17-2011 at 07:27 AM..
Reply With Quote
  #15  
Old 11-17-2011, 07:01 AM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
Quote:
Originally Posted by Gunderak View Post
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.
__________________
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 01:00 PM.


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