Jump to content


Photo
- - - - -

Two Objects In One View Example


  • Please log in to reply
30 replies to this topic

#1 daman123125

daman123125

    AZN Game Programmer

  • GMC Member
  • 1957 posts
  • Version:GM8

Posted 16 January 2009 - 07:47 AM

  • Title: Two Objects in View Example
  • Description: This simple example will teach you how to put two objects in a view.
  • GM Version: GM7
  • Registered: No
  • File Type: .ZIP
  • File Size: 39.09 KB
  • File Link: http://www.mediafire.com/?wa9twaolago
Additional Info:
This example shows how two objects are put into one view. It was made in simple GML, but with comments to make sure you know what is going on. It requires no credit whatsoever. Also, you can use the graphics without giving credit, which were made in seconds.
Here is the simple script to use when you want two objects in a view:
/*
TWO OBJECTS IN VIEW SCRIPT

No credit is required to use this simple script!
How to use: two_object_view(otherobject,viewnumber)

By: daman123125*/

view_hview[argument1]=(distance_to_object(argument
0)*1.5)+150 //sets the height of the view to the distance to the second object times 1.5 plus 150
view_wview[argument1]=(distance_to_object(argument
0)*2)+180 //sets the width of the view to the distance to the second object times 2 plus 180
view_xview[argument1]=((x+argument0.x)/2)-(view_wview[argument1]/2)//sets the x position of the view
view_yview[argument1]=((y+argument0.y)/2)-(view_hview[argument1]/2)// sets the y position of the view

Edited by daman123125, 16 April 2013 - 02:37 AM.

  • 3
Posted ImagePosted Image
Posted ImagePosted Image

Tuts/Examples: Ultimate Weapon Arsenal(AI included) | Wall-Jumping | Plasma Pistol/Rifle Overheating System | Cash Register Example | Two Objects In One View
PM me if one or more of my posted examples have a broken link.

#2 Snotrocket

Snotrocket

    GMC Member

  • New Member
  • 121 posts

Posted 17 January 2009 - 12:59 AM

hey thats a very nice example! I never hought it would be that easy to change the view like that. Everything is very well documkented except the script, which i think would need the most. I don't understand which parts of the script do what and why you chose those numbers :s
But it might just be me because I'm already busy making a game and trying to learn a thousand things at once so I don't have too much time to be tweaking code I wont need.

but i'll definetly find this usefull in the future! thanks alot!
  • 0

#3 daman123125

daman123125

    AZN Game Programmer

  • GMC Member
  • 1957 posts
  • Version:GM8

Posted 17 January 2009 - 03:16 AM

Ok. I've edited the first post to show the script, with comments.
Thanks for the comment. :)
  • 0
Posted ImagePosted Image
Posted ImagePosted Image

Tuts/Examples: Ultimate Weapon Arsenal(AI included) | Wall-Jumping | Plasma Pistol/Rifle Overheating System | Cash Register Example | Two Objects In One View
PM me if one or more of my posted examples have a broken link.

#4 Snotrocket

Snotrocket

    GMC Member

  • New Member
  • 121 posts

Posted 17 January 2009 - 03:26 AM

:)
i totally get it now! thanks alot that was really helpfull!
  • 0

#5 daman123125

daman123125

    AZN Game Programmer

  • GMC Member
  • 1957 posts
  • Version:GM8

Posted 17 January 2009 - 09:13 PM

No problem.

I've been seeing a lot of people trying to get 2 objects in a view. Anyone else want to comment?
  • 0
Posted ImagePosted Image
Posted ImagePosted Image

Tuts/Examples: Ultimate Weapon Arsenal(AI included) | Wall-Jumping | Plasma Pistol/Rifle Overheating System | Cash Register Example | Two Objects In One View
PM me if one or more of my posted examples have a broken link.

#6 Mocgames998

Mocgames998

    GMC Member

  • GMC Member
  • 118 posts

Posted 18 January 2009 - 01:09 AM

Congratulations, you perfectly emulated the SSB camera system without much GML! :)
  • 0

???

It looks like I can finally put something here, before the GMC goes down:

Spoiler

#7 daman123125

daman123125

    AZN Game Programmer

  • GMC Member
  • 1957 posts
  • Version:GM8

Posted 18 January 2009 - 01:42 AM

Well, this tutorial only has two objects, and in SSB, there's 4 players...
But, anyways, thanks for the comment! :)
  • 0
Posted ImagePosted Image
Posted ImagePosted Image

