Gamestudio Links
Zorro Links
Newest Posts
Data from CSV not parsed correctly
by EternallyCurious. 04/18/24 10:45
StartWeek not working as it should
by Zheka. 04/18/24 10:11
folder management functions
by VoroneTZ. 04/17/24 06:52
lookback setting performance issue
by 7th_zorro. 04/16/24 03:08
zorro 64bit command line support
by 7th_zorro. 04/15/24 09:36
Zorro FIX plugin - Experimental
by flink. 04/14/24 07:48
Zorro FIX plugin - Experimental
by flink. 04/14/24 07:46
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
2 registered members (EternallyCurious, howardR), 646 guests, and 0 spiders.
Key: Admin, Global Mod, Mod
Newest Members
EternallyCurious, 11honza11, ccorrea, sakolin, rajesh7827
19046 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Camera Collision in Workshop 25 - Multiplayer 11 #381409
08/27/11 16:36
08/27/11 16:36
Joined: Aug 2011
Posts: 42
F
fabiomartins Offline OP
Newbie
fabiomartins  Offline OP
Newbie
F

Joined: Aug 2011
Posts: 42
Hello Everybody!

How to implement Camera Collision in the Multiplayer Workshop?
I,m using simple_camera Function, But isn´t work fine...

Download Full Source:
http://www.mediafire.com/?b3wh245826st3mh

Code:

#include <acknex.h>
#include <default.c>
#include <acknet.h>


var client_ent = 0; // stores the handle to the client entity (client's player)
var server_fired = 0; // is set to 1 if the player that runs the server fires a shot
var client_fired = 0; // is set to 1 if the player that runs the client fires a shot
var bullet_pan; // stores the pan angle of the server or client player
var players_health;

#define health skill20

STRING* meu_ip = " ";



BMAP* hit_tga = "hit.tga";

PANEL* health_pan =
{
bmap = "health.tga";
pos_x = -6; // don't display the tiny red bad that remains when player's health is zero (panel.scale_x = 0.01 in this case)
pos_y = 0;
flags = visible;
}






function abre_ip(){


var id = http_sendpost("http://coniserver.net/scratch/ip.php","");
while (!http_status(id))
{
wait(1);//wait for the server to reply
}

if (http_status(id) == 1) { //transfer successful?
http_result(id,meu_ip); //get the replied IP
printf(_chr(meu_ip));
http_free(id);
} else

http_free(id);


}

function mouse_event(code) {
if (code == 280)
{
code = 283;
abre_ip();
//printf("left key pressed!");
wait(-10);

}
if (code == 281)
{
printf("right key pressed!");
}
if (code == 282)
{
printf("middle key pressed!");
}
}





function damage_players(){
if (you.skill99 != 1234) {return;} // the player hasn't collided with a bullet? Then nothing should happen!
my.health -= 10;
my.health = maxv(0, my.health); // don't allow my.health to reach negative values
send_skill(my.health, 0); // send any health value change over the network
}


function fire_bullets()// this function is called from main, so it runs on the server and on the client
{
if (connection == 2) // this section runs on the client
{
client_fired = 1; // we set client_fired for a frame, and then we reset it
wait (1);
client_fired = 0;
}
if (connection == 3) // this section runs on the server
{
server_fired = 1; // we set server_fired for a frame, and then we reset it
wait (1);
server_fired = 0;
}
}




function simple_camera()
{

vec_set (camera.x, my.x);
camera.x = my.x - 25 * cos(my.pan); // 250 = distance between the player and the camera, play with this value
camera.y = my.y - 25 * sin(my.pan); // use the same value here
camera.z = my.z + 30; // place the camera above the player, play with this value
camera.pan = my.pan; // the camera and the player have the same pan angle
wait (1);
}

