Jump to content


Photo

smooth angle turning


  • Please log in to reply
10 replies to this topic

#1 darkkyoun

darkkyoun

    GMC Member

  • New Member
  • 44 posts
  • Version:GM8

Posted 18 August 2012 - 11:27 AM

Hi ,

As the topic suggest, I am trying to make my object have a smooth angle turning.

Posted Image

With the object image_angle change accordingly from x1 to x2.


My current code:

    if going_left=3  // when in this state
        if x <=375
        {
            speed=0;
            vspeed=2;
            image_angle = point_direction(sticky_obj.x,sticky_obj.y,x,y-10);  // this is the one that is helping my object image angle turn but it is not smooth.
            going_left=4;
        }



So my question is , is there a better way to code it?
  • 0

#2 Koaske

Koaske

    GMC Member

  • GMC Member
  • 383 posts
  • Version:GM8

Posted 18 August 2012 - 12:05 PM

Didn't I reply to your topic concerning movement in this way a month ago? :biggrin:

To answer whether there is or isn't a better way, you have to give a bit more information: Is this the only situation where you want to use this or are there other shapes that the character must move around? I assume you're using this for an enemy AI, is this right?

If you provide some more information about the situation(s) you're going to use the code in, I'd be glad to help you out.
  • 0

#3 darkkyoun

darkkyoun

    GMC Member

  • New Member
  • 44 posts
  • Version:GM8

Posted 18 August 2012 - 01:53 PM

Didn't I reply to your topic concerning movement in this way a month ago? :biggrin:

To answer whether there is or isn't a better way, you have to give a bit more information: Is this the only situation where you want to use this or are there other shapes that the character must move around? I assume you're using this for an enemy AI, is this right?

If you provide some more information about the situation(s) you're going to use the code in, I'd be glad to help you out.


Did u ? sorry I think I forgot to set the necessary settings. Anyway, that is the only shape the object is moving. it will have to loop round that thing like multiple times unless I click on it.

Not really an enemy AI, more like cargo box that have to go round a magnetic conveyor belt. It is more into physic.
  • 0

#4 Koaske

Koaske

    GMC Member

  • GMC Member
  • 383 posts
  • Version:GM8

Posted 19 August 2012 - 12:16 AM

Okay. In that case, your current method is suited for the job. I don't know where the origin of the belt is, so I can't tell if (x, y-10) is at the center of the circular end. I would use
point_direction(stickyObj.x, stickyObj.y, endCircleCenterX, endCircleCenterY)

In theory, you could optimise it a bit further, but there's really no need, since what you'd gain wouldn't be worth the effort. Assuming the end is a circle, the speed at which the image_angle changes remains a constant. In other words, the image_angle gets changed the same amount each step, so there's
need to calculate point_direction over and over again in each step.

I decided to make you an example in any case. It's up to you if you want to use it or not, but that's how I would make what you described. Note that you can also use lengthdir_x and lengthdir_y and degrees instead of cos, -sin and radians.

Conveyor Belt Example

Edited by Koaske, 19 August 2012 - 12:17 AM.

  • 0

#5 darkkyoun

darkkyoun

    GMC Member

  • New Member
  • 44 posts
  • Version:GM8

Posted 19 August 2012 - 01:12 AM

Okay. In that case, your current method is suited for the job. I don't know where the origin of the belt is, so I can't tell if (x, y-10) is at the center of the circular end. I would use

point_direction(stickyObj.x, stickyObj.y, endCircleCenterX, endCircleCenterY)

In theory, you could optimise it a bit further, but there's really no need, since what you'd gain wouldn't be worth the effort. Assuming the end is a circle, the speed at which the image_angle changes remains a constant. In other words, the image_angle gets changed the same amount each step, so there's
need to calculate point_direction over and over again in each step.

I decided to make you an example in any case. It's up to you if you want to use it or not, but that's how I would make what you described. Note that you can also use lengthdir_x and lengthdir_y and degrees instead of cos, -sin and radians.

Conveyor Belt Example


