Gamestudio Links
Zorro Links
Newest Posts
Trading Journey
by howardR. 04/28/24 09:55
Zorro Trader GPT
by TipmyPip. 04/27/24 13:50
Help with plotting multiple ZigZag
by M_D. 04/26/24 20:03
Data from CSV not parsed correctly
by jcl. 04/26/24 11:18
M1 Oversampling
by jcl. 04/26/24 11:12
Why Zorro supports up to 72 cores?
by jcl. 04/26/24 11:09
Eigenwerbung
by jcl. 04/26/24 11:08
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
2 registered members (AndrewAMD, Nymphodora), 485 guests, and 3 spiders.
Key: Admin, Global Mod, Mod
Newest Members
wandaluciaia, Mega_Rod, EternallyCurious, howardR, 11honza11
19049 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 1 of 2 1 2
Noob question about setting orientations. #446986
11/10/14 20:40
11/10/14 20:40
Joined: Nov 2014
Posts: 24
ME
H
Hawthourne Offline OP
Newbie
Hawthourne  Offline OP
Newbie
H

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? crazy 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 Offline
Senior Expert
3run  Offline
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:
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


Looking for free stuff?? Take a look here: http://badcom.at.ua
Support me on: https://boosty.to/3rung
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 Offline
Serious User
Reconnoiter  Offline
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:

Code:
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: Reconnoiter] #447000
11/11/14 20:19
11/11/14 20:19
Joined: Nov 2014
Posts: 24
ME
H
Hawthourne Offline OP
Newbie
Hawthourne  Offline OP
Newbie
H

Joined: Nov 2014
Posts: 24
ME
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.

Re: Noob question about setting orientations. [Re: Hawthourne] #447121
11/18/14 00:35
11/18/14 00:35
Joined: Nov 2014
Posts: 24
ME
H
Hawthourne Offline OP
Newbie
Hawthourne  Offline OP
Newbie
H

Joined: Nov 2014
Posts: 24
ME
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?

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
W
Wjbender Offline
User
Wjbender  Offline
User
W

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
H
Hawthourne Offline OP
Newbie
Hawthourne  Offline OP
Newbie
H

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
W
Wjbender Offline
User
Wjbender  Offline
User
W

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: Wjbender] #447170
11/21/14 12:06
11/21/14 12:06
Joined: Dec 2011
Posts: 1,823
Netherlands
Reconnoiter Offline
Serious User
Reconnoiter  Offline
Serious User

Joined: Dec 2011
Posts: 1,823
Netherlands
@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).

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
H
Hawthourne Offline OP
Newbie
Hawthourne  Offline OP
Newbie
H

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;
}

Page 1 of 2 1 2

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1