function move_players() // both entities are moved by the server
{
// my.gravity = 6; // needed for
// my.zoffset = 2;
var walk_percentage;
var stand_percentage;
var death_percentage = 0;
my.emask |= (ENABLE_IMPACT | ENABLE_ENTITY);
my.smask |= (NOSEND_ALPHA | NOSEND_COLOR | NOSEND_LIGHT | NOSEND_SCALE | NOSEND_SKIN |NOSEND_ATTACH );
my.event = damage_players;
my.health = 100;
my.ambient = 40;
// new code
// send the health value once, at the very beginning, in order to update the health value on the client player at game start
send_skill(my.health, 0);

while (my.health > 0)
{

c_move(my, vector(my.skill1, 0, 0), nullvector, IGNORE_PASSABLE | USE_POLYGON | USE_AABB | USE_POLYGON); // move the entity using its own skill1
my.pan += my.skill2; // and rotate it using its own skill2





if (my.skill1) // the player is moving the entity? (skill1 isn't zero)
{
walk_percentage += 1.5 * my.skill1 * sign(my.skill1); // allow forward / back animations
ent_animate(my, "run", walk_percentage, ANM_CYCLE); // animate the entity
stand_percentage = 0; // reset stand_percentage
}
else
{
stand_percentage += 1.2 * time_step; // the "stand" animation values don't depend on player's input
ent_animate(my, "idle", stand_percentage, ANM_CYCLE); // animate the entity
walk_percentage = 0; // reset walk_percentage
}
ent_sendnow(my); // send the updated entity position regardless of the "dplay_entrate" value, etc
wait (1);
}



while (death_percentage < 100)
{
ent_animate(my, "DeathHeadshot", death_percentage, NULL); // play the "death" animation
death_percentage += 5 * time_step;
ent_sendnow(my); // send the updated entity position regardless of the "dplay_entrate" value, etc

wait (1);
}

}



function fade_hits(PARTICLE *p)
{
p.alpha -= 4 * time_step; // fade out the hit_tga particles
if (p.alpha < 0)
p.lifespan = 0;
}

function player_hit(PARTICLE *p)
{
p->vel_x = 0.2 - random(1) / 4;
p->vel_y = 0.2 - random(1) / 4;
p->vel_z = 0.3 + random(1) / 3;
p.alpha = 25 + random(50);
p.bmap = hit_tga;
p.size = 5; // gives the size of the hit particles
p.flags |= (BRIGHT | MOVE);
p.event = fade_hits;
}

function hit_effect() // this function will generate particles only on the client
{
effect(player_hit, 5, my.x, nullvector); // generate 5 particles each frame
}

function remove_bullets() // this function runs when the bullet collides with something
{
if (connection == 2) // this section runs on the client?
{
proc_client(my, hit_effect); // then start a function that will only run on the client
}
else // this section runs on the server
{
// under normal conditions (without checking the "connection" value) this would create particles on the client, as well as on the server
effect(player_hit, 5, my.x, nullvector);
}
wait (1); // wait a frame to be sure (don't trigger engine warnings)
ent_remove (my); // and then remove the bullet
}

function move_bullets() // this function runs on the server (any ent_create instruction is run on the server)
{
VECTOR bullet_speed[3]; // stores the speed of the bullet
my.emask = ENABLE_IMPACT | ENABLE_ENTITY | ENABLE_BLOCK; // the bullet is sensitive to impacts with entities and level blocks
my.event = remove_bullets; // when it collides with something, its event function (remove_bullets) will run
my.pan = bullet_pan; // my.pan could be set to server's player.pan or client's player.pan
my.skill99 = 1234; // set a unique identifier for the bullets
bullet_speed.x = 50 * time_step; // adjust the speed of the bullet here
bullet_speed.y = 0; // the bullet doesn't move sideways
bullet_speed.z = 0; // or up / down on the z axis
while (my) // this loop will run for as long as the bullet exists (it isn't "NULL")
{
c_move (my, bullet_speed, nullvector, IGNORE_PASSABLE | IGNORE_YOU); // move the bullet ignoring its creator (the player)
ent_sendnow(my); // send the updated bullet position each frame (ensures smooth bullet movement on the client as well)
wait (1);
}
}