Thanks for replying, the origin for the belt is x:125 , y:32. which mean means , it is in the center of rectangular instead of the circle. what do you mean endCircleCenterX, endCircleCenterY ?
Your example is great, really smooth turning where all angles is being calculated. But it is in the draw event while mine is at the step event. Let me explain.

redcargo_obj
step_event:
    if place_meeting(x,y,sticky_obj)    // if the cargo collide with sticky_obj, this means it drops from somewhere
        {
            going_left=3;    // this just change the state to 3 if the above is true
            vspeed=0;    //stop the bags from falling
            hspeed=-3;  // starting moving towards the left which is the start moving towards the edge of the belt
            speed=3;
            image_index=0;
            image_angle = 0;
        }
    if going_left=3  // when in this state
        if x <=375  // when this coordinates is met 
        {
            speed=0;
            vspeed=2;
            image_angle = point_direction(x,y+1,sticky_obj.x,sticky_obj.y+1); // i tweet it last night to make it look more realistic, but it is not working 
            going_left=4;
        }

however it will not loop from x1 to x2. it will go round the belt multiple times until i press to release it.

Edited by darkkyoun, 19 August 2012 - 01:26 AM.

  • 0

#6 Koaske

Koaske

    GMC Member

  • GMC Member
  • 383 posts
  • Version:GM8

Posted 19 August 2012 - 10:34 AM

My example was only a guideline, not a full implementation. I guess I'll explain it a bit further.

I figured that the crate in your game goes around the whole conveyor belt, but I only made the ending part, since that was what you were having trouble with. My code being in draw event is not a problem. You can put the same code in step event too, and you don't need the draw functions, since your crate is an actual object, not just a sprite.

If you don't want to fit my example in your code, then probably the easiest (but not the most efficient, but like I said, it doesn't really matter) solution would be to do like I said:
point_direction(stickyObj.x, stickyObj.y, endCircleCenterX, endCircleCenterY)

Let me explain those two new variables for you. Your conveyor belt consists of 3 basic geometric shapes, doesn't it? You have two circles and a rectangle that connects to both of these circles. To make the turning smooth, you can't use the current origin of your sprite, but the center point of the circle you're going around at the moment. If you're circling the left circle, you must set (endCircleCenterX, endCircleCenterY) to the center point of the left circle, and when circling the right, you'll use the coordinates of the right circle's center. This way the direction will always be correct.

To see what I mean, check where I put the origin in my conveyor sprite: it's at the center point of the purple circle.
  • 0

#7 darkkyoun

darkkyoun

    GMC Member

  • New Member
  • 44 posts
  • Version:GM8

Posted 20 August 2012 - 06:36 AM

My example was only a guideline, not a full implementation. I guess I'll explain it a bit further.

I figured that the crate in your game goes around the whole conveyor belt, but I only made the ending part, since that was what you were having trouble with. My code being in draw event is not a problem. You can put the same code in step event too, and you don't need the draw functions, since your crate is an actual object, not just a sprite.

If you don't want to fit my example in your code, then probably the easiest (but not the most efficient, but like I said, it doesn't really matter) solution would be to do like I said:

point_direction(stickyObj.x, stickyObj.y, endCircleCenterX, endCircleCenterY)

Let me explain those two new variables for you. Your conveyor belt consists of 3 basic geometric shapes, doesn't it? You have two circles and a rectangle that connects to both of these circles. To make the turning smooth, you can't use the current origin of your sprite, but the center point of the circle you're going around at the moment. If you're circling the left circle, you must set (endCircleCenterX, endCircleCenterY) to the center point of the left circle, and when circling the right, you'll use the coordinates of the right circle's center. This way the direction will always be correct.

To see what I mean, check where I put the origin in my conveyor sprite: it's at the center point of the purple circle.


ok, you were right. The point direction thing is not smooth. But I did the origin as the conveyor example and put the code
i = pi/2   //create event
iDir = 1  //create event
i += iDir*(pi/96)  //step event

I did not put the if statement as I want it to loop round the conveyor belt. for example:

