Unable to add strafe function...!

I’m trying add strafe function in my program, i tried everything i can but still i was unable to get desire output! Can anyone please tell me how can i add strafe function in my program?.. :thinking: :roll_eyes:

here is how my camera function look like:

    glTranslatef(xtrans, ytrans, -5);
	glRotatef(updateY, 1, 0, 0);
	glRotatef(updateX, 0, 1, 0);
	glTranslatef(-1, -1, 0);

here is my input…

    void MouseMove(int MouseX, int MouseY)
    {
	    if (panning == true)
    	{
    		changeAngleX_pan = (MouseX - xOrigin_pan);
    		changeAngleY_pan = (MouseY - yOrigin_pan);
    		ndc_x = changeAngleX_pan * 2.0f / 500;		//ndc_x = No direction change in x-field
    		ndc_y = -changeAngleY_pan * 2.0f / 500;		//ndc_y = No direction change in y-field
    
    		xtrans = ndc_x1 + ndc_x;
    		ytrans = ndc_y1 + ndc_y;
    	}
    	else
    	{
    		changeAngleX = (MouseX - xOrigin);
    		changeAngleY = (MouseY - yOrigin);
    		updateX = angleX + changeAngleX;
    		updateY = angleY + changeAngleY;
    	}
    	Display();
    }

to view my full code :point_right: click here.

is there anyone who uses Blender (3d modeling/animation software)…if you use blender…then do you know how center point or looking point of camera changes while panning…! that’s what i’m trying to add in my program…

You could start out by explaining what you mean by strafing.
Moving above a terrain while panning the camera?

Strafing is generally moving directly left or right, instead of rotating the camera.
So I want to change my look-at values of camera, like when camera move (not rotate) to left/right side then look-at value also move to left or right with respect to camera’s current position.

Note:- look-at value in current question is (-1,-1,0) in 4th line gltranslate(-1,-1,0)

your transforms confuse me with reference to the view-matrix.
mixing translates and rotates is tricky since the order is important.
the LookAt() that can produces a view-matrix takes two positions as input. If you used this to produce the matrix you would add an offset to both points for a strafe. If you recall the other thread: a position and a direction can be gleaned from the two arguments.

As for panning only centerpoint is moving (center of what you’r looking at). The lookAt() would suit well for it’s start before panning. Recall that it has a position now - that’ll foul up a rotation. To rotate it around itself you’ll conseptually
translate it to (0,0,0)
do an incremental rotate
translate back
… edit …
About the strafe: The offset should be perpendicular to the ‘forward’ direction. The first column of the view-matrix is the x unit-vector and should be usable for the purpose.
And, for the rotation:
You could try the rotate(position, axis) version with position = -(position of view-matrix). And without the translation back and forth.

1 Like

i’m not able to understand/confused what exactly i should do?..sir can you please suggest me where i edit in

glTranslatef(xtrans, ytrans, -5);
glRotatef(updateY, 1, 0, 0);
glRotatef(updateX, 0, 1, 0);
glTranslatef(-1, -1, 0);

@VinDrawins
before I start explaining:
Take a piece of paper and draw what you do to the camera, including it’s start-pose.
You seem to have chosen (-1,-1,0) as a fixed position for the camera. The 4 transformations looks as if you mean:

  1. translate camera to 0,0
  2. do rotations on camera
  3. translate camera to where it came from

… instead of (-xtrans,-ytrans,5) you choose (-1,-1,0). Apart for that, it’s ok for the rotate.
view = transform(view, trans-params);

Now, your strafe will work differently than before the rotate since it ideally should translate the camera perpendicularly sideways. The view-matrix exposes it’s gaze-direction (-z-axis) in it’s data as the third column of the 4*4-matrix. The perpendicular as the first column … and it’s position as the fourth.
You scale the .x unit-axis (first column) with the drag.x and adds it to the position …

    vec4 xa = 1.column
    vec4 xOff = drag.x * xa
    view[][4]  += xOff  ;

you’ll have to look up the proper writing for the above pseudo-code

and the camera will move perpendicularly sideways, strafe without rotation.
You know how to translate the view-matrix … you can rewrite the fourth column directly as an alternative way.

You may confuse yourself in: if you start building the view from scratch (identity) you’ll need the summed translates and rotates before you add dX,dY. If you reuse the transformed view, you only need dX,dY in the transform-parameters … does that make sense?

dX is a standard way of writing ‘difference in x’ = X2 - X1 = current_mouse.x. - previous_mouse.x.

1 Like

You can releave some GUI confusion by

vec2 old_position;
bool mouse_is_down;
bool start_drag;
bool do_rotate;
bool do_strafe;

void cursor( vec2 position ){
	
	if(start_drag){
		old_position=position ;
		start_drag = false;
	}
	if(mouse_is_down){
		drag(position.x-old_position.x , position.y-old_position.y  );
		old_position = position;
	}
}

void mouse( .. params..){
	//set 
	mouse_is_down=true;
	//when relevant, but also:
	start_drag=true;
}

void drag( float dX, float dY){
	if(do_rotate){
		...
	}
	if(do_strafe){
		...
	}
}
1 Like

Thank very very much :star_struck:…i get it​:grin::+1:t2:.

Can I understand this as “the code works” or did it turn out to be “I got somewhat wiser, but…” ?