function main()
{
dplay_entrate = 4;
dplay_smooth = 0; // dead reckoning not needed
dplay_port = 8000;

video_window(NULL,NULL,0,"Procurando Sessão...");
fps_max = 60; // limit the number of data packets that are sent over the network each second to 60
//video_screen = 1; //Tela Cheia!
// on_mouse_left = fire_bullets; // the player use the left mouse buttons to fire
random_seed(0);
if (connection == 0)
{
if (!session_connect("plasticwar",""))
{
session_open("plasticwar"); // become the server
}
}

if (connection & CONNECT_CLIENT) // this instance of the game runs as a client?
{

level_load ("multiplayer11.wmb");
wait(1);

my = ent_create ("redsoldier.mdl", vector (random(100)+50,random(50)+25, 40), move_players); // then create the red soldier!
wait (-0.5); // wait until the handle is ready and valid for all the clients (this line is very important!)




while (1)
{

my.skill1 = 5 * (key_w - key_s) * time_step;
my.skill2 = 8 * (-mouse_force.x) * time_step;

send_skill (my.skill1, 0);
send_skill (my.skill2, 0);
send_skill (my.skill3, 0);

on_mouse_left = mouse_event;

simple_camera();

wait (1);
// players_health = my.skill20; // display the proper player health value on the client
// health_pan.scale_x = maxv(0.01, my.health / 100); // set a proper scale_x value for the health panel (but don't allow it to be set to 0)

}
}

// if (connection & CONNECT_SERVER) // this instance of the game runs as a server and client at the same time? (connection = 3)
// {
// level_load ("multiplayer11.wmb");
// wait(1);
// my = ent_create ("bluesoldier.mdl", vector (-100, -50, 40), move_players); // create the blue soldier
// wait (-0.5);
// while (1)
// {
// my.skill1 = 5 * (key_w - key_s) * time_step;
// my.skill2 = 8 * (-mouse_force.x) * time_step;
// simple_camera(); // call the simple camera function
// players_health = my.health; // display the proper player health value on the server
// on_mouse_left = mouse_event;
// health_pan.scale_x = maxv(0.01, my.health / 100); // set a proper scale_x value for the health panel (but don't allow it to be set to 0)
// wait (1);
// }
// }


}

Re: Camera Collision in Workshop 25 - Multiplayer 11 [Re: fabiomartins] #381591
08/30/11 07:06
08/30/11 07:06
Joined: Aug 2011
Posts: 42
F
fabiomartins Offline OP
Newbie
fabiomartins  Offline OP
Newbie
F

Joined: Aug 2011
Posts: 42
I fix the issue
just add this function


void updateCam(ENTITY* ent)
{
//Positionsparamter
ANGLE camang;
vec_set(camang, vector(180, 60, 0));//Posicionamento da camera
var camdist = 52; // Distancia da camera em relação ao player
var camheight = 34; // altura da camera
var tiltmin = -20; //
var tiltmax = 20;
var collshorten = 25;

//Updateparameter
var angupdate = 0.5;
var centerupdate = 0.7;
var distupdate = 0.3;
var tiltspeed = 10;

//aktuelle Kameraparameter
ANGLE currang;
vec_set(currang, ent.pan);
vec_add(currang, camang);
VECTOR currcenter;
vec_set(currcenter, vector(ent.x, ent.y, ent.z));
var currdist = camdist;

//Temporäres
ANGLE targetang;
VECTOR tempvec;
var tempdist;

proc_mode = PROC_LATE;
while(1)
{
//Steuern der Höhe mit der Maus
camang.tilt -= mouse_force.y*tiltspeed*time_step;
camang.tilt = clamp(camang.tilt, tiltmin, tiltmax);

//Setzen der relativen Zielposition
vec_set(targetang, ent.pan);
vec_add(targetang, camang);

//relatives Positionsupdate
vec_diff(tempvec, targetang, currang);
vec_scale(tempvec, angupdate*time_step);
vec_add(currang, tempvec);

//absolutes Positionsupdate
vec_diff(tempvec, ent.x, currcenter);
vec_scale(tempvec, centerupdate*time_step);
vec_add(currcenter, tempvec);

//setzen der resultierenden Position
vec_set(camera.x, vector(camdist, 0, 0));
vec_rotate(camera.x, currang);
vec_add(camera.x, vector(currcenter.x, currcenter.y, currcenter.z+camheight));

//Kollision der Kamera
you = ent;
tempdist = c_trace(vector(currcenter.x, currcenter.y, currcenter.z+camheight), camera.x, IGNORE_YOU|USE_POLYGON);
tempdist = minv(tempdist, abs(tempdist-collshorten));
if(tempdist > 0 && currdist >= tempdist)
{
currdist = tempdist;
}else
{
if(tempdist > 0)
{
currdist += (tempdist-currdist)*distupdate*time_step;
}else
{
currdist += (camdist-currdist)*distupdate*time_step;
}
}

//Anpassen der Position bei Kollision
vec_diff(camera.x, camera.x, vector(currcenter.x, currcenter.y, currcenter.z+camheight));
vec_normalize(camera.x, currdist);
vec_add(camera.x, vector(currcenter.x, currcenter.y, currcenter.z+camheight));

//setzen der Blickrichtung
vec_diff(camera.pan, vector(ent.x, ent.y, ent.z+camheight), camera.x);
vec_to_angle(camera.pan, camera.pan);
camera.roll = 0;

wait(1);
}
}


Moderated by  HeelX, Spirit 

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