2 registered members (Ayumi, kzhao),
1,354
guests, and 5
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Noob question about setting orientations.
#446986
11/10/14 20:40
11/10/14 20:40
|
Joined: Nov 2014
Posts: 24 ME
Hawthourne
OP
Newbie
|
OP
Newbie
Joined: Nov 2014
Posts: 24
ME
|
Greetings, so I have been going through tutorials and working on some basic projects, but I have a question which Google, forums, and manuals doesn't seem to be helping me with. If I want an object to face a variable point (like the mouse cursor), how might I go about doing that? My plan was to take the difference between the x and y components of my object and the mouse, but SED doesn't seem to have arctan programmed into it. I tried using the first 40 terms or so of the taylor expansion, but it still seems to break down. Currently my only remaining idea is to try if/then statements consisting of if(tan(.5) < (mouse_cursor.y- Turret.pos_y)/(mouse_cursor.x - Turret.pos_x) < tan(1.5)) object.angle = 1 And to do this 360 times for each degree (or reasonable approximations, maybe every 3rd degree). I would also have to work up another case for the 180-360 range. Any suggestions for a noob to make the code much simpler? I'm gonna feel really stupid if there is a simple command to make it happen. Thanks!
|
|
|
Re: Noob question about setting orientations.
[Re: Hawthourne]
#446992
11/11/14 09:34
11/11/14 09:34
|
Joined: May 2009
Posts: 5,370 Caucasus
3run
Senior Expert
|
Senior Expert
Joined: May 2009
Posts: 5,370
Caucasus
|
Hi there! Welcome first of all! About rotating entity to a specific point, you can do the following:
// rotate towards specific point:
function turn_towards_target(ENTITY* ent, VECTOR* to){
// create temp vector:
VECTOR tempVec;
// free vector (set XYZ to zero):
vec_fill(tempVec.x, 0);
// save target position:
vec_set(tempVec, to.x);
// subtract our current position:
vec_sub(tempVec, ent.x);
// rotate towards the target (as we've got a direction to rotate at):
vec_to_angle(ent.pan, tempVec);
}
So in order to use it, you need to find 'target' position, to rotate at, and use it like this:
// my - entity which will rotate
// targetVec - position for entity to rotate at
turn_towards_target(my, targetVec);
This code will change entitie's tilt angle as well (f.e. when target position will be bellow entities feet, it will rotate downwards), in order to prevent that, reset it's TILT angle to zero after calling this function. About mouse cursor, you'll have to find the mouse position in the world, there are several solutions for this: mouse_dir3d mouse_pos3dGreets
|
|
|
Re: Noob question about setting orientations.
[Re: 3run]
#446994
11/11/14 13:08
11/11/14 13:08
|
Joined: Dec 2011
Posts: 1,823 Netherlands
Reconnoiter
Serious User
|
Serious User
Joined: Dec 2011
Posts: 1,823
Netherlands
|
Hi Hawthourne, to add to what 3run said, you might want to check this thread too http://www.opserver.de/ubb7/ubbthreads.php?ubb=showflat&Number=445062&page=1, and for example take txesmi's solution. Basically the main function you are looking for is vec_to_angle . So lets say you have your vector point you want your object to you look at (e.g. at the Player), the function looks something like this:
vec_to_angle(object.pan, player.x); //this lets object look at player
Last edited by Reconnoiter; 11/11/14 13:09.
|
|
|
Re: Noob question about setting orientations.
[Re: Hawthourne]
#447123
11/18/14 08:20
11/18/14 08:20
|
Joined: Mar 2012
Posts: 927 cyberspace
Wjbender
User
|
User
Joined: Mar 2012
Posts: 927
cyberspace
|
you are going to have to clarify exactly what you have and what you need to do .
you have mentioned an object then you mentioned a sprite , it's hard to understand what you want to rotate if you say object and sprite , is it a 3d object or is it a 2d sprite ?
being clear will help people help you.
if it's a problem in your code you are going to need help with , please provide your code.
Last edited by Wjbender; 11/18/14 08:23.
Compulsive compiler
|
|
|
Re: Noob question about setting orientations.
[Re: Wjbender]
#447161
11/20/14 20:27
11/20/14 20:27
|
Joined: Nov 2014
Posts: 24 ME
Hawthourne
OP
Newbie
|
OP
Newbie
Joined: Nov 2014
Posts: 24
ME
|
Basically, I am trying to design a basic 2D game/program as a test run through the code. There is a turret (I'm using a panel, but I believe a sprite would act in a similar manner) which I am trying to track the mouse cursor with (hence the rotational part). Using the "vec_to_angle" command, I was able to succeed in rotating the turret to track the cursor, but it doesn't rotate around the center of the turret, rather the top-left of the panel. By using,
Turret.center_y = Turret.size_y * .5;
I was able to shift the rotation point to being halfway down the panel. However, when I try to use Turret.center_x = Turret.size_x * .5;
to move the center of rotation to the right, it has no discernible effect, even if I just set "Turret.center_y = Turret.size_y +500;" for testing purposes. Sorry about the terminology swap, I's still getting used to it, but in this case I am specifically working with a panel in a 2D environment.
|
|
|
Re: Noob question about setting orientations.
[Re: Hawthourne]
#447164
11/20/14 21:50
11/20/14 21:50
|
Joined: Mar 2012
Posts: 927 cyberspace
Wjbender
User
|
User
Joined: Mar 2012
Posts: 927
cyberspace
|
turret.center_x=bmap_width (turretbmp)/2; turret.center_y=bmap_height (turretbmp)/2;
rotate the sprite with turret.angle .. here turretbmp represents your image pointer.
turret.size_x / 2 and turret.size_y / 2 should work
edited:typo
oh , and bmaps and panels and in general 2d objects are handled in integer values , integers are numbers with no decimal point , like 123456789 but doubles or floats or vars can have decimal points for precision like 0.5 1.890 45.55
Last edited by Wjbender; 11/20/14 22:08.
Compulsive compiler
|
|
|
Re: Noob question about setting orientations.
[Re: Reconnoiter]
#447172
11/21/14 19:00
11/21/14 19:00
|
Joined: Nov 2014
Posts: 24 ME
Hawthourne
OP
Newbie
|
OP
Newbie
Joined: Nov 2014
Posts: 24
ME
|
Thanks for the feedback. As you can see, I attempted to change the center_x/center_y in my previous reply. I replaced the "* .5" with "/2", thinking that might help but it didn't. For testing purposes, I also set center_x = 800 to see if things would mess up, but there was no discernible impact on the gameplay (center_y appears to work).
My main function is shown below. The important lines (I think) have a "$" in front of them.
function main() { level_load("") ; mouse_mode = 2; //mouse_map = cursor; PatrolCraft.center_x = PatrolCraft.size_x * .5; PatrolCraft.center_y = PatrolCraft.size_y * .5; $ Turret.center_y = Turret.size_y * .5; $ Turret.center_x = Turret.size_x * .5; while (1) { x=PatrolCraft.pos_x ; y=PatrolCraft.pos_y ; if (key_d) PatrolCraft.pos_x += .9; if (key_d) x+= 100; if (key_w) PatrolCraft.pos_y -= .9; if (key_w) y-= 100; if (key_a) PatrolCraft.pos_x -= .9; if (key_a) x-= 100; if (key_s) PatrolCraft.pos_y += .9; if (key_s) y+= 100;
vec_to_angle(PatrolCraft.angle, vector(x-PatrolCraft.pos_x, -y + PatrolCraft.pos_y,0)); Turret.pos_x = PatrolCraft.pos_x + 33; Turret.pos_y = PatrolCraft.pos_y+ 13; $ vec_to_angle(Turret.angle, vector(mouse_cursor.x - Turret.pos_x,-mouse_cursor.y+Turret.pos_y,0)); wait(4) ; if(mouse_left) fire() ; mouse_pos.x = mouse_cursor.x; mouse_pos.y = mouse_cursor.y; }
|
|
|
Moderated by mk_1, Perro, rayp, Realspawn, Rei_Ayanami, rvL_eXile, Spirit, Superku, Tobias, TSG_Torsten, VeT
|