Noob question about setting orientations.

Posted By: Hawthourne

Noob question about setting orientations. - 11/10/14 20:40

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? crazy I'm gonna feel really stupid if there is a simple command to make it happen.

Thanks!
Posted By: 3run

Re: Noob question about setting orientations. - 11/11/14 09:34

Hi there! Welcome first of all!

About rotating entity to a specific point, you can do the following:
Code:
// 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:
Code:
// 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_pos3d

Greets
Posted By: Reconnoiter

Re: Noob question about setting orientations. - 11/11/14 13:08

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:

Code:
vec_to_angle(object.pan, player.x); //this lets object look at player

Posted By: Hawthourne

Re: Noob question about setting orientations. - 11/11/14 20:19

Thanks for the assist! I am enjoying learning the programming language and how the logical arguments fit together. As a math major, a lot of it is fairly intuitive but I still have quite a bit to learn.
Posted By: Hawthourne

Re: Noob question about setting orientations. - 11/18/14 00:35

Though using the "vec_to_angle" lets me rotate the object, I am having a little trouble setting the point that it swivels around. Using the "Turret.center_y=" command, I can alter the relative position of the point it rotates around on the sprite. However, the "Turret.center_x =" command fails to change the x coordinate of the point it rotates around. Any ideas?
Posted By: Wjbender

Re: Noob question about setting orientations. - 11/18/14 08:20

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.
Posted By: Hawthourne

Re: Noob question about setting orientations. - 11/20/14 20:27

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.
Posted By: Wjbender

Re: Noob question about setting orientations. - 11/20/14 21:50

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
Posted By: Reconnoiter

Re: Noob question about setting orientations. - 11/21/14 12:06

@Hawthourne, if you want to rotate a panel around its center, change its center_x/center_y. That said, I would not recommend using a panel for something like a player or an enemy. Sprite is better for that (more flexibel) and easier when changing the camera position.

You can use panels for e.g. 2d tile-based games though (e.g. see AUM 83).
Posted By: Hawthourne

Re: Noob question about setting orientations. - 11/21/14 19:00

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;
}
Posted By: Wjbender

Re: Noob question about setting orientations. - 11/21/14 20:15

Code:
void main()
{
	level_load("");
	mouse_mode=1;
	
	//after you created the panels you needed
	
	patrolcraft.center_x = patrolcraft.size_x /2;
	patrolcraft.center_y = patrolcraft.size_y /2;
	turret.center_x = turret.size_x /2;
	turret.center_y = turret.size_y /2;
	

	//rotation
	patrolcraft.angle+= (key_q-key_e)*9*time_step;
	
	//rotation
	turret.angle += mouse_force.x;

	//to move forward into the direction of the panel angle
	//you need someone else to help you 
	//but thats one way, you can set the panel.angle 
	//......
}



untested..

observe the usage of time_step ,this is because
you want your movement to act the same on computers with different speeds (frame rate), you can
solidify this time_step increments by setting the max
allowed frames per second through fps_max , usually
as a standard the eye of a human when it comes to
animation perceived by rapid frame changes ,is most
comfortable and easily tricked at 60 frames per second , 60 hz is also a general frequency update for
a computer screen , so it is advisable to set your max_fps=60 somewhere in the first line of your application entry point (the main function) .

another tip , like I said , it is best to stick to integer
values when your dealing with 2d sizes and positions , even though you could step in smaller increments during each frame for movement , ultimately your still dealing with pixels wich cannot have half sized pixels ,for example there is no position 0.5 by 0.5 on a computer screen in terms of pixels , yes ultimately luckily for you the size of the panel is integers also but it is best as I said to stick to integers on sizes and positions in 2d , but you may increase movement by using numbers with values exceeding the decimal , I know multiplication by 0.5 represent half af a number but sizes are integers here luckily for you , the problem however is that you may end up teaching yourself a habbit wich could apply badly to different cases , you are free to ignore this obviously ,just giving my 2cents ..

have fun .
Posted By: Reconnoiter

Re: Noob question about setting orientations. - 11/22/14 19:17

@Hawthourne, post your code within [code] [/code""] (without the quotes) tags in the future, makes it more clear to read here wink

Also incase you haven't already, AUM http://www.coniserver.net/coni_users/web_users/pirvu/aum/aumonline_e/ may also have some handy code examples for you (about e.g. panels)
Posted By: Hawthourne

Re: Noob question about setting orientations. - 11/23/14 04:43

So thanks for all the neat tips and such. I've been playing with them and see how they can be handy. I've read about the timestep stuff before, but hadn't thought about the integers thing.

However, I still have the same issue as I have had the past few posts. The script editor does not seem to be recognizing the center_x command for either of the panels I am playing with (center_y works fine). I'm not super familiar with computers, but I do recall an earlier issue I had with certain lines not being read. After switching the order of independent strings of code (seemingly irrelevant) it started working but such attempts with this line seem fruitless. Could it be a syntax issue or a bad install of the SED script editor?
Posted By: Hawthourne

Re: Noob question about setting orientations. - 11/23/14 05:04

So it seems to me that when I go back to my original plan, changing the angle by setting PatrolCraft.angle = somevalue it does take the center_x into account. vectoangle, on the other hand, ignores the center_x (though it will still take the center_y into account). I'm guessing that this is just a consequence as to how the code is set up and that there isn't any easy fix. Thanks for all of your assistance! My next project is familiarizing myself with entity generation within the script, but there seems to be a lot more material on that so hopefully I won't have to come back here asking more questions about it.
Posted By: Wjbender

Re: Noob question about setting orientations. - 11/23/14 08:28

just ignore the integer thingy its irrelevant ..