if place_meeting(x,y,sticky_obj)    // if the cargo collide with sticky_obj, this means it drops from somewhere
        {
            going_left=3;    // this just change the state to 3 if the above is true
            vspeed=0;    //stop the bags from falling
            hspeed=-3;  // starting moving towards the left which is the start moving towards the edge of the belt
            speed=3;
            image_index=0;
            image_angle = 0;
        }
    if going_left=3  // when in this state
        if x <=375  // when this coordinates is met 
        {
            speed=0;
            vspeed=2;
            i += iDir*(pi/96)   // suppose to make it turn smoothly, but it did not work out as plan
            going_left=4;
        }

This is the file http://www.mediafire...24i0fu6hurr41xn . where did i go wrong?

Edited by darkkyoun, 20 August 2012 - 11:46 PM.

  • 0

#8 Koaske

Koaske

    GMC Member

  • GMC Member
  • 383 posts
  • Version:GM8

Posted 21 August 2012 - 05:06 PM

Since it would take a long time to figure out all of the necessary code in your file, I just decided to upgrade my example: Conveyor Belt Example

What I did (I think you've done this too) is split the moving around in 4 phases, going left, curving down, going right and curving up. In the curve phases, I use the trigonometric method, where I move the box along the border of a half-circle. I hope you know some trigonometry, because otherwise it will be hard to understand what's happening in the code. If you need me to explain something, just ask.
  • 0

#9 darkkyoun

darkkyoun

    GMC Member

  • New Member
  • 44 posts
  • Version:GM8

Posted 22 August 2012 - 12:47 PM

Since it would take a long time to figure out all of the necessary code in your file, I just decided to upgrade my example: Conveyor Belt Example

What I did (I think you've done this too) is split the moving around in 4 phases, going left, curving down, going right and curving up. In the curve phases, I use the trigonometric method, where I move the box along the border of a half-circle. I hope you know some trigonometry, because otherwise it will be hard to understand what's happening in the code. If you need me to explain something, just ask.



That was the perfect example, however I forgot to tell you that my game have gravity set, and hspeed and vspeed. I try to change the hspeed into x +=3, tweak the values time and again but nothing seems to work.

Let me explain. The code below shows what will happen when obj_redcargo land and exit obj_leftdirection(which is another conveyor belt)

{
    if place_meeting(x,y,obj_leftdirection)    // if box collide with leftdirection object
        {
            going_left=1;
            vspeed=0;    //stop the box from falling
            hspeed=-3;   //this is to make the box 'fly' to another platform
        }
    if going_left=1
        if place_free(x,y)
        {
            hspeed=0; //stop the cargo from moving forward when it exit the conveyor
            vspeed=3;
        }
}

After exiting obj_leftdirection, it will then land on the sticky conveyor and this when the problem occur. The code can be found in in this file http://www.mediafire...24i0fu6hurr41xn under scripts by the name gavi and redbag_collide_leftbelt
Really not sure if it is my code that is affecting your code, or I am forgetting missing, really need your advice.

Edited by darkkyoun, 22 August 2012 - 12:59 PM.

  • 0

#10 Koaske

Koaske

    GMC Member

  • GMC Member
  • 383 posts
  • Version:GM8

Posted 22 August 2012 - 04:26 PM

Sorry, but there's really too much code and stuff in the file to do a thorough troubleshoot. I would need a lot of time to pinpoint the problem.

Try doing these:
*Disable the collision check for the box object when the conveyor belt is touched for the first time.
*Set gravity to 0 when the belt is met.
*Set hspeed and vspeed to 0 when reaching the curve phase. You can use hspeed when the box is moving straight to left or right.
  • 0

#11 darkkyoun

darkkyoun

    GMC Member

  • New Member
  • 44 posts
  • Version:GM8

Posted 23 August 2012 - 06:16 AM

Sorry, but there's really too much code and stuff in the file to do a thorough troubleshoot. I would need a lot of time to pinpoint the problem.

Try doing these:
*Disable the collision check for the box object when the conveyor belt is touched for the first time.
*Set gravity to 0 when the belt is met.
*Set hspeed and vspeed to 0 when reaching the curve phase. You can use hspeed when the box is moving straight to left or right.


No worries =D you have been a great help =D I could use the example you gave me and remodel it into another game.
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users