make Object's rotation based on object position

Y

Yvalson

Guest
so I'm currently making an android game.
atm i'm using my mouse to make the object look at it (as it always moves forwards)
but this is isn't a really good solution.
what I'd like to make is an object in the botom left corner of the screen that resembles the rotation based on its position inside an area of the view is this possible, and if so could anyone give me leads on how to do it. I've seen lots of virtual joysticks but all of them have like 12 scripts full of code and I like my game to be as lightweight as possible

thanks in advance
 

DarthTenebris

Definitely not a Sith Lord
You want to make your object to constantly point at the mouse on android? Then try this:
Code:
image_angle = point_direction(device_mouse_x(0), device_mouse_y(0));
Put it in the step event.
Keep in mind that it will only work if you're holding down the screen, as x y coordinates don't auto update on android (and iOS) unless the screen is held down.

Hope I helped :)
 

Perseus

Not Medusa
Forum Staff
Moderator
I don't understand what you mean by rotation based on position. Could you please elaborate a bit more on that?

If you want an instance to rotate depending on the target instance's position with respect to the top-left corner of the view, you could use point_direction to calculate the direction and set it as its image_angle.

Code:
image_angle = point_direction(view_xview[0], view_yview[0], obj_player.x, obj_player.y);
 
Y

Yvalson

Guest
You want to make your object to constantly point at the mouse on android? Then try this:
Code:
image_angle = point_direction(device_mouse_x(0), device_mouse_y(0));
Put it in the step event.
Keep in mind that it will only work if you're holding down the screen, as x y coordinates don't auto update on android (and iOS) unless the screen is held down.

Hope I helped :)
I don't understand what you mean by rotation based on position. Could you please elaborate a bit more on that?

If you want an instance to rotate depending on the target instance's position with respect to the top-left corner of the view, you could use point_direction to calculate the direction and set it as its image_angle.

Code:
image_angle = point_direction(view_xview[0], view_yview[0], obj_player.x, obj_player.y);
thanks for the quick replies but the code you guys provided is what I have already but the inconvenient of that is that I have to drag my finger all around the screen (as my player is in the middle) to make him move the other way

what I want is a object in the botom left corner that acts like a sort of area of its own in which another object is that calculates where inside the area it currently is and based on that the player should rotate (I made some drawings to clarify)

example1 is what I want to have and example2 is what I currently have

important to note is that the player only rotates towards the mouse atm as the movement is done automatically
 

Attachments

JackTurbo

Member
So you want a virtual joystick?

Should be possible to do relatively simply I'd imagine.

You could create an object with a sprite for the background of the joystick, with the origin set to centred.

Then create another object for the top/moving part of the joystick, again centre the origin. Then on touch/click if the mouse/finger is coliding with the joystick background then update the x/y of the top part to the mouse_x, mouse_y.

Then in your player set their direction to equal a point direction between the joystick background and the joystick top object.

I think that ought to work?
 
Y

Yvalson

Guest
So you want a virtual joystick?

Should be possible to do relatively simply I'd imagine.

You could create an object with a sprite for the background of the joystick, with the origin set to centred.

Then create another object for the top/moving part of the joystick, again centre the origin. Then on touch/click if the mouse/finger is coliding with the joystick background then update the x/y of the top part to the mouse_x, mouse_y.

Then in your player set their direction to equal a point direction between the joystick background and the joystick top object.

I think that ought to work?
yeah a virtual joystick i'm already working the outer and inner rings(as I call them) but could you explain how to do the last part with the directions
 
Y

Yvalson

Guest
Its a function called point_direction. You can find its entry in the manual here:
https://docs.yoyogames.com/source/dadiospice/002_reference/maths/vector functions/point_direction.html

Basically you give it two sets of coordinates and it returns the direction value of the vector from the first set to the second set.

So the code in your player would be something like:
direction = point_direction(outerRing.x, outerRing.y, innerRing.x, innerRing.y);
wow thanks man gonna try it right now

holy cow it works thank you so much
 
Last edited by a moderator:

JackTurbo

Member
wow thanks man gonna try it right now

holy cow it works thank you so much
No worries, glad it helped.

If you're not familiar with them, I'd suggest reading up on the other vector functions in the manual, they're all super useful. :)
 
Y

Yvalson

Guest
one more question I think is relevant for this topic too

i'm currently using clamp

x = clamp(x, 32, 224);
y = clamp(y, 800, 960);

to make the innerring stay inside the outerring
but im also using views. however clamp is currently used on my room so how can I make it work with my view?
 

JackTurbo

Member
one more question I think is relevant for this topic too

i'm currently using clamp

x = clamp(x, 32, 224);
y = clamp(y, 800, 960);

to make the innerring stay inside the outerring
but im also using views. however clamp is currently used on my room so how can I make it work with my view?
Assuming you're not rotating you view or using it to zoom in/out then it should be as simple as adding the view x/y to your desired coordinates. So something like:

Code:
x = clamp(x, view_xview + 32, view_xview +  224);
y = clamp(y,  view_yview + 800,  view_yview + 960);
 
Y

Yvalson

Guest
Assuming you're not rotating you view or using it to zoom in/out then it should be as simple as adding the view x/y to your desired coordinates. So something like:

Code:
x = clamp(x, view_xview + 32, view_xview +  224);
y = clamp(y,  view_yview + 800,  view_yview + 960);
the view is zoomed in the real size of the room is 2000 width 1000 height

the view in room is 1152 by 648
and the port to screen is 1600 by 900
 

JackTurbo

Member
the view is zoomed in the real size of the room is 2000 width 1000 height

the view in room is 1152 by 648
and the port to screen is 1600 by 900
Then you'd need to do a bunch of math to get it to work correctly.

Realistically though, it'd probably be better to move this element up to the GUI layer which should save some of these headaches.
 
Y

Yvalson

Guest
Then you'd need to do a bunch of math to get it to work correctly.

Realistically though, it'd probably be better to move this element up to the GUI layer which should save some of these headaches.
how do I implement that? if I move it to the Draw GUI
 
Top