the vec_to_angle function however works for 3 dimensional entities where a pan tilt and roll are used , the panel.angle however only needs the angle value itself whereas vec_to_angle uses 3 components for
3d Euler angles , this is why I asked you if its a 2d object or a 3d object , there are different ways to deal
with each , axis orientation in 3d space is also different from 2d

don't feel bad about asking help , this is what this forum is for .
Posted By: Hawthourne

Re: Noob question about setting orientations. - 11/23/14 15:32

So is there an analogous function for 2 dimensional projects, like the one I am working on (what I am working on now). The vec_to_angle does work, it jsut doesn't rotate it around the panel's center. Amusingly, it seems that 2D might actually be harder than 3D in some respects, since in 3D levels you have the level editor to work with rather than doing everything in script. Most of the helpful material out there seems geared towards 3D projects as well.
Posted By: 3run

Re: Noob question about setting orientations. - 11/23/14 16:51

In this example which I've made, you can see how to rotate both, MODEL and PANEL!
Here you go:
Code:
// game function:
void main(){
	// load an empty level:
	level_load("");
	// make sky WHITE:
	vec_set(sky_color.blue, COLOR_WHITE);
	// no sun_light:
	sun_light = 0;
	// rotate camera downwards:
	vec_set(camera.pan, vector(0, -90, 0));
	
	// enable mouse:
	mouse_mode = 4;
	
	// create empty panel:
	PANEL* spaceShip = pan_create("", 1);
	// make it visible:
	set(spaceShip, SHOW);
	// create black bmap for it:
	spaceShip.bmap = bmap_createblack(16, 32, 24);
	// update panels size:
	spaceShip.size_x = bmap_width(spaceShip.bmap);
	spaceShip.size_y = bmap_height(spaceShip.bmap);
	// set rotation center for the panel (right in the middle):
	spaceShip.center_x = spaceShip.size_x * 0.5;
	spaceShip.center_y = spaceShip.size_y * 0.5;
	
	// create cube model:
	you = ent_create(CUBE_MDL, vector(0, 0, -50), NULL);
	// set it's size:
	vec_set(you.scale_x, vector(0.5, 0.25, 0.5));
	// make it change it's color:
	set(you, LIGHT);
	// make it's color GREEN:
	vec_set(you.blue, COLOR_GREEN);
	
	// temporary vector:
	VECTOR tempVec;
	// set all vector members to zero:
	vec_fill(tempVec.x, 0);
	
	// loop:
	while(!key_esc){
		
		// save mouse positions (XY):
		tempVec.x = mouse_pos.x;
		tempVec.y = mouse_pos.y;
		// save cube's Z position (not really needed):
		tempVec.z = you.z;
		
		// convert vector from screen coordinates into the world!
		vec_for_screen(tempVec.x, camera);
		
		// subtract cube's position (so we'll have a direction to rotate at):
		vec_sub(tempVec.x, you.x);
		// rotate cube towards the direction we've calculated above:
		vec_to_angle(you.pan, tempVec.x);
		// cycle cube's PAN angle from 0 to 360:
		you.pan %= 360;
		// don't allow to change it's TILT and ROLL angles:
		you.tilt = you.roll = 0;
		
		// place panel right in the middle of the screen:
		spaceShip.pos_x = (screen_size.x / 2) - (bmap_width(spaceShip.bmap) / 2);
		spaceShip.pos_y = (screen_size.y / 2) - (bmap_height(spaceShip.bmap) / 2);
		
		// set it's pan to the cube's pan:
		spaceShip.angle = you.pan;
		// cycle panel's pan from 0 to 360:
		spaceShip.angle %= 360;
		
		// show panel's angle on screen:
		DEBUG_VAR(spaceShip.angle, 10);
		// show cube's PAN angle on screen:
		DEBUG_VAR(you.pan, 40);
		// show current mouse position as a red quad:
		draw_quad(NULL, vector(mouse_pos.x, mouse_pos.y, 0), NULL, vector(16, 16, 0), NULL, COLOR_RED, 100, 0);
		// wait one frame:
		wait(1);
	}
	// exit application:
	sys_exit("bye!");
}

Please read carefully, and if you have any questions, before asking them here, read manual, it may give you an answer!


Greets
Posted By: Reconnoiter

Re: Noob question about setting orientations. - 12/05/14 12:32

A bit late to the party, but @Hawthourne, vec_to_angle can be used with 2d coordinates too. But since 2d works with only 2 coordinates, and not 3 like 3d, you need to set the z coordinates of the vector to zero and generally speaking pos_y = x and pos_x = y (see below code to understand what I mean by that). The code below uses mouse_pos as vector for vec_to_angle and so it lets mypanel rotate towards the cursor:

Code:
ANGLE vectoangle_ang;
 VECTOR temp_vec1, temp_vec2; 

 while (1)
 {
  vec_set(temp_vec1, vector(mypanel.pos_y, mypanel.pos_x, 0));
  vec_set(temp_vec2, vector(mouse_pos.y, mouse_pos.x, 0));
  vec_sub(temp_vec2, temp_vec1);	
  vec_to_angle(vectoangle_ang, temp_vec2);
  
  mypanel.center_x = (mypanel.size_x / 2); // set the rotation center at the panel center
  mypanel.center_y = (mypanel.size_y / 2);
  mypanel.angle = integer(vectoangle_ang.pan); //! only pan is usefull here
  
  DEBUG_VAR(vectoangle_ang.pan, 80); //just for testing purposes
  
  wait(1);
 }



I don't know if this great code or whatever, just know it works and its fairly easy to understand & to work with.
© 2024 lite-C Forums