Tuts/Examples: Ultimate Weapon Arsenal(AI included) | Wall-Jumping | Plasma Pistol/Rifle Overheating System | Cash Register Example | Two Objects In One View
PM me if one or more of my posted examples have a broken link.

#8 deric007

deric007

    GMC Member

  • GMC Member
  • 165 posts

Posted 04 February 2009 - 08:44 PM

Very ce example!! ^_^ :)
  • 0

#9 appledave42

appledave42

    GMC Member

  • New Member
  • 1 posts

Posted 11 March 2009 - 04:34 PM

Dude, ty. This is v. easy way but it works well. You could make it so the camera stays on screen, but I'll add that myself 4 my game. :)
  • 0

#10 digidog08

digidog08

    GMC Member

  • New Member
  • 69 posts

Posted 30 May 2009 - 07:09 PM

SSB Camera Lite!


A few request:
I am making an action battle sidescroller. I want the camera to focus on the closet object parented with par_enemy and the main player's object. And when it destroyed, set the view back to normal until another object parented with par_enemy is close.

I also want it to set the view back to normal if the distance_to_object(hero) and any instance with the parent "par_enemy" is out of view (or 400 pixels away).

I'm decent in GML, so i should be able to follow any reply given.

Edited by digidog08, 31 May 2009 - 01:31 AM.

  • 0
 Dell Latitude E6400 | Intel Core 2 Duo | 4 GB Ram | 500 GB Hard Drive | Windows Vista Ultimate - Windows 7 - Ubuntu "Jaunty" - iPc MAC OSX 10
| 22' LCD Monitor | Vistablet 12" Tablet | 16x DVD - RW |

Software: Adobe Creative Suite CS4 | Blender 3D | 3DS Max | Mixcraft | Garageband | Game Maker 7 Pro | FLStudio 8 | Final Cut Pro |
I need a composer for my Game

#11 piedude04

piedude04

    GMC Member

  • New Member
  • 330 posts

Posted 31 May 2009 - 06:29 AM

Hmm...how would you do this with one object and the mouse? Since the cursor pretty much moves along when the view changes, so it goes crazy.
  • 0

#12 daman123125

daman123125

    AZN Game Programmer

  • GMC Member
  • 1957 posts
  • Version:GM8

Posted 31 May 2009 - 10:11 PM

You can just edit the script:
view_hview[argument1]=(distance_to_object(argument0)*1.5)+150
view_wview[argument1]=(distance_to_object(argument0)*2)+180
view_xview[argument1]=((x+argument0.x)/2)-(view_wview[0]/2)
view_yview[argument1]=((y+argument0.y)/2)-(view_hview[0]/2)
To this:
view_hview[argument1]=(distance_to_point(mouse_x,mouse_y)*1.5)+150
view_wview[argument1]=(distance_to_point(mouse_x,mouse_y)*2)+180
view_xview[argument1]=((x+mouse_x)/2)-(view_wview[0]/2)
view_yview[argument1]=((y+mouse_y)/2)-(view_hview[0]/2)

  • 0
Posted ImagePosted Image
Posted ImagePosted Image

Tuts/Examples: Ultimate Weapon Arsenal(AI included) | Wall-Jumping | Plasma Pistol/Rifle Overheating System | Cash Register Example | Two Objects In One View
PM me if one or more of my posted examples have a broken link.

#13 piedude04

piedude04

    GMC Member

  • New Member
  • 330 posts

Posted 01 June 2009 - 07:01 AM

I still get the problem: it still goes crazy. Since the cursor changes position in the room as the view changes size, the view would keep getting bigger or smaller even when the user isn't moving the mouse.
  • 0

#14 daman123125

daman123125

    AZN Game Programmer

  • GMC Member
  • 1957 posts
  • Version:GM8

Posted 01 June 2009 - 02:59 PM

Try using window_set_mouse to change the position.
I'm not sure how you would do it though.
  • 0
Posted ImagePosted Image
Posted ImagePosted Image

Tuts/Examples: Ultimate Weapon Arsenal(AI included) | Wall-Jumping | Plasma Pistol/Rifle Overheating System | Cash Register Example | Two Objects In One View
PM me if one or more of my posted examples have a broken link.

#15 chocoblock

chocoblock

    GMC Member

  • New Member
  • 35 posts

Posted 01 June 2009 - 08:25 PM

I really like how this works its a very nice and easy to implement. :)
  • 0

#16 digidog08

digidog08

    GMC Member

  • New Member
  • 69 posts

