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.

Gunderak 11-17-2011 07:47 AM

Never mind, it seems to be working on.
it was a problem when turning left with the angle going minus that was stuffing it.
I just set the start angle and it works fine now.
Now all I need to do is the characters head and im done.

Gunderak 11-17-2011 09:22 AM

I now have the cars rotation, player head rotation and now reversing.
Any idea's one how I could make something so if it hit a wall it would bounce back?
Well I know the bounce back bit.
In my script I have on a timeout if the speed is less than zero that it will slowly stop..
But how would the wall checks work for something like this?
the most complex part I know is if(onwall(x, y)){
Pretty sad eh?
To clarify it up for anyone that doesn't understand.
I am wondering how I could make something caculate if there is a wall I front of you and bounce you back.
How would you get all the on wall checks to work ^_~

fowlplay4 11-17-2011 04:24 PM

Use the same math you're using to move the car to check if that x, y position is on a wall. If it is then 'bounce back'.

Tricxta 11-17-2011 09:49 PM

You can invert the angle easily by doing

invertedangle=(angle+pi)%(pi*2)

as for the power, I would subtract a little from the initial collision. Then use the same direction formula you have already been given.

Gunderak 11-17-2011 11:12 PM

Thanks for the help, il get started on the collision checking when I get home.

cbk1994 11-17-2011 11:18 PM

Quote:

Originally Posted by Tricxta (Post 1674488)
You can invert the angle easily by doing

invertedangle=(angle+pi)%(pi*2)

as for the power, I would subtract a little from the initial collision. Then use the same direction formula you have already been given.

All you need to do is add pi to it, the sin and cos functions don't require it to be in [0, 2pi).

Tricxta 11-18-2011 08:06 AM

Quote:

Originally Posted by cbk1994 (Post 1674502)
All you need to do is add pi to it, the sin and cos functions don't require it to be in [0, 2pi).

I just like to keep things clean. Still,thanks for the info.

Gunderak 11-18-2011 08:54 AM

I know how to detect if there on a wall, but how would I handle it?
I have tried reversing the players movement which is this.
PHP Code:

    player.-= sin(this.angle) * this.speed;
    
player.-= cos(this.angle) * this.speed

But that doesn't work.
I think I'm in way over my head.

cbk1994 11-19-2011 03:32 AM

Quote:

Originally Posted by Tricxta (Post 1674538)
I just like to keep things clean. Still,thanks for the info.

You're not keeping it clean, you're just overcomplicating it and adding more work for the engine. It takes longer for a scripter to read and understand your code than it does mine.

Tricxta 11-19-2011 06:24 AM

Quote:

Originally Posted by Gunderak (Post 1674540)
I know how to detect if there on a wall, but how would I handle it?
I have tried reversing the players movement which is this.
PHP Code:

    player.-= sin(this.angle) * this.speed;
    
player.-= cos(this.angle) * this.speed

But that doesn't work.
I think I'm in way over my head.

That won't reverse the direction in which the player moves, instead do what chris said and just add pi to the existing angle.

Quote:

Originally Posted by cbk1994 (Post 1674607)
You're not keeping it clean, you're just overcomplicating it and adding more work for the engine. It takes longer for a scripter to read and understand your code than it does mine.

I meant variable wise, you're going to keep on adding onto your angle which will eventually become quite a large number if used for a while. Also while you do have a valid point I'm sure one extra mod and multiplication operator doesn't effect performance to the point where you'd notice on any machine. What if I wanted to see what was going on in the script and I had some huge messy number as the angle, it'd be pretty ugly to work with. Also by including mod and multiplication the angle can work with other formulas if you choose.

Also for those who don't know % is the mod operator. It works by spitting out the remainder of 2 given numbers. Eg:6%4=2, 6%5=1, 11%10=1

Gunderak 11-19-2011 06:36 AM

Update: I now have collision detection and it works.
Next up: When you fire the weapon it should put you in the car, when you press A it should place a NPC of the car on the map and allow you to press A next to it to enter.
@Everyone Thanks for all your help


All times are GMT +2. The time now is 11:05 AM.

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