mirror of
https://github.com/Ladebeze66/cub3D.git
synced 2026-11-09 13:47:10 +01:00
manquecorrigetouchesetparsing
This commit is contained in:
@@ -6,45 +6,48 @@
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/01/11 14:09:52 by fgras-ca #+# #+# */
|
||||
/* Updated: 2024/01/11 20:35:31 by fgras-ca ### ########.fr */
|
||||
/* Updated: 2024/01/21 21:40:56 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../cub3d.h"
|
||||
|
||||
void calculate_future_position(t_position_params *params)
|
||||
{
|
||||
*(params->future_px) = params->px + params->pdx + params->collisionBuffer;
|
||||
*(params->future_py) = params->py + params->pdy + params->collisionBuffer;
|
||||
void calculate_future_position(t_position_params *params) {
|
||||
*(params->future_px) = params->px + params->pdx + params->collisionBuffer;
|
||||
*(params->future_py) = params->py + params->pdy + params->collisionBuffer;
|
||||
|
||||
printf("calculate_future_position: Current px = %d, pdx = %d, collisionBuffer = %d\n", params->px, params->pdx, params->collisionBuffer);
|
||||
printf("calculate_future_position: Current py = %d, pdy = %d, collisionBuffer = %d\n", params->py, params->pdy, params->collisionBuffer);
|
||||
printf("calculate_future_position: Calculated Future Position: x = %d, y = %d\n", *(params->future_px), *(params->future_py));
|
||||
}
|
||||
|
||||
int check_collision(t_structure_main *w, int future_px, int future_py)
|
||||
{
|
||||
int future_ipx;
|
||||
int future_ipy;
|
||||
int check_collision(t_structure_main *w, int future_px, int future_py) {
|
||||
int future_ipx = future_px / w->s_map.mapS;
|
||||
int future_ipy = future_py / w->s_map.mapS;
|
||||
|
||||
future_ipx = future_px / w->s_map.mapS;
|
||||
future_ipy = future_py / w->s_map.mapS;
|
||||
return (w->s_map.map[future_ipy * w->s_map.mapX + future_ipx] == '0');
|
||||
printf("check_collision: Checking Collision at Future Map Index: x = %d, y = %d\n", future_ipx, future_ipy);
|
||||
printf("check_collision: Collision Status: %s\n", w->s_map.map[future_ipy * w->s_map.mapX + future_ipx] == '0' ? "No Collision" : "Collision Detected");
|
||||
|
||||
return (w->s_map.map[future_ipy * w->s_map.mapX + future_ipx] == '0');
|
||||
}
|
||||
|
||||
void calculate_future_position_right_left(t_position_params *params)
|
||||
{
|
||||
double angle;
|
||||
int collision_px;
|
||||
int collision_py;
|
||||
void calculate_future_position_right_left(t_position_params *params) {
|
||||
double angle = params->pa;
|
||||
int collision_px;
|
||||
int collision_py;
|
||||
|
||||
angle = params->pa;
|
||||
if (params->direction == 'd')
|
||||
angle += M_PI_2;
|
||||
else
|
||||
angle -= M_PI_2;
|
||||
*(params->future_px) = params->px + (int)(cos(angle) * 5);
|
||||
*(params->future_py) = params->py + (int)(sin(angle) * 5);
|
||||
collision_px = *(params->future_px) + (int)(cos(params->pa)
|
||||
* params->collisionBuffer);
|
||||
collision_py = *(params->future_py) + (int)(sin(params->pa)
|
||||
* params->collisionBuffer);
|
||||
*(params->future_px) = collision_px;
|
||||
*(params->future_py) = collision_py;
|
||||
if (params->direction == 'd')
|
||||
angle += M_PI_2;
|
||||
else
|
||||
angle -= M_PI_2;
|
||||
|
||||
*(params->future_px) = params->px + (int)(cos(angle) * 5);
|
||||
*(params->future_py) = params->py + (int)(sin(angle) * 5);
|
||||
collision_px = *(params->future_px) + (int)(cos(params->pa) * params->collisionBuffer);
|
||||
collision_py = *(params->future_py) + (int)(sin(params->pa) * params->collisionBuffer);
|
||||
*(params->future_px) = collision_px;
|
||||
*(params->future_py) = collision_py;
|
||||
|
||||
printf("calculate_future_position_right_left: Current px = %d, py = %d, direction = %c\n", params->px, params->py, params->direction);
|
||||
printf("calculate_future_position_right_left: Calculated Future Position: x = %d, y = %d\n", collision_px, collision_py);
|
||||
}
|
||||
|
||||
Binary file not shown.
@@ -6,7 +6,7 @@
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/01/11 14:17:56 by fgras-ca #+# #+# */
|
||||
/* Updated: 2024/01/12 20:28:07 by fgras-ca ### ########.fr */
|
||||
/* Updated: 2024/01/21 21:12:41 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -35,37 +35,44 @@ int *kill_prog(t_structure_main *w)
|
||||
return (0);
|
||||
}
|
||||
|
||||
void adjust_player_angle(t_structure_main *w, int key)
|
||||
void adjust_player_angle(t_structure_main *w, int key)
|
||||
{
|
||||
double angle_adjustment;
|
||||
double angle_adjustment;
|
||||
printf("adjust_player_angle: Key pressed: %d\n", key);
|
||||
printf("adjust_player_angle: Before adjustment - pa: %f\n", w->s_player.pa);
|
||||
|
||||
if (key != 65361 && key != 65363)
|
||||
return ;
|
||||
angle_adjustment = 16 * ((PI / 3) / NUMRAY);
|
||||
if (key == 65361)
|
||||
{
|
||||
w->s_player.pa -= angle_adjustment;
|
||||
if (w->s_player.pa < 0)
|
||||
w->s_player.pa += 2 * PI;
|
||||
}
|
||||
else
|
||||
{
|
||||
w->s_player.pa += angle_adjustment;
|
||||
if (w->s_player.pa > 2 * PI)
|
||||
w->s_player.pa -= 2 * PI;
|
||||
}
|
||||
w->s_player.pdx = cos(w->s_player.pa) * 5;
|
||||
w->s_player.pdy = sin(w->s_player.pa) * 5;
|
||||
if (key != 65361 && key != 65363)
|
||||
return;
|
||||
angle_adjustment = 16 * ((PI / 3) / NUMRAY);
|
||||
if (key == 65361)
|
||||
{
|
||||
w->s_player.pa -= angle_adjustment;
|
||||
if (w->s_player.pa < 0)
|
||||
w->s_player.pa += 2 * PI;
|
||||
}
|
||||
else
|
||||
{
|
||||
w->s_player.pa += angle_adjustment;
|
||||
if (w->s_player.pa > 2 * PI)
|
||||
w->s_player.pa -= 2 * PI;
|
||||
}
|
||||
w->s_player.pdx = cos(w->s_player.pa) * 5;
|
||||
w->s_player.pdy = sin(w->s_player.pa) * 5;
|
||||
printf("adjust_player_angle: After adjustment - pa: %f\n", w->s_player.pa);
|
||||
}
|
||||
|
||||
void handle_movement_keys(int key, t_structure_main *w)
|
||||
void handle_movement_keys(t_structure_main *w, int key)
|
||||
{
|
||||
if (key == 119 || key == 100 || key == 115 || key == 97)
|
||||
move(key, w);
|
||||
{
|
||||
adjust_player_angle(w, key);
|
||||
move(w, key);
|
||||
}
|
||||
}
|
||||
|
||||
int deal_key(int key, t_structure_main *w)
|
||||
{
|
||||
printf("deal_key: Key received: %d\n", key);
|
||||
if (key == 65307)
|
||||
{
|
||||
kill_prog(w);
|
||||
@@ -74,12 +81,10 @@ int deal_key(int key, t_structure_main *w)
|
||||
{
|
||||
adjust_player_angle(w, key);
|
||||
}
|
||||
else if (key == 65362 || key == 65364 || key == 114 || key == 102)
|
||||
{
|
||||
}
|
||||
else
|
||||
{
|
||||
handle_movement_keys(key, w);
|
||||
handle_movement_keys(w, key);
|
||||
}
|
||||
printf("deal_key: Key handled: %d\n", key);
|
||||
return (key);
|
||||
}
|
||||
|
||||
Binary file not shown.
@@ -6,105 +6,117 @@
|
||||
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/01/11 14:08:25 by fgras-ca #+# #+# */
|
||||
/* Updated: 2024/01/11 20:35:09 by fgras-ca ### ########.fr */
|
||||
/* Updated: 2024/01/21 21:09:26 by fgras-ca ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../cub3d.h"
|
||||
|
||||
void move_forward(t_structure_main *w, int key)
|
||||
void move_forward(t_structure_main *w, int key)
|
||||
{
|
||||
t_position_params params;
|
||||
int future_px;
|
||||
int future_py;
|
||||
printf("move_forward: Key pressed: %d\n", key);
|
||||
printf("move_forward: Before move - px: %f, py: %f, pdx: %f, pdy: %f\n", w->s_player.px, w->s_player.py, w->s_player.pdx, w->s_player.pdy);
|
||||
t_position_params params;
|
||||
int future_px;
|
||||
int future_py;
|
||||
|
||||
if (key != 119)
|
||||
return ;
|
||||
params.future_px = &future_px;
|
||||
params.future_py = &future_py;
|
||||
params.pdx = w->s_player.pdx;
|
||||
params.pdy = w->s_player.pdy;
|
||||
params.collisionBuffer = COLBUF;
|
||||
params.px = w->s_player.px;
|
||||
params.py = w->s_player.py;
|
||||
calculate_future_position(¶ms);
|
||||
if (check_collision(w, future_px, future_py))
|
||||
{
|
||||
w->s_player.px += w->s_player.pdx;
|
||||
w->s_player.py += w->s_player.pdy;
|
||||
}
|
||||
if (key != 119)
|
||||
return;
|
||||
params.future_px = &future_px;
|
||||
params.future_py = &future_py;
|
||||
params.pdx = w->s_player.pdx;
|
||||
params.pdy = w->s_player.pdy;
|
||||
params.collisionBuffer = COLBUF;
|
||||
params.px = w->s_player.px;
|
||||
params.py = w->s_player.py;
|
||||
calculate_future_position(¶ms);
|
||||
if (check_collision(w, future_px, future_py))
|
||||
{
|
||||
w->s_player.px += w->s_player.pdx;
|
||||
w->s_player.py += w->s_player.pdy;
|
||||
}
|
||||
printf("move_forward: After move - px: %f, py: %f\n", w->s_player.px, w->s_player.py);
|
||||
}
|
||||
|
||||
void move_backward(t_structure_main *w, int key)
|
||||
void move_backward(t_structure_main *w, int key)
|
||||
{
|
||||
t_position_params params;
|
||||
int future_px;
|
||||
int future_py;
|
||||
printf("move_forward: Key pressed: %d\n", key);
|
||||
printf("move_forward: Before move - px: %f, py: %f, pdx: %f, pdy: %f\n", w->s_player.px, w->s_player.py, w->s_player.pdx, w->s_player.pdy);
|
||||
t_position_params params;
|
||||
int future_px;
|
||||
int future_py;
|
||||
|
||||
if (key != 115)
|
||||
return ;
|
||||
params.future_px = &future_px;
|
||||
params.future_py = &future_py;
|
||||
params.pdx = -w->s_player.pdx;
|
||||
params.pdy = -w->s_player.pdy;
|
||||
params.collisionBuffer = COLBUF;
|
||||
params.px = w->s_player.px;
|
||||
params.py = w->s_player.py;
|
||||
calculate_future_position(¶ms);
|
||||
if (check_collision(w, future_px, future_py))
|
||||
{
|
||||
w->s_player.px -= w->s_player.pdx;
|
||||
w->s_player.py -= w->s_player.pdy;
|
||||
}
|
||||
if (key != 115)
|
||||
return;
|
||||
params.future_px = &future_px;
|
||||
params.future_py = &future_py;
|
||||
params.pdx = -w->s_player.pdx;
|
||||
params.pdy = -w->s_player.pdy;
|
||||
params.collisionBuffer = COLBUF;
|
||||
params.px = w->s_player.px;
|
||||
params.py = w->s_player.py;
|
||||
calculate_future_position(¶ms);
|
||||
if (check_collision(w, future_px, future_py))
|
||||
{
|
||||
w->s_player.px -= w->s_player.pdx;
|
||||
w->s_player.py -= w->s_player.pdy;
|
||||
}
|
||||
printf("Move Backward: Player position after moving backward: x = %f, y = %f\n", w->s_player.px, w->s_player.py);
|
||||
}
|
||||
|
||||
void move_right(t_structure_main *w, int key)
|
||||
void move_right(t_structure_main *w, int key)
|
||||
{
|
||||
t_position_params params;
|
||||
int future_px;
|
||||
int future_py;
|
||||
printf("move_forward: Key pressed: %d\n", key);
|
||||
printf("move_forward: Before move - px: %f, py: %f, pdx: %f, pdy: %f\n", w->s_player.px, w->s_player.py, w->s_player.pdx, w->s_player.pdy);
|
||||
t_position_params params;
|
||||
int future_px;
|
||||
int future_py;
|
||||
|
||||
if (key != 100)
|
||||
return ;
|
||||
params.future_px = &future_px;
|
||||
params.future_py = &future_py;
|
||||
params.pa = w->s_player.pa;
|
||||
params.collisionBuffer = COLBUF;
|
||||
params.px = w->s_player.px;
|
||||
params.py = w->s_player.py;
|
||||
params.direction = 'd';
|
||||
calculate_future_position_right_left(¶ms);
|
||||
if (check_collision(w, future_px, future_py))
|
||||
{
|
||||
w->s_player.px = future_px;
|
||||
w->s_player.py = future_py;
|
||||
}
|
||||
if (key != 100)
|
||||
return;
|
||||
params.future_px = &future_px;
|
||||
params.future_py = &future_py;
|
||||
params.pa = w->s_player.pa;
|
||||
params.collisionBuffer = COLBUF;
|
||||
params.px = w->s_player.px;
|
||||
params.py = w->s_player.py;
|
||||
params.direction = 'd';
|
||||
calculate_future_position_right_left(¶ms);
|
||||
if (check_collision(w, future_px, future_py))
|
||||
{
|
||||
w->s_player.px = future_px;
|
||||
w->s_player.py = future_py;
|
||||
}
|
||||
printf("Move Backward: Player position after moving backward: x = %f, y = %f\n", w->s_player.px, w->s_player.py);
|
||||
}
|
||||
|
||||
void move_left(t_structure_main *w, int key)
|
||||
void move_left(t_structure_main *w, int key)
|
||||
{
|
||||
t_position_params params;
|
||||
int future_px;
|
||||
int future_py;
|
||||
printf("move_forward: Key pressed: %d\n", key);
|
||||
printf("move_forward: Before move - px: %f, py: %f, pdx: %f, pdy: %f\n", w->s_player.px, w->s_player.py, w->s_player.pdx, w->s_player.pdy);
|
||||
t_position_params params;
|
||||
int future_px;
|
||||
int future_py;
|
||||
|
||||
if (key != 97)
|
||||
return ;
|
||||
params.future_px = &future_px;
|
||||
params.future_py = &future_py;
|
||||
params.pa = w->s_player.pa;
|
||||
params.collisionBuffer = COLBUF;
|
||||
params.px = w->s_player.px;
|
||||
params.py = w->s_player.py;
|
||||
params.direction = 'a';
|
||||
calculate_future_position_right_left(¶ms);
|
||||
if (check_collision(w, future_px, future_py))
|
||||
{
|
||||
w->s_player.px = future_px;
|
||||
w->s_player.py = future_py;
|
||||
}
|
||||
if (key != 97)
|
||||
return;
|
||||
params.future_px = &future_px;
|
||||
params.future_py = &future_py;
|
||||
params.pa = w->s_player.pa;
|
||||
params.collisionBuffer = COLBUF;
|
||||
params.px = w->s_player.px;
|
||||
params.py = w->s_player.py;
|
||||
params.direction = 'a';
|
||||
calculate_future_position_right_left(¶ms);
|
||||
if (check_collision(w, future_px, future_py))
|
||||
{
|
||||
w->s_player.px = future_px;
|
||||
w->s_player.py = future_py;
|
||||
}
|
||||
printf("Move Backward: Player position after moving backward: x = %f, y = %f\n", w->s_player.px, w->s_player.py);
|
||||
}
|
||||
|
||||
void move(int key, t_structure_main *w)
|
||||
void move(t_structure_main *w, int key)
|
||||
{
|
||||
move_forward(w, key);
|
||||
move_backward(w, key);
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user