Posted 02 June 2009 - 01:51 AM

I solved my problem. I edited to where i created an invisible object that turns into a "zoom in object"
SO like, if you are moving towards an area (such as a hill) It will zoom in as you approach it, and zoom out as you leave it.
  • 0
 Dell Latitude E6400 | Intel Core 2 Duo | 4 GB Ram | 500 GB Hard Drive | Windows Vista Ultimate - Windows 7 - Ubuntu "Jaunty" - iPc MAC OSX 10
| 22' LCD Monitor | Vistablet 12" Tablet | 16x DVD - RW |

Software: Adobe Creative Suite CS4 | Blender 3D | 3DS Max | Mixcraft | Garageband | Game Maker 7 Pro | FLStudio 8 | Final Cut Pro |
I need a composer for my Game

#17 capitankl

capitankl

    GMC Member

  • New Member
  • 1 posts

Posted 20 July 2009 - 10:27 PM

thanks so much I have made a 4 players race but I hope this can work very googd your example, I understand everything Thanks!!!
  • 0

#18 Strickendead

Strickendead

    GMC Member

  • New Member
  • 1 posts

Posted 20 January 2010 - 10:04 PM

view_xview[argument1]=((x+argument0.x)/2)-(view_wview[0]/2)//sets the x position of the view
view_yview[argument1]=((y+argument0.y)/2)-(view_hview[0]/2)// sets the y position of the view



In the last part of these two lines "(view_hview[0]/2)", is there a reason you used 0 on this one or should it be argument1? "(view_hview[argument1]/2)"

Edited by Strickendead, 20 January 2010 - 10:05 PM.

  • 0

#19 daman123125

daman123125

    AZN Game Programmer

  • GMC Member
  • 1957 posts
  • Version:GM8

Posted 21 January 2010 - 12:38 AM

It doesn't exactly matter, since there is only one view.
You probably wouldn't need multiple views, unless in the rare case you do, argument1 is there.
argument1 is just what view you are using, and goes from 0 to 7.
  • 0
Posted ImagePosted Image
Posted ImagePosted Image

Tuts/Examples: Ultimate Weapon Arsenal(AI included) | Wall-Jumping | Plasma Pistol/Rifle Overheating System | Cash Register Example | Two Objects In One View
PM me if one or more of my posted examples have a broken link.

#20 Braden

Braden

    GMC Member

  • New Member
  • 12 posts

Posted 16 February 2010 - 10:29 AM

if anyones interested im in the process of making one that uses a ds list for the objects to follow
so you can add an object to the list and the camera will adjust to follow more then 2 objects

for now heres a mod to the script to keep it within the bounds of the room size, enjoy


view_wview[argument1]=min(view_wport[argument1],max(distance_to_object(argument0)*2,view_wport[argument1]/2)); // sets the width of the view
view_hview[argument1]=min(view_hport[argument1],max(distance_to_object(argument0)*2,view_hport[argument1]/2)); // sets the height of the view
view_xview[argument1]=((x+argument0.x)/2)-(view_wview[argument1]/2) // sets the x position of the view
view_yview[argument1]=((y+argument0.y)/2)-(view_hview[argument1]/2) // sets the y position of the view

// keep view within bounds of room
if view_xview[argument1]+view_wview[argument1]>room_width {view_xview[argument1]=room_width-view_wview[argument1];}
if view_yview[argument1]+view_hview[argument1]>room_height {view_yview[argument1]=room_height-view_hview[argument1];}
if view_xview[argument1]<0 {view_xview[argument1]=0;}
if view_yview[argument1]<0 {view_yview[argument1]=0;}

<shameless plug> when i get the new camera script working it will be in my TDS [see sig] </ shamless plug>
  • 0

#21 Rudey

Rudey

    GMC Member

  • New Member
  • 1 posts

Posted 01 September 2010 - 07:37 PM

This might be an old topic, but it's the only one about 'two object views'.

I didn't like the current one so I editted it a lot.
And now I cannot help myself posting my version up here...

I made my script compatible with every kind of aspect ratio.
It doesn't matter what the room width and height are, this will work.

I've also declared a couple of new variables so the script doesn't look difficult.
Don't pay attention to them. And don't pay attention to anything beneath it.
Just pay attention to the variables minzoom and maxzoom.
Oh, and change objects player1 and player2 to what you like.

minzoom=1                           // sets the minimal zoom
maxzoom=2.5                         // sets the maximal zoom

min_wview=room_width/maxzoom        // sets the minimal width of the view
min_hview=room_height/maxzoom       // sets the minimal height of the view
max_wview=room_width/minzoom        // sets the maximal width of the view
max_hview=room_height/minzoom       // sets the maximal height of the view

// calculates the size and position of the view

xdistance=point_distance(player1.x,0,player2.x,0)
ydistance=point_distance(0,player1.y,0,player2.y)

if xdistance/room_width>ydistance/room_height {
    view_wview[0]=xdistance+room_width/4
    if view_wview[0]<min_wview {view_wview[0]=min_wview}
    if view_wview[0]>max_wview {view_wview[0]=max_wview}
    view_hview[0]=view_wview[0]/room_width*room_height
}
else {
    view_hview[0]=ydistance+room_height/4
    if view_hview[0]<min_hview {view_hview[0]=min_hview}
    if view_hview[0]>max_hview {view_hview[0]=max_hview}
    view_wview[0]=view_hview[0]/room_height*room_width
}

view_xview[0]=(player1.x+player2.x)/2-view_wview[0]/2
view_yview[0]=(player1.y+player2.y)/2-view_hview[0]/2

// keeps the view inside the room

if view_xview[0]+view_wview[0]>room_width {
    view_xview[0]=room_width-view_wview[0]
}

if view_yview[0]+view_hview[0]>room_height {
    view_yview[0]=room_height-view_hview[0]
}

if view_xview[0]<0 {
    view_xview[0]=0
}

if view_yview[0]<0 {
    view_yview[0]=0
}

  • 1

#22 _191723

_191723

    GMC Member

  • New Member
  • 16 posts

Posted 20 March 2011 - 04:36 PM

You are a genius, thank you!
  • 0

#23 Tobias(NL)

Tobias(NL)

    GMC Member

  • GMC Member
  • 896 posts
  • Version:Unknown

Posted 14 February 2012 - 06:35 PM

Pretty flashy, but it zooms in way too much. It needs a limiter or something of the kind.
Overall the example was smooth and worked well!
TobiasNL
  • 0

Posted Image
-Click the image for both the download & topic
Visit my homepage [Aquarel Games.tk]


#24 _206203

_206203

    GMC Member

  • New Member
  • 5 posts
  • Version:GM:Studio

Posted 20 April 2012 - 04:34 AM

why like this?

Posted Image
  • -1

#25 ironmaneleven

ironmaneleven

    GMC Member

  • New Member
  • 2 posts
  • Version:GM:Studio

Posted 09 December 2012 - 07:10 AM

I am making a 2D multiplayer game with 2 players and I want it to zoom out when they get further away from each other, but I also don't
want it to zoom in too far when they are close to each other. How should I alter this script to function this way? Please help
  • 0

#26 ironmaneleven

ironmaneleven

    GMC Member

  • New Member
  • 2 posts
  • Version:GM:Studio

Posted 09 December 2012 - 07:31 AM

[quote name='Rudey' date='02 September 2010 - 05:23 AM' timestamp='1283368984' post='3595109']
This might be an old topic, but it's the only one about 'two object views'.

I didn't like the current one so I editted it a lot.
And now I cannot help myself posting my version up here...

I made my script compatible with every kind of aspect ratio.
It doesn't matter what the room width and height are, this will work.

I've also declared a couple of new variables so the script doesn't look difficult.
Don't pay attention to them. And don't pay attention to anything beneath it.
Just pay attention to the variables minzoom and maxzoom.
Oh, and change objects player1 and player2 to what you like.

minzoom=1                           // sets the minimal zoom
maxzoom=2.5                         // sets the maximal zoom

min_wview=room_width/maxzoom        // sets the minimal width of the view
min_hview=room_height/maxzoom       // sets the minimal height of the view
max_wview=room_width/minzoom        // sets the maximal width of the view
max_hview=room_height/minzoom       // sets the maximal height of the view

// calculates the size and position of the view

xdistance=point_distance(player1.x,0,player2.x,0)
ydistance=point_distance(0,player1.y,0,player2.y)

if xdistance/room_width>ydistance/room_height {
    view_wview[0]=xdistance+room_width/4
    if view_wview[0]<min_wview {view_wview[0]=min_wview}
    if view_wview[0]>max_wview {view_wview[0]=max_wview}
    view_hview[0]=view_wview[0]/room_width*room_height
}
else {
    view_hview[0]=ydistance+room_height/4
    if view_hview[0]<min_hview {view_hview[0]=min_hview}
    if view_hview[0]>max_hview {view_hview[0]=max_hview}
    view_wview[0]=view_hview[0]/room_height*room_width
}

view_xview[0]=(player1.x+player2.x)/2-view_wview[0]/2
view_yview[0]=(player1.y+player2.y)/2-view_hview[0]/2

// keeps the view inside the room

if view_xview[0]+view_wview[0]>room_width {
    view_xview[0]=room_width-view_wview[0]
}

if view_yview[0]+view_hview[0]>room_height {
    view_yview[0]=room_height-view_hview[0]
}

if view_xview[0]<0 {
    view_xview[0]=0
}

if view_yview[0]<0 {
    view_yview[0]=0
}

Thankyou for this code, but how do I apply to my code. sorry I am very new to adding scripts into my game.
  • -1

#27 ironmaneleven

ironmaneleven

    GMC Member

  • New Member
  • 2 posts
  • Version:GM:Studio

Posted 09 December 2012 - 07:32 AM

Thankyou for this code, but how do I apply to my code. sorry I am very new to adding scripts into my game.

Edited by ironmaneleven, 09 December 2012 - 07:33 AM.

  • 0

#28 AlienJordanwolf

AlienJordanwolf

    GMC Member

  • New Member
  • 1 posts
  • Version:GM8

Posted 03 February 2013 - 07:53 AM

Thank You SOOOO MUCH! I've been needing this for a long time, and i finally found it ::lmao:: ! I might actually even give credit just because you deserve it A LOT :D
  • 0

#29 shadowxau

shadowxau

    GMC Member

  • New Member
  • 8 posts
  • Version:Unknown

Posted 12 November 2013 - 12:09 PM

This might be an old topic, but it's the only one about 'two object views'.

I didn't like the current one so I editted it a lot.
And now I cannot help myself posting my version up here...

I made my script compatible with every kind of aspect ratio.
It doesn't matter what the room width and height are, this will work.

I've also declared a couple of new variables so the script doesn't look difficult.
Don't pay attention to them. And don't pay attention to anything beneath it.
Just pay attention to the variables minzoom and maxzoom.
Oh, and change objects player1 and player2 to what you like.
 

minzoom=1                           // sets the minimal zoom
maxzoom=2.5                         // sets the maximal zoom

min_wview=room_width/maxzoom        // sets the minimal width of the view
min_hview=room_height/maxzoom       // sets the minimal height of the view
max_wview=room_width/minzoom        // sets the maximal width of the view
max_hview=room_height/minzoom       // sets the maximal height of the view

// calculates the size and position of the view

xdistance=point_distance(player1.x,0,player2.x,0)
ydistance=point_distance(0,player1.y,0,player2.y)

if xdistance/room_width>ydistance/room_height {
    view_wview[0]=xdistance+room_width/4
    if view_wview[0]<min_wview {view_wview[0]=min_wview}
    if view_wview[0]>max_wview {view_wview[0]=max_wview}
    view_hview[0]=view_wview[0]/room_width*room_height
}
else {
    view_hview[0]=ydistance+room_height/4
    if view_hview[0]<min_hview {view_hview[0]=min_hview}
    if view_hview[0]>max_hview {view_hview[0]=max_hview}
    view_wview[0]=view_hview[0]/room_height*room_width
}

view_xview[0]=(player1.x+player2.x)/2-view_wview[0]/2
view_yview[0]=(player1.y+player2.y)/2-view_hview[0]/2

// keeps the view inside the room

if view_xview[0]+view_wview[0]>room_width {
    view_xview[0]=room_width-view_wview[0]
}

if view_yview[0]+view_hview[0]>room_height {
    view_yview[0]=room_height-view_hview[0]
}

if view_xview[0]<0 {
    view_xview[0]=0
}

if view_yview[0]<0 {
    view_yview[0]=0
}

This is a fantastic script but I am having issues modifying it to work with 4 (or more) players to track the 2 players furthest apart on the screen.

 

Can anyone provide any tips on how to get this working?


  • 0

#30 Evilbecky

Evilbecky

    GMC Member

  • GMC Member
  • 218 posts
  • Version:GM:Studio

Posted 12 December 2013 - 08:24 PM

Is there a way to make a max zoom out or zoom in if so how?

Thanks


  • 0

#31 Gurman8r

Gurman8r

    GMC Member

  • GMC Member
  • 194 posts
  • Version:GM:Studio

Posted 20 January 2014 - 11:56 PM

Great job! I love it!


  • 0